3

I'm using the SWT browser widget to embed a Mozilla browser inside a Java process, and I'd like to modify the browser's about:config preferences programmatically from Java, at run time. Is this possible to do? And if so, how?

1 Answers1

2

You can probably use JavaXPCOM for that. Something like this should work:

import org.mozilla.xpcom.Mozilla;
import org.mozilla.interfaces.nsIServiceManager;
import org.mozilla.interfaces.nsIPrefBranch;

...

Mozilla mozilla = Mozilla.getInstance();
nsIServiceManager serviceManager = mozilla.getServiceManager();
nsIPrefBranch prefs = (nsIPrefBranch)serviceManager
    .getServiceByContractID("@mozilla.org/preferences-service;1",
        nsIPrefBranch.NS_IPREFBRANCH_IID);
prefs.setBoolPref("javascript.enabled", false);

If you cannot access JavaXPCOM then this likely isn't doable.

Wladimir Palant
  • 56,865
  • 12
  • 98
  • 126
  • As far as I know, the only XPCOM object that SWT will allow you to access directly is the `nsIWebBrowser` for your `Browser` widget. Is there a way to implement this using only an `nsIWebBrowser` as a base? –  Apr 05 '12 at 13:55
  • Did you try it? If SWT exposes `nsIWebBrowser` to you then it means that JavaXPCOM is present - and you can access a global service directly, you don't need to go through `nsIWebBrowser` for that. The preferences aren't a property of this particular browser instance. – Wladimir Palant Apr 05 '12 at 16:46