Page Mods Question

15 views
Skip to first unread message

Cid Dennis

unread,
Sep 30, 2010, 4:34:56 PM9/30/10
to mozilla-la...@googlegroups.com
I started looking at the page-mod package and thought that it provided a separate "sandboxed" environment for scripts. However, after testing I find that the page can indeed access my javascript function.

Does page-mod not provide a separate javascript context?

Basically, what I have is a some javascript that alters the page. The javascript I am using to do this is not only my custom JS but includes jQuery and a number of jQuery plugins.

However, I do not want to remote page to have any interaction with my javascript and I also do not need/want access to the remote pages javascript.

Any suggestions.

Thanks

Irakli Gozalishvili

unread,
Oct 1, 2010, 7:51:49 AM10/1/10
to mozilla-la...@googlegroups.com
Hi Cid,

Page mod indeed uses separate js context and all your content scripts are loaded there. Content scripts are able to access JavaScript objects in the context of the page but not other way round. If you mean that you're able to call functions in the content script from the page itself then it's a bug. If you can provide simple example to reproduce that please do, that will help to fix a bug if it exists.

On my side I'll try to verify that everything works the way we expect it to work.
  
Regards
--
Irakli Gozalishvili
Web: http://www.jeditoolkit.com/
Address: 29 Rue Saint-Georges, 75009 Paris, France





--
You received this message because you are subscribed to the Google Groups "mozilla-labs-jetpack" group.
To post to this group, send email to mozilla-la...@googlegroups.com.
To unsubscribe from this group, send email to mozilla-labs-jet...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/mozilla-labs-jetpack?hl=en.


Cid Dennis

unread,
Oct 1, 2010, 11:42:32 AM10/1/10
to mozilla-la...@googlegroups.com
Thanks for the help.

Attached is an very simple example that adds a function called sayhello. Then I have the page that loades in the browser call the function.

On the note if I load the test2.html file with file open. No issue function is undefined. When I load it if my http://localhost/test2.html. It will call the function.


var pageMod = require("page-mod");
var my_extra_js = "window.sayhello = function() { alert('hello from insite bx')};";
pageMod.add(new pageMod.PageMod({
include: ["*"],
contentScriptWhen: 'ready',
contentScript: my_extra_js
}));


Example Script

<script>
function go()
{
try
{
alert("about to");
sayhello();
}
catch(aaa)
{
alert("error" + aaa.message);
}
}
setTimeout(go,4000);
</script>


fire_fox_bx 2.zip

Irakli Gozalishvili

unread,
Oct 1, 2010, 1:50:16 PM10/1/10
to mozilla-la...@googlegroups.com
Thanks for attachment!! I see what happens now. You're adding function to the window and it's get exposed to the window scope itself.


var my_extra_js =  "window.sayhello = function() { alert('hello from insite bx')};";

I'm not sure if that's something we should prevent, but we can. Please also note that if you don't wont to expose your function you can always rewrite it this way.

"function sayhello () { alert('hello from insite bx')};";

But you have a very good point here. Probably some frameworks use window object to expose their public API and there for they may pollute pages global scope.

I will talk with other jetpack devs and will come back to you.










--

Cid Dennis

unread,
Oct 1, 2010, 2:53:47 PM10/1/10
to mozilla-la...@googlegroups.com
Thanks.  

I think jquery is an example that adds things to the window object.   I know google chrome allows this but still protects me from the main pages JS environment so I can add things to window with out it being accessible from the pages JS environment.
 

Thanks again.

dankantor

unread,
Oct 1, 2010, 6:22:07 PM10/1/10
to mozilla-labs-jetpack
I'd vote for window to be separated. Too may scripts use it.

Slightly related question - if I insert 2 contentScriptURL's, should
they be able to see each other's variables? It looks like they cannot
see each other right now. Chrome and Safari both allow you to insert
multiple scripts that act as one. This helps greatly in separating
code to make it cleaner.

On Oct 1, 2:53 pm, Cid Dennis <cid.den...@gmail.com> wrote:
> Thanks.  
>
> I think jquery is an example that adds things to the window object.   I know google chrome allows this but still protects me from the main pages JS environment so I can add things to window with out it being accessible from the pages JS environment.
>
> Thanks again.
>
> On Oct 1, 2010, at 11:50 AM, Irakli Gozalishvili wrote:
>
>
>
>
>
>
>
> > Thanks for attachment!! I see what happens now. You're adding function to the window and it's get exposed to the window scope itself.
>
> > var my_extra_js =  "window.sayhello = function() { alert('hello from insite bx')};";
>
> > I'm not sure if that's something we should prevent, but we can. Please also note that if you don't wont to expose your function you can always rewrite it this way.
>
> > "function sayhello () { alert('hello from insite bx')};";
>
> > But you have a very good point here. Probably some frameworks use window object to expose their public API and there for they may pollute pages global scope.
>
> > I will talk with other jetpack devs and will come back to you.
>
> > Regards
> > --
> > Irakli Gozalishvili
> > Web:http://www.jeditoolkit.com/
> > Address: 29 Rue Saint-Georges, 75009 Paris, France
>
> > On Fri, Oct 1, 2010 at 17:42, Cid Dennis <cid.den...@gmail.com> wrote:
> > Thanks for the help.
>
> > Attached is an very simple example that adds a function called sayhello.  Then I have the page that loades in the browser call the function.
>
> > On the note if I load the test2.html file with file open.  No issue function is undefined. When I load it if myhttp://localhost/test2.html.  It will call the function.
>
> > var pageMod = require("page-mod");
> > var my_extra_js =  "window.sayhello = function() { alert('hello from insite bx')};";
> > pageMod.add(new pageMod.PageMod({
> >  include: ["*"],
> >  contentScriptWhen: 'ready',
> >  contentScript: my_extra_js
> > }));
>
> > Example Script
>
> > <script>
> > function go()
> > {
> >        try
> >        {
> >                alert("about to");
> >                sayhello();
> >        }
> >        catch(aaa)
> >        {
> >                alert("error" + aaa.message);
> >        }
> > }
> > setTimeout(go,4000);
> > </script>
>
> > --
> > You received this message because you are subscribed to the Google Groups "mozilla-labs-jetpack" group.
> > To post to this group, send email to mozilla-la...@googlegroups.com.
> > To unsubscribe from this group, send email to mozilla-labs-jet...@googlegroups.com.
> > For more options, visit this group athttp://groups.google.com/group/mozilla-labs-jetpack?hl=en.

dankantor

unread,
Oct 1, 2010, 7:41:45 PM10/1/10
to mozilla-labs-jetpack
Is postMessage in there yet? I can't seem to talk from my jetpack to
the contentScript or viceversa. I tried this -

var pageMod = require("page-mod");
pageMod.add(new pageMod.PageMod({
include: ["http://*"],
contentScriptWhen: 'ready',
contentScriptURL: self.data.url('big.js'),
onOpen: pageModOnOpen
}));

function pageModOnOpen(worker, mod){
console.log('w', worker, mod)
worker.on('error', function(error) {
console.error(error.message);
});
worker.on('message', function(data) {
console.log(data);
});

worker.postMessage('Worker, what is your location ?');
}


pageModOnOpen never gets called. Also, in the contentScript I do this
-

postMessage('hey');

and it throws this error -
Not enough arguments [nsIDOMWindowInternal.postMessage] undefined 0
undefined

I know that postMessage is supposed to take the domain you are sending
to as an argument but I don't know what it would be in this case.

Any help you can provide would be greatly appreciated. Thanks

Irakli Gozalishvili

unread,
Oct 1, 2010, 7:59:39 PM10/1/10
to mozilla-la...@googlegroups.com
Hi Dan,

Take another look at the example form the docs and make sure your have 0.8:
https://jetpack.mozillalabs.com/sdk/0.8/docs/#module/jetpack-core/page-mod

You should use `onAttach` instead of `onOpen` and error with `postMessage` message makes me think your jetpack-core is out of date, since it seems that windows postMessage is called instead which should not happen.

Regarding several content scripts they do share same scope and there for all the variables defined in that scope. To see a real working examples of page-mod take a look at page-mod tests.

Irakli Gozalishvili

unread,
Oct 1, 2010, 8:27:26 PM10/1/10
to mozilla-la...@googlegroups.com
I have made bug report for that so you can track progress here:
https://bugzilla.mozilla.org/show_bug.cgi?id=601295
Reply all
Reply to author
Forward
0 new messages