Re: [webkit-dev] safari extension script loading within injected file

2011-02-02 Thread Adam Roben
This list is for discussion of the development of the WebKit project itself, 
not questions about how to use WebKit (or a browser that uses WebKit, such as 
Safari). See http://webkit.org/contact.html for details. You'll have better 
luck asking in a Safari-specific venue, such as 
https://devforums.apple.com/community/safari.

-Adam

___
webkit-dev mailing list
webkit-dev@lists.webkit.org
http://lists.webkit.org/mailman/listinfo.cgi/webkit-dev


Re: [webkit-dev] safari extension script loading within injected file

2011-02-02 Thread Lou Zell
On Tue, Feb 1, 2011 at 5:44 PM, Nick Guenther nick.guent...@dossierview.com
 wrote:


 Cluestick (or n00b question): What's 'document' within the extension scope?


It is not the same as 'document' of the loaded page.  I am no expert with
terminology here, so in case that makes zero sense: If I load up a web page
and use the debugger to print window.document, it is different from the
window.document that I get when I breakpoint on the injected javascript.

I made a simple test where I load a single javascript file from within the
injected js.  This single file creates a variable, window.MyGlobal.  What is
very interesting to me is that this object is accessible from the loaded
page!  Meaning if I create an html file that tries to access
window.MyGlobal, it can.  I don't think web page authors are supposed to be
able to access variables created by extensions.

I'm going to pose the question in the Apple list Adam suggested.  I'll post
the solution here for completeness when I get one.

Lou
___
webkit-dev mailing list
webkit-dev@lists.webkit.org
http://lists.webkit.org/mailman/listinfo.cgi/webkit-dev


Re: [webkit-dev] safari extension script loading within injected file

2011-02-02 Thread Lou Zell
On Wed, Feb 2, 2011 at 10:59 PM, Lou Zell lzel...@gmail.com wrote:

 I made a simple test where I load a single javascript file from within the
 injected js.  This single file creates a variable, window.MyGlobal.  What is
 very interesting to me is that this object is accessible from the loaded
 page!  Meaning if I create an html file that tries to access
 window.MyGlobal, it can.  I don't think web page authors are supposed to be
 able to access variables created by extensions.


After giving this more thought.  This behavior does seem perfectly
reasonable.  The javascript I'm using (in the first email) loads the second
script by appending a script tag to head.  So obviously whatever is in
that script is going to be accessible by the page's author.


 I'm going to pose the question in the Apple list Adam suggested.  I'll post
 the solution here for completeness when I get one.


So, it appears the answer to my question is: you're doing it wrong!  Any
javascript that my extension needs must be injected in.  I can specify
multiple files to inject, but I was trying to only load files as I needed
them--instead of injecting them all on each page load.  In the future I can
imagine Apple adding some form of conditional injection to extensions.

Best,
Lou
___
webkit-dev mailing list
webkit-dev@lists.webkit.org
http://lists.webkit.org/mailman/listinfo.cgi/webkit-dev


Re: [webkit-dev] safari extension script loading within injected file

2011-02-01 Thread Nick Guenther

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

___
webkit-dev mailing list
webkit-dev@lists.webkit.org
http://lists.webkit.org/mailman/listinfo.cgi/webkit-dev