fix(ads): Fixes media from playing when ads are blocked

This commit is contained in:
ferdiemmen 2018-01-29 22:40:08 +01:00
parent cc128e6088
commit c4eb4c97ac
6 changed files with 36 additions and 7 deletions

2
dist/plyr.js vendored

File diff suppressed because one or more lines are too long

2
dist/plyr.js.map vendored

File diff suppressed because one or more lines are too long

View File

@ -46,7 +46,15 @@
"stylelint-selector-bem-pattern": "^2.0.0", "stylelint-selector-bem-pattern": "^2.0.0",
"uglify-es": "^3.3.8" "uglify-es": "^3.3.8"
}, },
"keywords": ["HTML5 Video", "HTML5 Audio", "Media Player", "DASH", "Shaka", "WordPress", "HLS"], "keywords": [
"HTML5 Video",
"HTML5 Audio",
"Media Player",
"DASH",
"Shaka",
"WordPress",
"HLS"
],
"repository": { "repository": {
"type": "git", "type": "git",
"url": "git://github.com/sampotts/plyr.git" "url": "git://github.com/sampotts/plyr.git"

View File

@ -18,6 +18,7 @@ class Ads {
this.player = player; this.player = player;
this.playing = false; this.playing = false;
this.initialized = false; this.initialized = false;
this.blocked = false;
this.enabled = utils.is.url(player.config.ads.tag); this.enabled = utils.is.url(player.config.ads.tag);
// Check if a tag URL is provided. // Check if a tag URL is provided.
@ -25,10 +26,15 @@ class Ads {
return; return;
} }
// Check if the Google IMA3 SDK is loaded // Check if the Google IMA3 SDK is loaded or load ourselves.
if (!utils.is.object(window.google)) { if (!utils.is.object(window.google)) {
utils.loadScript(player.config.urls.googleIMA.api, () => { utils.loadScript(player.config.urls.googleIMA.api, () => {
this.ready(); this.ready();
}, () => {
// Script failed to load or is blocked.
this.blocked = true;
this.player.debug.log('Ads error: Google IMA SDK failed to load');
}); });
} else { } else {
this.ready(); this.ready();

View File

@ -306,11 +306,12 @@ class Plyr {
} }
/** /**
* Play the media, or play the advertisement * Play the media, or play the advertisement (if they are not blocked)
*/ */
play() { play() {
if (this.ads.enabled && !this.ads.initialized) { if (this.ads.enabled && !this.ads.initialized && !this.ads.blocked) {
this.ads.play(); this.ads.play();
return;
} }
this.media.play(); this.media.play();

View File

@ -82,7 +82,7 @@ const utils = {
}, },
// Load an external script // Load an external script
loadScript(url, callback) { loadScript(url, callback, error) {
const current = document.querySelector(`script[src="${url}"]`); const current = document.querySelector(`script[src="${url}"]`);
// Check script is not already referenced, if so wait for load // Check script is not already referenced, if so wait for load
@ -99,6 +99,10 @@ const utils = {
element.callbacks = element.callbacks || []; element.callbacks = element.callbacks || [];
element.callbacks.push(callback); element.callbacks.push(callback);
// Error queue
element.errors = element.errors || [];
element.errors.push(error);
// Bind callback // Bind callback
if (utils.is.function(callback)) { if (utils.is.function(callback)) {
element.addEventListener( element.addEventListener(
@ -111,6 +115,16 @@ const utils = {
); );
} }
// Bind error handling
element.addEventListener(
'error',
event => {
element.errors.forEach(err => err.call(null, event));
element.errors = null;
},
false,
);
// Set the URL after binding callback // Set the URL after binding callback
element.src = url; element.src = url;