Created
March 22, 2011 08:50
-
-
Save edvakf/880949 to your computer and use it in GitHub Desktop.
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
// ==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