[webkit-dev] safari extension script loading within injected file
Lou Zell
lzell11 at gmail.com
Tue Feb 1 13:07:06 PST 2011
Hello list,
I am seeing some behavior that I cannot explain with a safari 5 extension.
First, let me show a bit of code that works as expected outside the
extension environment. This loads jQuery from javascript:
(function(){
var node = document.createElement('script');
node.onload = function(){
if(typeof(jQuery) == 'undefined'){
console.log("fail: jQuery is undefined");
}
else{
console.log("success: jQuery is defined");
}
};
node.async = "async";
node.type = "text/javascript";
node.src = "http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"
document.getElementsByTagName('head')[0].appendChild(node);
})();
If I open the Safari Console and load a page with this bit of code, I see
"success: jQuery is defined". So far so good. Next, I'll copy that exact
chunk of code into a file that my safari extension injects, injected.js:
function messageHandler(event){
if(event.name === "show"){
(function(){
var node = document.createElement('script');
node.onload = function(){
if(typeof(jQuery) == 'undefined'){
console.log("fail: jQuery is undefined");
}
else{
console.log("success: jQuery is defined");
}
};
node.async = "async";
node.type = "text/javascript";
node.src = "
http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js"
document.getElementsByTagName('head')[0].appendChild(node);
})();
}
}
safari.self.addEventListener("message",messageHandler,false);
A tiny bit about my extension: when the user clicks a toolbar button it
sends the 'show' message to the injected script. When that happens, I see
"fail: jQuery is undefined" logged to the console. If I put a breakpoint
inside the onload callback, I can verify that this is the case. However,
once I hit continue in the debugger, jQuery is defined! It is as if jQuery
is not defined in the scope of my extension when I load it with this method.
Does that make sense?
In summary, what would cause the loading method used above to work outside
the safari extension environment, but not inside it?
Thanks,
Lou Zell
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.webkit.org/pipermail/webkit-dev/attachments/20110201/2cd4a329/attachment.html>
More information about the webkit-dev
mailing list