Accessing WebExtensions world from chrome

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

Accessing WebExtensions world from chrome

Post by pintassilgo »

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: 6404
Joined: February 3rd, 2009, 6:29 pm

Re: Accessing WebExtensions world from chrome

Post by morat »

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/Mozil ... PI/runtime
http://searchfox.org/mozilla-release/so ... runtime.js
http://searchfox.org/mozilla-release/so ... runtime.js

browser.tabs
http://developer.mozilla.org/docs/Mozil ... s/API/tabs
http://searchfox.org/mozilla-release/so ... xt-tabs.js
http://searchfox.org/mozilla-release/so ... xt-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/We ... /Debugging
pintassilgo
Posts: 200
Joined: August 30th, 2013, 3:50 pm

Re: Accessing WebExtensions world from chrome

Post by pintassilgo »

- 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: 6404
Joined: February 3rd, 2009, 6:29 pm

Re: Accessing WebExtensions world from chrome

Post by morat »

I got a code snippet working in the browser console in Firefox.

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.gnu.org/licenses/gpl-3.0-standalone.html",
            });
            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";
})();
uBlock Origin (declares tabs permission in manifest.json)
http://addons.mozilla.org/firefox/addon/607454

...

I got a code snippet working in the error console in Thunderbird.

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) {
        var uri = Services.io.newURI(browser.contentWindow.location.href);
        if (WebExtensionPolicy.getByURI(uri) &&
            WebExtensionPolicy.getByURI(uri).extension.manifest.name == "XNote++") {
          var script = "data:text/plain," + encodeURIComponent(`
            content.wrappedJSObject.browser.tabs.create({
              url: "http://www.gnu.org/licenses/gpl-3.0-standalone.html",
            });
            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 XNote++";
})();
XNote++ (declares tabs permission in manifest.json)
http://addons.thunderbird.net/thunderbird/addon/64758
Last edited by morat on November 12th, 2020, 4:10 pm, edited 1 time in total.
pintassilgo
Posts: 200
Joined: August 30th, 2013, 3:50 pm

Re: Accessing WebExtensions world from chrome

Post by pintassilgo »

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.
Post Reply