[jetpack] Running a content script in context of currently active tab

37 views
Skip to first unread message

ehsanul

unread,
Dec 24, 2010, 10:00:26 PM12/24/10
to mozilla-labs-jetpack, me...@njit.edu
I'm having tremendous difficulty with this seemingly super simple
task, and I'm not making much progress, even as I go through the
source of the packages I'm trying to use.

I just want to be able to run some JS code in the context of the web
page the user is focused on, on demand. So I have a widget set up,
which is supposed to make this happen when clicked. The widget is very
easy to set up, but I can't see how I can get access to the currently
active web page, through any sort of worker. I'd either like to send a
message to an existing worker in the context of the currently active
web page, or be able to create a new worker within it. Either one
would be fine.

The page-mod module doesn't really work since that relies on a URL
pattern. I want my code to run in the context of whatever page the
user happens to be on, and only run when the widget is clicked.

So I tried using the Worker module directly within my widget's onclick
event. Worker expects a window object passed in. So I tried using the
windows module, passing in windows.activeWindow to the Worker.
However, it seems like that's the active browser window, containing a
list of tabs, so that just doesn't work.

So I looked at tabs.activeTab, but that also doesn't let you get to
the actual "window" instance within the tab it seems from the docs. I
can't figure out any other way to get to the window object Worker
expects using the packages included in the Addon SDK (version 1.0b1).

The only alternative seems to be something XUL-based (I'm not really
familiar with all that, just a guess). Would I be better off just
figuring out how the XUL stuff works instead of wrestling with
JetPack? I mean, I can think of some hacky ways to figure out the
active web page, like a page-mod script that runs on every web page
and reports back when user-initiated events occur. But I expect there
to be a way built into the SDK for this.

KWierso

unread,
Dec 24, 2010, 11:08:19 PM12/24/10
to mozilla-labs-jetpack
You should be able to add a pagemod that can apply to every single
page with something like this:

include: "*"


In your contentScript (a separate file in the data/ directory works
best if it's anything complicated or long), set up a listener for
receiving messages.

Then in your widget, you register the onclick event/attribute to do a
postMessage to the contentScript, which then initiates the content
script's functions on that page.

Without knowing what you're trying to accomplish specifically, I can't
really help you beyond pointing to the documentation:
https://jetpack.mozillalabs.com/sdk/1.0b1/docs/#module/addon-kit/page-mod

ehsanul

unread,
Dec 25, 2010, 3:28:33 AM12/25/10
to mozilla-labs-jetpack
(My last message didn't get through it seems, so sending another.)

Thanks for the reply.

I got as far as you had explained before posting here, and have gone
through the documentation pretty thoroughly I think. The issue with
the approach you outlined is knowing which worker to postMessage() to,
since I'd like to only invoke the worker associated with the
*currently active* web page.

You see, with page-mod being used on every page with include: "*", I'd
keep an array of all the workers for every page open simultaneously.
So I'd do something like workers[i].postMessage('my message').
However, I don't know which of the workers in the array is associated
with the *active* window. So the key problem is finding the worker of
the *active page*.

Alternatively, just having a way to create a new worker specifically
for the currently active page also works. I can't find any way to do
either of these. There's window.activeWindow, but that's the browser
window, and tags.activeTab, but that has a pretty limited API
according to the docs, no way I see to create a worker on the active
tab's web page.
> really help you beyond pointing to the documentation:https://jetpack.mozillalabs.com/sdk/1.0b1/docs/#module/addon-kit/page...

ehsanul

unread,
Dec 25, 2010, 3:17:51 AM12/25/10
to mozilla-labs-jetpack
Thanks for the reply.

I've gone through the docs as thoroughly as I can, and then resorted
to reading the source for the modules. I realize I can insert a
content script into every page with page-mod and including "*". I got
as far as your explanation suggests I can do. However, when the widget
is clicked, I can't actually figure out which worker is attached to
the currently active web page.

You see, page-mod will give me a worker for every single tabbed opened
to some URL, so I'd keep an array of all the workers naturally.
However, I need to run worker[i].postMessage('my message'), where
worker[i] is the worker which is attached to the active tab/web page
that is currently active. This is my problem, I found no way to find
out which worker is associated with the active tab/web page, or
alternatively, to create a new worker associated with the currently
active tab/web page.

I hope my problem is clearer now. I need a way to find the worker
related to the active tab/web page, or to create a new worker
specifically for the active tab/web page. Either way will solve my
problem.


On Dec 24, 11:08 pm, KWierso <kwie...@gmail.com> wrote:
> really help you beyond pointing to the documentation:https://jetpack.mozillalabs.com/sdk/1.0b1/docs/#module/addon-kit/page...

arun

unread,
Dec 28, 2010, 8:59:59 AM12/28/10
to mozilla-labs-jetpack, kwi...@gmail.com
I am facing same problem.I am working on a highlighting add on.So i
include the content script in every page.Now user can select any text
and highlight it.Also if user opens any URL for which highlighting
already exists it is applied by default.

Now the problem is when user highlights some text then the content
script can easily communicate with the addon script but reverse
communication is impossible.
When i insert content script i don't even know the url of the page in
which the script is being inserted.There seems no way i can pair pages
and workers(by urls or unique tab identifiers).Now how do i find which
worker is for which page.I have read all documentation and i cant find
any way to do this.

Does page mod work that way or am I missing something.Do let me know
if there are any workarounds.

arun

unread,
Dec 29, 2010, 4:07:52 AM12/29/10
to mozilla-labs-jetpack
Any updates??

ehsanul

unread,
Dec 29, 2010, 10:24:37 AM12/29/10
to mozilla-labs-jetpack
Arun, the way I'm going to be working around this is by actually
tracking every user-initiated event on every web page, relaying that
back to the main addon script. Then, the addon script will keep track
of which worker most recently sent a message saying "active" or
something. Then I'll always have the most recently active web page's
worker stored in a variable.

It's a really hacky solution, but I expect it to work. I'll be working
on it later today. Why don't you give it a try yourself?

But yeah, I really expect there to be a built-in way to do this, and
it looks like there's none in Jetpack.


On Dec 29, 4:07 am, arun <arunteva...@gmail.com> wrote:
> Any updates??

Irakli Gozalishvili

unread,
Dec 30, 2010, 5:45:26 AM12/30/10
to mozilla-la...@googlegroups.com, me...@njit.edu
Hi ehsanul,

There is no build-in way to create a worker / mod for an active page / tab even though it should not be to hard to add such a functionality. I also can see that this would be a valuable addition to the existing APIs, but at the moment we're pretty busy integrating out of the process architecture.

I would recommend 1. Creating a bug with a request of desired API. 2. If you'll have time to work on it pull request will definitely help in getting this feature inn faster.

Also if you'll decide go that way you can cc me in a bug & I'll try to help you by directing you in right path.

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.


Irakli Gozalishvili

unread,
Dec 30, 2010, 6:17:14 AM12/30/10
to mozilla-la...@googlegroups.com
Your solution is indeed pretty hacky. I've gisted a snippet for a simpler (but still) a hack for you:

https://gist.github.com/759683

Alexandre poirot

unread,
Dec 30, 2010, 6:54:36 AM12/30/10
to mozilla-la...@googlegroups.com
Same workflow than yesterday.

Feature request on bugzilla, with a patch: https://bugzilla.mozilla.org/show_bug.cgi?id=622077

Example of use:
const tabs = require("tabs");

let lastWorker;
tabs.on('activate', function(tab) {
 
  lastWorker = tab.createWorker({
    window: tab.window,
    contentScript: 'document.body.style.border="10px solid red";',
    contentScriptWhen: 'ready'
  });
});

And again, comments are welcome!


2010/12/30 Irakli Gozalishvili <rfo...@gmail.com>

Irakli Gozalishvili

unread,
Dec 30, 2010, 6:59:58 AM12/30/10
to mozilla-la...@googlegroups.com
That's great Alex!

I think you should assign that review to someone or bring it up during weekly meeting!

Cheers!

ehsanul

unread,
Dec 31, 2010, 10:22:16 AM12/31/10
to mozilla-labs-jetpack
Thanks for the snippet Irakli, but I think you agree that Alexandre's
way is the right way to go.

Thanks Alex! I applied the patch and it seems to be working perfectly.
Wish I could have figured that out. Any resources available that would
help me in that regard, besides reading the source of other modules?

Also, patching modules should carry over when publishing the addon,
right? WIthout conflicting with other addons using other versions of
the same modules?


On Dec 30, 6:59 am, Irakli Gozalishvili <rfo...@gmail.com> wrote:
> That's great Alex!
>
> I think you should assign that review to someone or bring it up during
> weekly meeting!
>
> Cheers!
> --
> Irakli Gozalishvili
> Web:http://www.jeditoolkit.com/
> Address: 29 Rue Saint-Georges, 75009 Paris, France <http://goo.gl/maps/3CHu>
>
> On Thu, Dec 30, 2010 at 12:54, Alexandre poirot <poirot.a...@gmail.com>wrote:
>
>
>
>
>
>
>
> > Same workflow than yesterday.
>
> > Feature request on bugzilla, with a patch:
> >https://bugzilla.mozilla.org/show_bug.cgi?id=622077
>
> > Example of use:
> > const tabs = require("tabs");
>
> > let lastWorker;
> > tabs.on('activate', function(tab) {
>
> >   lastWorker = tab.createWorker({
> >     window: tab.window,
> >     contentScript: 'document.body.style.border="10px solid red";',
> >     contentScriptWhen: 'ready'
> >   });
> > });
>
> > And again, comments are welcome!
>
> > 2010/12/30 Irakli Gozalishvili <rfo...@gmail.com>
>
> >> Your solution is indeed pretty hacky. I've gisted a snippet for a simpler
> >> (but still) a hack for you:
>
> >>https://gist.github.com/759683
>
> >> Regards
> >> --
> >> Irakli Gozalishvili
> >> Web:http://www.jeditoolkit.com/
> >> Address: 29 Rue Saint-Georges, 75009 Paris, France<http://goo.gl/maps/3CHu>
>
> >> On Wed, Dec 29, 2010 at 16:24, ehsanul <ehsanul...@gmail.com> wrote:
>
> >>> Arun, the way I'm going to be working around this is by actually
> >>> tracking every user-initiated event on every web page, relaying that
> >>> back to the main addon script. Then, the addon script will keep track
> >>> of which worker most recently sent a message saying "active" or
> >>> something. Then I'll always have the most recently active web page's
> >>> worker stored in a variable.
>
> >>> It's a really hacky solution, but I expect it to work. I'll be working
> >>> on it later today. Why don't you give it a try yourself?
>
> >>> But yeah, I really expect there to be a built-in way to do this, and
> >>> it looks like there's none in Jetpack.
>
> >>> On Dec 29, 4:07 am, arun <arunteva...@gmail.com> wrote:
> >>> > Any updates??
>
> >>> --
> >>> 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<mozilla-labs-jetpack%2Bun subs...@googlegroups.com>
> >>> .
> >>> For more options, visit this group at
> >>>http://groups.google.com/group/mozilla-labs-jetpack?hl=en.
>
> >>  --
> >> 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<mozilla-labs-jetpack%2Bun subs...@googlegroups.com>
> >> .
> >> For more options, visit this group at
> >>http://groups.google.com/group/mozilla-labs-jetpack?hl=en.
>
> >  --
> > 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<mozilla-labs-jetpack%2Bun subs...@googlegroups.com>
> > .

Jeremy Pesner

unread,
Jan 6, 2011, 5:09:01 PM1/6/11
to mozilla-la...@googlegroups.com
How do you patch the module?

ehsanul

unread,
Jan 7, 2011, 1:03:24 PM1/7/11
to mozilla-labs-jetpack
> How do you patch the module?

You need git. Save the text of the patch into a separate file, maybe
call it 'active_tab.patch'. You put this file in your addon sdk
directory. Then you can simply run the following command in the addon
sdk directory:

git apply active_tab.patch

That should do it. You can check if it worked by looking at the
appropriate files, as mentioned in the patch, to see if the code
additions are there.

If you can't install git for whatever reason, you could just copy/
paste the patch code into the appropriate files...

Hernan Rodriguez Colmeiro

unread,
Jan 18, 2011, 2:09:20 PM1/18/11
to mozilla-la...@googlegroups.com
On Fri, Dec 31, 2010 at 12:22, ehsanul <ehsan...@gmail.com> wrote:
> Also, patching modules should carry over when publishing the addon,
> right? WIthout conflicting with other addons using other versions of
> the same modules?
>

Each xpi created with jetpack has it's own jetpack core, akin to
static linking. You can even install addons created with different
jetpack versions :)

Hernán

Reply all
Reply to author
Forward
0 new messages