Need help writing WebExtensions

General discussion about web technology.
Post Reply
barbaz
Senior Member
Posts: 10841
Joined: Sat Aug 03, 2013 5:45 pm

Need help writing WebExtensions

Post 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?
*Always* check the changelogs BEFORE updating that important software!
-
barbaz
Senior Member
Posts: 10841
Joined: Sat Aug 03, 2013 5:45 pm

Re: Need help writing WebExtensions

Post 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
*Always* check the changelogs BEFORE updating that important software!
-
User avatar
GµårÐïåñ
Lieutenant Colonel
Posts: 3365
Joined: Fri Mar 20, 2009 5:19 am
Location: PST - USA
Contact:

Re: Need help writing WebExtensions

Post 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.
~.:[ Lï£ê ï§ å Lêmðñ åñÐ Ì Wåñ† M¥ Mðñê¥ ßå¢k ]:.~
________________ .: [ Major Mike's ] :. ________________
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0
barbaz
Senior Member
Posts: 10841
Joined: Sat Aug 03, 2013 5:45 pm

Re: Need help writing WebExtensions

Post 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
*Always* check the changelogs BEFORE updating that important software!
-
barbaz
Senior Member
Posts: 10841
Joined: Sat Aug 03, 2013 5:45 pm

Re: Need help writing WebExtensions

Post 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! :)
*Always* check the changelogs BEFORE updating that important software!
-
User avatar
GµårÐïåñ
Lieutenant Colonel
Posts: 3365
Joined: Fri Mar 20, 2009 5:19 am
Location: PST - USA
Contact:

Re: Need help writing WebExtensions

Post 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.
~.:[ Lï£ê ï§ å Lêmðñ åñÐ Ì Wåñ† M¥ Mðñê¥ ßå¢k ]:.~
________________ .: [ Major Mike's ] :. ________________
Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0
Post Reply