aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSam Potts <sam@selz.com>2018-01-30 09:22:54 +1100
committerGitHub <noreply@github.com>2018-01-30 09:22:54 +1100
commitbb51647fe2f6e8e0eabb3099b58cc334e23936f0 (patch)
treef0ed45d84bc0304a543bafa08216355e63e55ac8 /src
parent8f7a8940f36ab85c87a3406a378d383e58ee3d60 (diff)
parent71efbe7a927a9638204129ca7a2d52c498fa7e6e (diff)
downloadplyr-bb51647fe2f6e8e0eabb3099b58cc334e23936f0.tar.lz
plyr-bb51647fe2f6e8e0eabb3099b58cc334e23936f0.tar.xz
plyr-bb51647fe2f6e8e0eabb3099b58cc334e23936f0.zip
Merge pull request #772 from sampotts/fix/ads-blocked
Fix: ads blocked and media playing before ad plays
Diffstat (limited to 'src')
-rw-r--r--src/js/plugins/ads.js18
-rw-r--r--src/js/plyr.js8
-rw-r--r--src/js/utils.js16
3 files changed, 33 insertions, 9 deletions
diff --git a/src/js/plugins/ads.js b/src/js/plugins/ads.js
index a71336bb..72fd49d8 100644
--- a/src/js/plugins/ads.js
+++ b/src/js/plugins/ads.js
@@ -35,17 +35,27 @@ class Ads {
this.enabled = player.config.ads.enabled;
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.
if (!this.enabled) {
return;
}
- // Check if the Google IMA3 SDK is loaded
+ // 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();
- });
+ 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 27019759..52fe378e 100644
--- a/src/js/plyr.js
+++ b/src/js/plyr.js
@@ -306,13 +306,13 @@ class Plyr {
}
/**
- * Play the media, or play the advertisement
+ * Play the media, or play the advertisement (if they are not blocked)
*/
play() {
- // Play the ad if setup
- // TODO: Fix the occasional play of the video before the Ad fires?
- if (this.ads.enabled && !this.ads.initialized) {
+ // TODO: Always return a promise?
+ if (this.ads.enabled && !this.ads.initialized && !this.ads.blocked) {
this.ads.play();
+ return null;
}
// Return the promise (for HTML5)
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;