foxygestures icon indicating copy to clipboard operation
foxygestures copied to clipboard

User script and mozilla.cfg

Open morat523035 opened this issue 2 years ago • 3 comments

Does anyone know a way to execute a code snippet in the mozilla.cfg file using a mouse gesture?

I tried the console-api-log-event and cookie-changed observer topics.

  • add user script and gesture
  • create <installation directory>\defaults\pref\autoconfig.js file
  • create <installation directory>\mozilla.cfg file
  • restart application
  • perform mouse gesture to test

Installation directory http://kb.mozillazine.org/Installation_directory

/* Foxy Gestures UserScript */

(function () {

  // fails i.e. browser console does not show "mozilla.cfg Alpha"
  console.log("Foxy Gestures Alpha");

  // fails i.e. browser console does not show "mozilla.cfg Alpha"
  executeInBackground("(" + function () {
    console.log("Foxy Gestures Alpha");
  } + ")", []);

  // fails i.e. browser console does not show "mozilla.cfg Beta"
  // except when web developer tools storage tab is open
  var now = new Date();
  var expires = new Date(now.setTime(now.getTime() + 1000));
  document.cookie = "foxygestures=Foxy Gestures Beta; expires=" + expires.toUTCString();

}());
pref("general.config.sandbox_enabled", false);
pref("general.config.filename", "mozilla.cfg");
pref("general.config.obscure_value", 0);
// mozilla.cfg file needs to start with a comment line

Components.utils.import("resource://gre/modules/Services.jsm");

// execute console.log("Foxy Gestures Alpha"); in browser console shows "mozilla.cfg Alpha"
// execute console.log("Foxy Gestures Alpha"); in web console does not show "mozilla.cfg Alpha"

Services.obs.addObserver(function (aSubject, aTopic, aData) {
  var consoleMsg = aSubject.wrappedJSObject;
  if (consoleMsg.arguments && consoleMsg.arguments.length) {
    switch (consoleMsg.arguments[0]) {
      case "Foxy Gestures Alpha":
        Services.console.logStringMessage("mozilla.cfg Alpha");
        break;
    }
  }
}, "console-api-log-event", false);

Services.obs.addObserver(function (aSubject, aTopic, aData) {
  if (aSubject.name == "foxygestures" && (aData == "added" || aData == "changed")) {
    switch (aSubject.value) {
      case "Foxy Gestures Beta":
        Services.console.logStringMessage("mozilla.cfg Beta");
        break;
    }
  }
}, "cookie-changed");

Deploying Firefox in an enterprise environment http://web.archive.org/web/20191006061004/https://developer.mozilla.org/en-US/docs/Mozilla/Firefox/Enterprise_deployment_before_60

Observer Notifications http://web.archive.org/web/20191006034558/https://developer.mozilla.org/en-US/docs/Mozilla/Tech/XPCOM/Observer_Notifications

morat523035 avatar May 02 '22 14:05 morat523035

I'm not familiar with mozilla.cfg so I can't help with that. Three things worth mentioning are:

  1. executeInBackground internally calls Function.toString(), so you do not need to do "(" + ... + ")" when calling it.
  2. console.log from executeInBackground will output in the addon's background page context. You can find it by going to about:debugging > This Firefox, and inspecting Foxy Gestures.
  3. I think Observer Notifications are from XPCOM components and I believe that subsystem was removed from Firefox.

What is the goal in using mozilla.cfg? If the goal is to invoke a script too complicated to input into the user script directly, then I recommend writing your own web-extension addon and invoking it using runtime.sendMessage. Your extension would use runtime.onMessageExternal and the user script would invoke it with runtime.sendMessage. See https://github.com/stoically/temporary-containers/issues/50#issuecomment-364718479 for examples.

marklieberman avatar May 04 '22 19:05 marklieberman

executeInBackground internally calls Function.toString(), so you do not need to do "(" + ... + ")" when calling it.

I don't like using arrow functions.

/* Foxy Gestures UserScript 1 */

(function () {

  var url = data.element.linkHref;
  if (url) {
    executeInBackground(aUrl => {
      getActiveTab(aTab => browser.tabs.create({
        active: false,
        index: aTab.index + 1,
        url: aUrl,
      }));
    }, [url]);
  }

}());
/* Foxy Gestures UserScript 2 */

(function () {

  var url = data.element.linkHref;
  if (url) {
    executeInBackground("(" + function (aUrl) {
      getActiveTab(function (aTab) {
        browser.tabs.create({
          active: false,
          index: aTab.index + 1,
          url: aUrl,
        });
      });
    } + ")", [url]);
  }

}());
/* Foxy Gestures UserScript 3 */

(function () {

  var url = data.element.linkHref;
  if (url) {
    executeInBackground(function (aUrl) {
      getActiveTab(function (aTab) {
        browser.tabs.create({
          active: false,
          index: aTab.index + 1,
          url: aUrl,
        });
      });
    }, [url]);
  }

}());

The third user script gives me an error in the browser console.

  • Error: function statement requires a name

eval(fn) and eval(arrowFn) returns different value http://stackoverflow.com/questions/43805644

morat523035 avatar May 04 '22 22:05 morat523035

My goal is to go beyond the restrictive WebExtensions API.

I got something working with a PlacesUtils listener.

/* Foxy Gestures UserScript */

(function () {

  executeInBackground("(" + function () {
    var promise = browser.bookmarks.create({
      title: "Foxy Gestures Gamma",
      url: "http://addons.mozilla.org/firefox/addon/803317",
    });
    promise.then(function (aResult) {
      browser.bookmarks.remove(aResult.id);
    });
  } + ")", []);

}());
pref("general.config.sandbox_enabled", false);
pref("general.config.filename", "mozilla.cfg");
pref("general.config.obscure_value", 0);
// mozilla.cfg file needs to start with a comment line

Components.utils.import("resource://gre/modules/Services.jsm");

Services.obs.addObserver(function (aSubject, aTopic, aData) {
  var chromeWindow = aSubject;
  chromeWindow.setTimeout(function () {
    try {
      if (chromeWindow.foxyGesturesMod) return;
      chromeWindow.foxyGesturesMod = true;
      var types = ["bookmark-added"];
      chromeWindow.PlacesUtils.observers.addListener(types, function (aEvents) {
        for (var event of aEvents) {
          if (event.type == types[0]) {
            switch (event.title) {
              case "Foxy Gestures Gamma":
                Services.console.logStringMessage("mozilla.cfg Gamma");
                break;
            }
          }
        }
      });
    } catch (e) {
      Components.utils.reportError(e); // [check] Show Content Messages
    }
  }, 10);
}, "browser-delayed-startup-finished", false);

Tips to click addon button and execute addon commands (see second section) http://forums.mozillazine.org/viewtopic.php?p=14922347#p14922347

morat523035 avatar May 04 '22 22:05 morat523035