From c4eb4c97ac1f186e5054da298e4960cb498c6d01 Mon Sep 17 00:00:00 2001 From: ferdiemmen Date: Mon, 29 Jan 2018 22:40:08 +0100 Subject: fix(ads): Fixes media from playing when ads are blocked --- src/js/plugins/ads.js | 8 +++++++- src/js/plyr.js | 5 +++-- src/js/utils.js | 16 +++++++++++++++- 3 files changed, 25 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/js/plugins/ads.js b/src/js/plugins/ads.js index 5c828141..c59c4667 100644 --- a/src/js/plugins/ads.js +++ b/src/js/plugins/ads.js @@ -18,6 +18,7 @@ class Ads { this.player = player; this.playing = false; this.initialized = false; + this.blocked = false; this.enabled = utils.is.url(player.config.ads.tag); // Check if a tag URL is provided. @@ -25,10 +26,15 @@ class Ads { 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)) { utils.loadScript(player.config.urls.googleIMA.api, () => { 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 { this.ready(); diff --git a/src/js/plyr.js b/src/js/plyr.js index 36b74e76..3cbbed4b 100644 --- a/src/js/plyr.js +++ b/src/js/plyr.js @@ -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() { - if (this.ads.enabled && !this.ads.initialized) { + if (this.ads.enabled && !this.ads.initialized && !this.ads.blocked) { this.ads.play(); + return; } this.media.play(); diff --git a/src/js/utils.js b/src/js/utils.js index 155dc087..7c277301 100644 --- a/src/js/utils.js +++ b/src/js/utils.js @@ -82,7 +82,7 @@ const utils = { }, // Load an external script - loadScript(url, callback) { + loadScript(url, callback, error) { const current = document.querySelector(`script[src="${url}"]`); // Check script is not already referenced, if so wait for load @@ -99,6 +99,10 @@ const utils = { element.callbacks = element.callbacks || []; element.callbacks.push(callback); + // Error queue + element.errors = element.errors || []; + element.errors.push(error); + // Bind callback if (utils.is.function(callback)) { 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 element.src = url; -- cgit v1.2.3 From afd695cb391535b508dc7a4301b6c2692ce2b607 Mon Sep 17 00:00:00 2001 From: ferdiemmen Date: Mon, 29 Jan 2018 22:45:46 +0100 Subject: Fix typo's --- src/js/plugins/ads.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/js/plugins/ads.js b/src/js/plugins/ads.js index c59c4667..0059b963 100644 --- a/src/js/plugins/ads.js +++ b/src/js/plugins/ads.js @@ -26,13 +26,13 @@ class Ads { return; } - // Check if the Google IMA3 SDK is loaded or load ourselves. + // Check if the Google IMA3 SDK is loaded or load it ourselves if (!utils.is.object(window.google)) { utils.loadScript(player.config.urls.googleIMA.api, () => { this.ready(); }, () => { - // Script failed to load or is blocked. + // Script failed to load or is blocked this.blocked = true; this.player.debug.log('Ads error: Google IMA SDK failed to load'); }); -- cgit v1.2.3