Skip to content

Instantly share code, notes, and snippets.

@DavidBruant
Forked from fearphage/async_defer_script.js
Created December 24, 2012 11:49
Show Gist options
  • Save DavidBruant/4368965 to your computer and use it in GitHub Desktop.
Save DavidBruant/4368965 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name defer/async for Opera
// @namespace http://d.hatena.ne.jp/edvakf/
// @license Public Domain
// ==/UserScript==
(function(window, document, opera) {
function load_script(script, attr) {
return function() {
var script2 = script.cloneNode(true);
script2.removeAttribute(attr);
script.parentNode.insertBefore(script2, script);
script.parentNode.removeChild(script);
};
}
var asyncSupport = 'async' in document.createElement('script');
// support async and defer
opera.addEventListener('BeforeExternalScript', function(e) {
var script = e.element;
if (!asyncSupport && script.hasAttribute('async')) {
setTimeout(load_script(script, 'async'), 10);
return e.preventDefault();
}
if (script.hasAttribute('defer') && document.readyState != 'complete') {
window.addEventListener('load', load_script(script, 'defer'), false);
return e.preventDefault();
}
}, false);
})(this, this.document, this.opera);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment