Right Open & Close

RClick open link, Double RClick close tab

Dovrai installare un'estensione come Tampermonkey, Greasemonkey o Violentmonkey per installare questo script.

You will need to install an extension such as Tampermonkey to install this script.

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo script.

Dovrai installare un'estensione come Tampermonkey o Userscripts per installare questo script.

Dovrai installare un'estensione come ad esempio Tampermonkey per installare questo script.

Dovrai installare un gestore di script utente per installare questo script.

(Ho già un gestore di script utente, lasciamelo installare!)

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione come ad esempio Stylus per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

Dovrai installare un'estensione per la gestione degli stili utente per installare questo stile.

(Ho già un gestore di stile utente, lasciamelo installare!)

// ==UserScript==
// @name        Right Open & Close
// @namespace   Violentmonkey Scripts
// @match       *://*/*
// @match       file:///*
// @run-at      document-start
// @grant       unsafeWindow
// @grant       GM_openInTab
// @grant       window.close
// @version     1.2
// @author      leoshone
// @description RClick open link, Double RClick close tab
// ==/UserScript==
(function() {
  //Hide context menu, Press CTRL + RClick show context menu
  document.addEventListener('contextmenu', function(e) {
    if (!e.ctrlKey)
      e.preventDefault();
  }, false);
  
  //RClick open link in background tab, Double RClick close tab
  var clickNo = 0;
  var resetId;
  document.addEventListener('mousedown', function(e) {
    if (!e.ctrlKey && e.button == 2) {
      clickNo++;
      if (clickNo == 1) {
        resetId = setTimeout(function() {
          clickNo = 0;
          var href = e.target.closest('a').href;  
          if (href !== "" && !/^javascript:/i.test(href.toString()))
            GM_openInTab(href, {active: false, insert: false});
        }, 300);
      } else if (clickNo == 2) {
        clickNo = 0;
        clearTimeout(resetId);
        window.close();
      } 
    }
    else
      clickNo = 0;
  }, false);
})();