laurentj / slimerjs

A scriptable browser like PhantomJS, based on Firefox
http://slimerjs.org
Other
3k stars 259 forks source link

A way to set Mozilla preferences #385

Open MarkR42 opened 9 years ago

MarkR42 commented 9 years ago

As a potential enhancement, it would be really useful to set Mozilla preferences programmatically.

marco-c commented 8 years ago

There is a way to do this, by using the chrome module: https://github.com/mozilla/pluotsorbet/blob/9afd89c0079833245de4447fd9dd13366bbc69dd/tests/automation.js#L8

MarkR42 commented 8 years ago

Yes, I see that, I tried using chrome module to set settings, it does work.

I suppose we should update the documentation to give this example.

Mark

On 2 October 2015 at 20:41, Marco notifications@github.com wrote:

There is a way to do this, by using the chrome module:

https://github.com/mozilla/pluotsorbet/blob/9afd89c0079833245de4447fd9dd13366bbc69dd/tests/automation.js#L8

— Reply to this email directly or view it on GitHub https://github.com/laurentj/slimerjs/issues/385#issuecomment-145136761.

danikp commented 8 years ago

@MarkR42 can you post an example code of setting preferences using chrome module ?

MarkR42 commented 8 years ago

Dmitry, Hi,

This works for me, but there might be a nicer way:

var {Cc, Ci} = require("chrome");
 var prefs = Cc["@mozilla.org/preferences-service;1"]
                .getService(Ci.nsIPrefService);
var branch = prefs.getBranch("privacy.donottrackheader.");
branch.setBoolPref("enabled", true);

This example sets the "Do not track" flag, which can be tested easily because web servers receive it as a HTTP header "DNT".

Unfortunately very little of the add-on SDK is shipped with SlimerJS, so it's not available (Firefox's own versions, cannot be require()d).

I originally tried: var prefs_service=require("sdk/preferences/service"); prefs_service.set(...

That does NOT work in SlimerJS.

However, because the Firefox addon-SDK is mostly wrappers for XPCOM stuff (as in the example above), it can be used directly.

Mark