The My Opera forums have been replaced with forums.opera.com. Please head over there to discuss Opera's products and features
See the new ForumsYou need to be logged in to post in the forums. If you do not have an account, please sign up first.
e.element.text empty during beforescript event for external scripts
For external scripts e.element.src contains the URL, but e.element.text is empty, so I cannot rewrite the script via the beforescript event.Afterscript has the same behaviour.
For example on www.google.ca:
e.type "BeforeScript"
e.element HTMLScriptElement
e.element.src "http://ssl.gstatic.com/gb/js/sem_93503e9315a5733d20e53739a0bde88e.js"
e.element.text ""
Searching the forums for "beforescript" or "afterscript" doesn't turn up anything (which makes me think the search isn't very good), but I did locate this post that assert e.element.text is supposed to contain the script body: http://my.opera.com/community/forums/findpost.pl?id=7433192
I'm new to javascript/web programming, old to C/device drivers. Maybe I'm looking in the wrong place for the script body? Do I have to select a DOM node somehow? (If so, why isn't the script body linked from the beforescript event?)
e.element.outerHTML just contains the script tag HTML.
Inline scripts have the opposite behaviour: e.element.src is empty and e.element.text contains the script text.
I'm running Opera 11.52 on Win 7.
...Stu, Dragonfly is awesome...
Update: search finds my post so I guess no one ever talked about beforescript and afterscript events.
Here's my test.js in my user script directory:
// ==UserScript==
// @include http*://*.google.*/*
// ==/UserScript==
(function() {
window.opera.addEventListener('BeforeScript', function (e) {
console.log("===== BeforeScript in ========================================");
console.log(e);
console.log(e.type);
if (e.element.src.length > 0) {
console.log(e.element.src);
if (e.element.src.match("sem_[^\.]*\.js")) {
console.log("=> sem_<blah>.js detected");
// attempt to rewrite here
e.element.text.replace(/try/, "/* trying... */ $1");
}
} else {
//console.log(e.element.outerHTML);
console.log("<inline>");
}
console.log(e.eventPhase);
console.log("===== BeforeScript out ========================================");
}, false);
window.opera.addEventListener('AfterScript', function (e) {
console.log("===== AfterScript in ========================================");
console.log(e);
console.log(e.type);
if (e.element.src.length > 0) {
console.log(e.element.src);
if (e.element.src.match("sem_[^\.]*\.js")) {
console.log("=> sem_<blah>.js detected");
// attempt to rewrite here
e.element.text.replace(/try/, "/* trying... */ $1");
}
} else {
//console.log(e.element.outerHTML);
console.log("<inline>");
}
console.log(e.eventPhase);
console.log("===== AfterScript out ========================================");
}, false);
})();
e.element.text = e.element.text.replace()
MyOpera Community Enhancements — by xErath & BtEO
MyOpera Community Optimizations — by fearphage
Scribit improved posting tools for the MyOpera Community — by xErath
Improve Weeklies Blog — by MisterE & fearphage
MyOpera Community Optimizations — by fearphage
Scribit improved posting tools for the MyOpera Community — by xErath
Improve Weeklies Blog — by MisterE & fearphage
Good spot, but that's not the cause of my problem. If I comment out those lines so I have
instead, it makes no difference. e.element.text (and .textContent) is empty during the BeforeScript event.
Rewriting cannot take place.
if (e.element.src.match("sem_[^\.]*\.js")) {
console.log("=> sem_<blah>.js detected");
// attempt to rewrite here
//e.element.text.replace(/try/, "/* trying... */ $1");
instead, it makes no difference. e.element.text (and .textContent) is empty during the BeforeScript event.
Rewriting cannot take place.
9. November 2011, 14:21:49 (edited)
Ah! It's a display bug in Dragonfly. The scripts tab inspection panel shows an empty field for e.element.text, but logging the empty field to the console outputs the script body, which is what I expect. So it's there and just not displayed.
Thanks all.
Filed as DFL-2654.
Thanks all.
Filed as DFL-2654.