MozillaZine

Accessing WebExtensions world from chrome

Talk about add-ons and extension development.
pintassilgo
 
Posts: 186
Joined: August 30th, 2013, 3:50 pm

Post Posted January 15th, 2020, 8:16 pm

For instance, how to run
Code: Select all
browser.runtime.sendMessage()

and
Code: Select all
browser.tabs.create(

from Browser Console?

The thing is: how to access browser object?

morat
 
Posts: 3791
Joined: February 3rd, 2009, 6:29 pm

Post Posted January 16th, 2020, 12:12 pm

A few years ago, I tried to create the browser object in the browser console. I couldn't figure out how to run code like "class extends ExtensionAPI" in the browser console.

browser.runtime
http://developer.mozilla.org/docs/Mozilla/Add-ons/WebExtensions/API/runtime
http://searchfox.org/mozilla-release/source/toolkit/components/extensions/parent/ext-runtime.js
http://searchfox.org/mozilla-release/source/toolkit/components/extensions/child/ext-runtime.js

browser.tabs
http://developer.mozilla.org/docs/Mozilla/Add-ons/WebExtensions/API/tabs
http://searchfox.org/mozilla-release/source/browser/components/extensions/parent/ext-tabs.js
http://searchfox.org/mozilla-release/source/browser/components/extensions/child/ext-tabs.js

Try using the console in the about:debugging page.

* open about:debugging
* left click This Firefox on the left side
* left click inspect button next to an extension
* copy and paste code into the console
* press enter to run

Code: Select all
(function () {
  browser.tabs.create({
    "url": "http://www.example.com/",
  });
})();

Debugging
http://developer.mozilla.org/Add-ons/WebExtensions/Debugging

pintassilgo
 
Posts: 186
Joined: August 30th, 2013, 3:50 pm

Post Posted June 5th, 2020, 9:02 pm

- Open Browser Toolbox (Ctrl+Alt+Shift+I);
- On upper right, click and select extensions/content/dummy

Each browser in there is one WebExtension. So, for example:

Code: Select all
document.getElementsByTagName('browser')[0].messageManager.loadFrameScript('data:application/javascript;charset=UTF-8,' + encodeURIComponent('(' + (function () {
  content.wrappedJSObject.browser.tabs.create({url: 'https://github.com/xiaoxiaoflood/firefox-scripts'})
}).toString() + ')();'), false)

morat
 
Posts: 3791
Joined: February 3rd, 2009, 6:29 pm

Post Posted June 6th, 2020, 2:42 am

I got a code snippet working in the browser console.

Code: Select all
(function () {
  var enumerator = Services.ww.getWindowEnumerator();
  while (enumerator.hasMoreElements()) {
    var win = enumerator.getNext();
    if (win.document && win.document.documentURI == "chrome://extensions/content/dummy.xhtml") {
      win.document.querySelectorAll("browser").forEach(function (browser) {
        if (browser._contentPrincipal.addonPolicy &&
            browser._contentPrincipal.addonPolicy.extension.manifest.name == "uBlock Origin") {
          var script = "data:text/plain," + encodeURIComponent(`
            content.wrappedJSObject.browser.tabs.create({
              url: "http://www.mozillazine.org/",
            });
            var data = {};
            data.permissions = content.wrappedJSObject.browser.runtime.getManifest().permissions;
            data.browser = content.wrappedJSObject.browser;
            sendAsyncMessage("foobar", data);
          `);
          browser.messageManager.addMessageListener("foobar", function removeMe(message) {
            browser.messageManager.removeMessageListener("foobar", removeMe);
            console.log(message.data.permissions);
            console.log(message.data.browser);
          });
          browser.messageManager.loadFrameScript(script, false);
        }
      });
    }
  }
  return "Debugging uBlock Origin";
})();

pintassilgo
 
Posts: 186
Joined: August 30th, 2013, 3:50 pm

Post Posted June 6th, 2020, 11:54 am

Great. I'll observe 'chrome-document-global-created' in userChromeJS (autoconfig.js) as it runs earlier than extensions, so I can access WebExtensions from the very beggining.

Return to Extension Development


Who is online

Users browsing this forum: No registered users and 2 guests