[webkit-dev] safari extension script loading within injected file
Nick Guenther
nick.guenther at dossierview.com
Tue Feb 1 17:44:35 PST 2011
Cluestick (or n00b question): What's 'document' within the extension scope?
----- Original message -----
> 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
More information about the webkit-dev
mailing list