Page 1 of 1

Need help writing WebExtensions

Posted: Sat Dec 23, 2017 10:55 pm
by barbaz
Figured I'd ask here first because NoScript 10 will also need the answer to my question.

I'm trying to port my user-agent switcher extension to WebExtensions, and preferably in a Chromium-compatible way. But I'm having trouble figuring out how to alter navigator.userAgent.

Basically I need to have a content script do the Webext equivalent of these legacy codes -

Code: Select all

  getSandbox: function(contentWindow) {
    // contentWindow might be an Xray, unwrap if it is
    let win = contentWindow.wrappedJSObject || contentWindow;
    let sandbox = new Cu.Sandbox(win, {sandboxPrototype: win, wantComponents: false, wantXHRConstructor: false, wantXrays: false});
    return sandbox;
  },

Code: Select all

// called from a content-document-global-created observer
let contentSandbox = utils.getSandbox(contentWindow);
Cu.evalInSandbox("(function(){/* code to set custom navigator.userAgent */ })()", contentSandbox, "1.8");
But I'm not finding the Webext equivalent of the above codes. I'm sure there is one, because running scripts in the page context is such a basic and obvious thing to do. And indeed, it is required by NoScript, for surrogates.

How to do this in Webext?

Re: Need help writing WebExtensions

Posted: Wed Dec 27, 2017 4:59 am
by barbaz
Never mind. Turns out WebExtensions limitations reduce user-agent switchers to Swiss cheese, i.e. full of holes, leaking the real UA everywhere. So I am forced to make this addon a webext-hybrid anyway.

Unbelievable. Image

Re: Need help writing WebExtensions

Posted: Wed Dec 27, 2017 8:14 pm
by GµårÐïåñ
Their API is not mature yet, they have A LOT of lacking API in the rush to get it out there, so it will be quite a while before they become reasonably usable for everything. This was the same issue Chrome faced in the early days and now they have it better covered and Mozilla decided, oh yeah we are going to blow up our ecosystem and become Chrome's infant stage instead, idiotic.

Re: Need help writing WebExtensions

Posted: Thu Jan 25, 2018 2:17 am
by barbaz
I found part of the answer to my original question. It is not technically possible until Firefox 59, when this API will debut -
https://developer.mozilla.org/docs/Mozi ... s/register

bugzilla link - https://bugzilla.mozilla.org/show_bug.cgi?id=1332273

Re: Need help writing WebExtensions

Posted: Fri Jan 26, 2018 1:28 am
by barbaz
barbaz wrote:Turns out WebExtensions limitations reduce user-agent switchers to Swiss cheese, i.e. full of holes, leaking the real UA everywhere. So I am forced to make this addon a webext-hybrid anyway.
Update: At the time I wrote this, I figured this limitation was by design. But the folks in https://github.com/gorhill/uMatrix/issues/925 are saying it is a browser bug. So there maybe still some hope?

Would be great if it does get fixed! :)

Re: Need help writing WebExtensions

Posted: Fri Jan 26, 2018 3:55 am
by GµårÐïåñ
Just be careful, browser gets blamed for a lot that has nothing to do with anything. Technically any API limitation is a "browser bug", so it becomes a bit of an overreaching net and ultimately meaningless.