Skip to content

Instantly share code, notes, and snippets.

@edvakf
Created March 22, 2011 08:50
Show Gist options
  • Save edvakf/880949 to your computer and use it in GitHub Desktop.
Save edvakf/880949 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name defer/async for Opera
// @namespace http://d.hatena.ne.jp/edvakf/
// @include *
// @license Public Domain
// ==/UserScript==
(function() {
var loaded = false;
window.addEventListener('load', function() {
loaded = true;
}, false);
// support defer
window.opera.addEventListener('BeforeExternalScript', function(e) {
var script = e.element;
if (!script.hasAttribute('defer') ||
script.hasAttribute('async') ||
script.hasAttribute('data-defer') ||
loaded) {
return;
}
var load_script = function() {
var script2 = script.cloneNode(true);
script2.setAttribute('data-defer', 'yes');
script.parentNode.insertBefore(script2, script);
script.parentNode.removeChild(script);
}
window.addEventListener('load', load_script, false);
return e.preventDefault();
}, false);
// support async
window.opera.addEventListener('BeforeExternalScript', function(e) {
var script = e.element;
console.log(script.src);
if (!script.hasAttribute('async') ||
script.hasAttribute('data-async')) {
return;
}
var load_script = function() {
var script2 = script.cloneNode(true);
script2.setAttribute('data-async', 'yes');
script.parentNode.insertBefore(script2, script);
script.parentNode.removeChild(script);
}
setTimeout(load_script, 10);
return e.preventDefault();
}, false);
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment