Conditional pageMod Content Script Injection?

73 views
Skip to first unread message

Geek in Training

unread,
Jun 13, 2013, 10:28:03 PM6/13/13
to mozilla-la...@googlegroups.com
How do I insert a content script only when a certain condition is true? 
For example: if (changeSite === true) { /*let pageMod insert content script*/ }

I am not talking about being conditional like whether site matches  *.example.com/* or not, but rather if site matches *.example.com/* AND someVariable == true.

var pageMod = require("sdk/page-mod");
 
pageMod.PageMod({
  include: "*.mozilla.org",
  contentScript: 'window.alert("Page matches ruleset");'
});

Jeff Griffiths

unread,
Jun 14, 2013, 1:35:50 AM6/14/13
to mozilla-la...@googlegroups.com
You want instead tab.attach:

https://addons.mozilla.org/en-US/developers/docs/sdk/latest/modules/sdk/tabs.html#attach%28options%29

The one limitation that immediately comes to mind is that not all
page-mod features ( contentStyleFile for example ) are supported.

Jeff
> --
> You received this message because you are subscribed to the Google Groups
> "mozilla-labs-jetpack" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to mozilla-labs-jet...@googlegroups.com.
> To post to this group, send email to mozilla-la...@googlegroups.com.
> Visit this group at http://groups.google.com/group/mozilla-labs-jetpack.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Geek in Training

unread,
Jun 14, 2013, 9:07:46 PM6/14/13
to mozilla-la...@googlegroups.com
Okay, thanks! I that works well for my case, I do not need to add any content scripts.

Geek in Training

unread,
Jun 14, 2013, 10:16:06 PM6/14/13
to mozilla-la...@googlegroups.com
Oops, I meant to write I do not need to add any content styles.

 I just came across a problem, I need the script injected on 'start', but there is no start event, just 'ready'.

Is there any way around this?

Jeff Griffiths

unread,
Jun 15, 2013, 1:14:50 AM6/15/13
to mozilla-la...@googlegroups.com
That's a limitation of tabs.atach, you don't have any control over
when it attaches, it only does 'ready'. Can you gibe us a bit more of
an idea what you are trying to do?

Jeff

Geek in Training

unread,
Jun 15, 2013, 1:41:57 AM6/15/13
to mozilla-la...@googlegroups.com, je...@canuckistani.ca
I am making a basic userAgent switcher. I have got the request header part all done, but I also want to override the javascript property navigator.userAgent. (Some other ones too.)  To do that I insert a content script:

Object.defineProperties( unsafeWindow.navigator, {
    'userAgent': { get: function() { return self.options.userAgent; } }
});


But for this to do any good it has to be loaded before any other scripts do. As I mentioned before, I want this to only be inserted if a certain condition is true. In my case it is a simple on off switch for the user.
> email to mozilla-labs-jetpack+unsub...@googlegroups.com.

Geek in Training

unread,
Jun 15, 2013, 4:50:24 PM6/15/13
to mozilla-la...@googlegroups.com, je...@canuckistani.ca
Is there  a way to  do this with the low-level SDK APIs? working with tab.attach() would be perfect if there was a start event for tabs, like tabs.on( "start", function(){} )


On Friday, June 14, 2013 10:14:50 PM UTC-7, Jeff Griffiths wrote:
> email to mozilla-labs-jetpack+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages