MozillaZine

Help in Firegesture script conversion to FoxyGesture....

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

Post Posted September 26th, 2017, 7:35 am

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

Post Posted September 26th, 2017, 11:51 am

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

Post Posted September 26th, 2017, 6:44 pm

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: 127
Joined: December 13th, 2013, 7:40 pm

Post Posted September 26th, 2017, 7:33 pm

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: 127
Joined: December 13th, 2013, 7:40 pm

Post Posted September 26th, 2017, 10:25 pm

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

Post Posted September 27th, 2017, 6:13 am

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: 127
Joined: December 13th, 2013, 7:40 pm

Post Posted September 27th, 2017, 6:21 am

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

Post Posted September 27th, 2017, 6:32 am

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: 127
Joined: December 13th, 2013, 7:40 pm

Post Posted September 27th, 2017, 6:38 am

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

Post Posted September 27th, 2017, 5:07 pm

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

Post Posted September 30th, 2017, 2:02 pm

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

Post Posted October 1st, 2017, 3:36 am

@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/

Return to Firefox Builds


Who is online

Users browsing this forum: No registered users and 7 guests