Last active
August 29, 2015 14:11
-
-
Save dmlap/039056534c84903b94de to your computer and use it in GitHub Desktop.
A Brightcove Player plugin that loads javascript and stylesheet resources at runtime.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Adapted from | |
* https://github.com/brightcove/videojs-debugger/blob/d9380572dab701130e6b44a055f9dffb6e70f516/src/js/bootstrap.js#L77 | |
*/ | |
videojs.plugin('customPlugin', function() { | |
'use strict'; | |
var player = this, | |
loaded, | |
script, | |
stylesheet,; | |
// dynamically create a script element to fetch the additional javascript | |
script = document.createElement('script'), | |
// you can do the same trick with stylesheets, too | |
stylesheet = document.createElement('link'), | |
// make sure that the load handler is only invoked once | |
loaded = false; | |
// setup an onload handler to initialize the plugin once the runtime | |
// resources are downloaded | |
script.onload = script.onreadystatechange = function() { | |
if (!loaded && (!this.readyState || this.readyState === 'loaded' || this.readyState === 'complete')) { | |
loaded = true; | |
script.onload = script.onreadystatechange = null; | |
// initialize the plugin now that the additional javascript is | |
// loaded and ready | |
player.customPlugin.init(); | |
} | |
}; | |
// provide an absolute URL to the extra script | |
// use a protocol-relateive URL if possible so your plugin works in | |
// HTTPS pages | |
script.src = '//example.com/customplugin.js'; | |
stylesheet.rel = "stylesheet"; | |
// provide an absolute URL to any stylesheet resources you need | |
stylesheet.href = '//example.com/customplugin.css'; | |
document.body.appendChild(script); | |
document.body.appendChild(stylesheet); | |
// initialize the plugin once all the external resources have finished loading | |
player.customPlugin.init = function() { | |
// ... | |
}; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment