Help in Firegesture script conversion to FoxyGesture....

Discussion about official Mozilla Firefox builds
Post Reply
ashleylai87
Posts: 218
Joined: December 13th, 2013, 7:40 pm

Help in Firegesture script conversion to FoxyGesture....

Post by ashleylai87 »

In the past, I used custom script for Firegesture :

Code: Select all

// if the gesture started from a link, open link in new tab
var linkURL = FireGestures.getLinkURL(FireGestures.sourceNode);
if (linkURL) {
gBrowser.loadOneTab(linkURL, null, null, null, true, false);
return;
}
// else if there is selected text
var sel = FireGestures.getSelectedText().replace(/^\s+|\s+$/g, '');
if (sel) {
// if it is an URL, open link in new tab
// else if it is an email, compose in new tab
// else, search for selection
if (sel.match(new RegExp("^(http(s?)://)?[a-z0-9]+(\\.[a-z0-9]+)+(/|$)"))) {
gBrowser.loadOneTab(sel, null, null, null, true, false);
} else if (sel.match(new RegExp("^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$", "i"))) {
gBrowser.loadOneTab("mailto:" + sel, null, null, null, true, false);
}
return;
}
// else open a new tab
document.getElementById("cmd_newNavigatorTab").doCommand();
The function of the script is....open link in new tab, otherwise open a new tab.


Since Firegesture doesnt work in FF57.....I found FoxyGesture to be suitable replacement....almost....
Since I am not exactly a programmer....can someone help me to create a custom script with same function above for FoxyGetures?
From FoxyGesture source code:

Code: Select all

// Open a link in a new tab.
  function commandOpenLinkInNewTab (data) {
    if (data.element.linkHref) {
      return getActiveTab(tab => {
        let tabOptions = {};
        tabOptions.url = data.element.linkHref;
        tabOptions.active = settings.insertTabIsActive;
        tabOptions.cookieStoreId = tab.cookieStoreId;
        if (settings.insertRelatedTab) {
          tabOptions.index = tab.index + 1;
        }
        return browser.tabs.create(tabOptions);
      });
    }
  }
  
  

Code: Select all

// Create a new empty tab.
    function commandNewTab (data) {
    return getActiveTab(tab => {
      // Firefox default is for new tabs to be active and inserted at the end of the tab bar.
      let tabOptions = {};
      tabOptions.url = notAboutNewTabUrl(settings.newTabUrl);
      tabOptions.active = true;
      tabOptions.cookieStoreId = tab.cookieStoreId;
      return browser.tabs.create(tabOptions);
    });
  }
How do I get "open link in new tab, otherwise open a new tab"?




edit: Just realize foxygesture's 'open link in new tab' opens new tab right next to current tab. Is there any method to make it open link in new tab at the end of the row of tabs?
Seem to be foxygesture issue as browser.tabs.insertRelatedAfterCurrent is set to false.
Nevermind, found the setting......


Dear mod, please don't move my thread to other forum section, I don't get reply from there T_T
morat
Posts: 6421
Joined: February 3rd, 2009, 6:29 pm

Re: Help in Firegesture script conversion to FoxyGesture....

Post by morat »

Anyone know how to write the following working code without arrow functions?

Code: Select all

/* Foxy Gestures UserScript */

(function () {

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

}());
I keep getting errors.

* Error: function statement requires a name
* Error: eval(...) is undefined

Arrow functions
http://developer.mozilla.org/en-US/docs ... _functions

WebExtensions browser.tabs.create
http://developer.mozilla.org/en-US/Add- ... abs/create

Foxy Gestures
http://addons.mozilla.org/firefox/addon/foxy-gestures/
http://github.com/marklieberman/foxygestures

Foxy Gestures User Scripts
http://github.com/marklieberman/foxyges ... er-Scripts

Edit:

Solution...

Code: Select all

/* Foxy Gestures UserScript */

(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]);
  }

}());
eval(fn) and eval(arrowFn) returns different value
http://stackoverflow.com/questions/43805644
morat
Posts: 6421
Joined: February 3rd, 2009, 6:29 pm

Re: Help in Firegesture script conversion to FoxyGesture....

Post by morat »

ashleylai87 wrote:How do I get "open link in new tab, otherwise open a new tab"?
Try this:

Code: Select all

/* Foxy Gestures UserScript */

(function () {

  var url = data.element.linkHref;
  if (url) {
    executeInBackground("(" + function (aUrl) {
      browser.tabs.create({active: false, url: aUrl});
    } + ")", [url]);
  } else {
    executeInBackground("(" + function (aUrl) {
      browser.tabs.create({active: false, url: aUrl});
    } + ")", ["about:blank"]);
  }

}());
ashleylai87
Posts: 218
Joined: December 13th, 2013, 7:40 pm

Re: Help in Firegesture script conversion to FoxyGesture....

Post by ashleylai87 »

morat wrote:
ashleylai87 wrote:How do I get "open link in new tab, otherwise open a new tab"?
Try this:

Code: Select all

/* Foxy Gestures UserScript */

(function () {

  var url = data.element.linkHref;
  if (url) {
    executeInBackground("(" + function (aUrl) {
      browser.tabs.create({active: false, url: aUrl});
    } + ")", [url]);
  } else {
    executeInBackground("(" + function (aUrl) {
      browser.tabs.create({active: false, url: aUrl});
    } + ")", ["about:blank"]);
  }

}());
Thank you very much. ^.^ It works!!
ashleylai87
Posts: 218
Joined: December 13th, 2013, 7:40 pm

Re: Help in Firegesture script conversion to FoxyGesture....

Post by ashleylai87 »

morat wrote:
ashleylai87 wrote:How do I get "open link in new tab, otherwise open a new tab"?
Try this:
I modified your script a bit it will switch to new tab immediately :
browser.tabs.create({active: true, url: aUrl});

However, there is slight issue with newly opened tab using your script.
The newly opened tab with your script doesn't automatically allow me to type at urlbar immediately. I need to click the urlbar to start typing.
The foxygesture built-in "New Tab" straight away allow me to type at urlbar after new tab is opened.
Any extra script for such purpose?
morat
Posts: 6421
Joined: February 3rd, 2009, 6:29 pm

Re: Help in Firegesture script conversion to FoxyGesture....

Post by morat »

The new tab page can be opened if no value for the url parameter is provided.

Try this:

Code: Select all

/* Foxy Gestures UserScript */

(function () {

  var url = data.element.linkHref;
  if (url) {
    executeInBackground("(" + function (aUrl) {
      browser.tabs.create({url: aUrl});
    } + ")", [url]);
  } else {
    executeInBackground("(" + function (aUrl) {
      browser.tabs.create({url: aUrl});
    } + ")", [""]);
  }

}());
WebExtensions browser.tabs.create parameters
http://developer.mozilla.org/en-US/Add- ... Parameters
ashleylai87
Posts: 218
Joined: December 13th, 2013, 7:40 pm

Re: Help in Firegesture script conversion to FoxyGesture....

Post by ashleylai87 »

morat wrote:The new tab page can be opened if no value for the url parameter is provided.

Try this:

Code: Select all

/* Foxy Gestures UserScript */

(function () {

  var url = data.element.linkHref;
  if (url) {
    executeInBackground("(" + function (aUrl) {
      browser.tabs.create({url: aUrl});
    } + ")", [url]);
  } else {
    executeInBackground("(" + function (aUrl) {
      browser.tabs.create({url: aUrl});
    } + ")", [""]);
  }

}());
WebExtensions browser.tabs.create parameters
http://developer.mozilla.org/en-US/Add- ... Parameters
Are you sure providing no value for the url parameter works?
Cause the moment I execute the gesture....it open the url below on new tab:
moz-extension://3458272f-xxxx-xxxx-xxxx-xxxxxxxxxx72/_generated_background_page.html

I just need the script to access address bar right after open in new tab.
After reading the documention, I think the stuff required is : Tab.url
How do I use it?
morat
Posts: 6421
Joined: February 3rd, 2009, 6:29 pm

Re: Help in Firegesture script conversion to FoxyGesture....

Post by morat »

Sorry. I got it working, then I changed the code and forgot to test again.

Try this:

Code: Select all

/* Foxy Gestures UserScript */

(function () {

  var url = data.element.linkHref;
  if (url) {
    executeInBackground("(" + function (aUrl) {
      browser.tabs.create({url: aUrl});
    } + ")", [url]);
  } else {
    executeInBackground("(" + function () {
      browser.tabs.create({});
    } + ")", []);
  }

}());
That works for me.

Foxy Gestures 1.0.8
Firefox 55.0.3
Windows 7 SP1 32-bit
ashleylai87
Posts: 218
Joined: December 13th, 2013, 7:40 pm

Re: Help in Firegesture script conversion to FoxyGesture....

Post by ashleylai87 »

morat wrote:Sorry. I got it working, then I changed the code and forgot to test again.

Try this:

Code: Select all

/* Foxy Gestures UserScript */

(function () {

  var url = data.element.linkHref;
  if (url) {
    executeInBackground("(" + function (aUrl) {
      browser.tabs.create({url: aUrl});
    } + ")", [url]);
  } else {
    executeInBackground("(" + function () {
      browser.tabs.create({});
    } + ")", []);
  }

}());
That works for me.

Foxy Gestures 1.0.8
Firefox 55.0.3
Windows 7 SP1 32-bit

:-D :-D
YES! It worked and perform exactly as expected now. Thank you very much. I really appreciate your help. ^.^b
morat
Posts: 6421
Joined: February 3rd, 2009, 6:29 pm

Re: Help in Firegesture script conversion to FoxyGesture....

Post by morat »

Here is how to search google with the selected text.

Code: Select all

/* Foxy Gestures UserScript */

(function () {

  var selection = getSelection();
  if (!selection.isCollapsed) {
    var text = selection.toString();
    text = text.trim();
    var query = escape(text).replace(/%20/g, "+");
    executeInBackground("(" + function (aUrl) {
      browser.tabs.create({url: aUrl});
    } + ")", ["http://www.google.com/search?q=" + query]);
  } else {
    executeInBackground("(" + function (aUrl) {
      browser.tabs.create({url: aUrl});
    } + ")", ["http://www.google.com/"]);
  }

}());
Here is how to inject css into a page.

Code: Select all

/* Foxy Gestures UserScript */

(function () {

  var element = document.getElementById("blackify");
  if (element) {
    element.parentNode.removeChild(element);
  } else {
    var style = document.createElement("style");
    style.type = "text/css";
    style.id = "blackify";
    style.textContent = [
      "* { background: #000000 !important; }",
      "* { color: #CCCCCC !important; }",
      "a { color: #FFCC66 !important; }",
      "a:link { color: #FFCC66 !important; }",
      "a:visited { color: #FFB720 !important; }",
      "a:hover { color: #FFFFCC !important; }",
      "a:active { color: #FFFFCC !important; }",
    ].join("\n");
    if (document.head) {
      document.head.appendChild(style);
    } else {
      document.documentElement.appendChild(style);
    }
  }

}());
Here is how to sort tabs in the current window.

Code: Select all

/* Foxy Gestures UserScript */

(function () {

  executeInBackground("(" + function () {
    var promise = getCurrentWindowTabs();
    promise.then(function (aTabs) {
      var tabList = [];
      for (var i = 0; i < aTabs.length; i++) {
        tabList.push(aTabs[i].url.
          replace("about:", "aaa").
          replace("chrome://", "bbb").
          replace("moz-extension://", "ccc").
          replace("file:///", "ddd").
          replace("ftp://", "eee").
          replace("http://", "fff").
          replace("https://", "fff").
          replace("newtab", "zzz").
          replace("www.", "") + "***" + aTabs[i].id);
      }
      tabList = tabList.sort();
      for (var i = 0; i < tabList.length; i++) {
        var id = tabList[i].substring(tabList[i].indexOf("***") + 3);
        browser.tabs.move(parseInt(id), {index: i});
      }
    });
  } + ")", []);

}());
Stal5
Posts: 1
Joined: September 30th, 2017, 1:52 pm

Re: Help in Firegesture script conversion to FoxyGesture....

Post by Stal5 »

I need help in Firegesture script conversion to FoxyGesture too. Open youtube links in potplayer:

Code: Select all

const APP_PATH = "C:\W1\Progs\PotPlayer\PotPlayerMini.exe";

var srcNode = FireGestures.sourceNode;
var linkURL = FireGestures.getLinkURL(srcNode);
if (!linkURL)
throw "Not on a link";

var file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
file.initWithPath(APP_PATH);
if (!file.exists()) {
alert("File does not exist: " + APP_PATH);
return;
}
var process = Cc["@mozilla.org/process/util;1"].createInstance(Ci.nsIProcess);
try {
var args = [linkURL];
process.init(file);
process.run(false, args, args.length);
}
catch (ex) {
alert("Failed to execute: " + APP_PATH);
}

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

Re: Help in Firegesture script conversion to FoxyGesture....

Post by morat »

@Stal5

Foxy Gestures is a WebExtension.

WebExtensions cannot access the file system.
Martin Brinkmann wrote:Greasemonkey won't come with a text editor anymore but relies on an embedded editor instead. This is a restriction of the new WebExtensions system as extensions cannot access the file system anymore.
Greasemonkey and Firefox 57 compatibility
http://www.ghacks.net/2017/09/21/grease ... atibility/
morat
Posts: 6421
Joined: February 3rd, 2009, 6:29 pm

Re: Help in Firegesture script conversion to FoxyGesture....

Post by morat »

I got the Foxy Gestures extension to open the userChrome.css file in notepad using the config file, but the hack only works with multiprocess windows disabled.

Here is how to disable multiprocess windows.

* open about:config
* set browser.tabs.remote.autostart preference to false
* restart

* open about:support
* is multiprocess windows disabled?
* if yes, then continue

Here is how to set up the user script and config file.

* add user script and gesture
* create <installation directory>\defaults\pref\autoconfig.js file
* create <installation directory>\mozilla.cfg file
* restart

* perform gesture to test

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

Code: Select all

/* Foxy Gestures UserScript */

(function () {

  console.log("Foxy Gestures Open userChrome.css");

}());

Code: Select all

pref("general.config.sandbox_enabled", false);
pref("general.config.filename", "mozilla.cfg");
pref("general.config.obscure_value", 0);

Code: Select all

// 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 consoleMsg = aSubject.wrappedJSObject;
  if (consoleMsg.arguments && consoleMsg.arguments.length) {
    switch (consoleMsg.arguments[0]) {
      case "Foxy Gestures Open userChrome.css":
        var file = Components.classes["@mozilla.org/file/directory_service;1"].
          getService(Components.interfaces.nsIProperties).
          get("UChrm", Components.interfaces.nsIFile);
        file.append("userChrome.css");
        var localFile = Components.classes["@mozilla.org/file/local;1"].
          createInstance(Components.interfaces.nsIFile);
        var process = Components.classes["@mozilla.org/process/util;1"].
          createInstance(Components.interfaces.nsIProcess);
        var args = [file.path];
        localFile.initWithPath("C:\\Windows\\System32\\notepad.exe");
        process.init(localFile);
        process.run(false, args, args.length);
        break;
    }
  }
}, "console-api-log-event", false);
Deploying Firefox in an enterprise environment
http://web.archive.org/web/201910060610 ... _before_60

Observer Notifications
http://web.archive.org/web/201910060345 ... ifications

Edit:

The hack doesn't work anymore even if you disable multiprocess windows.

How to disable Multiprocess in Firefox 83
http://forums.mozillazine.org/viewtopic ... &t=3067564
Post Reply