Override HTML Standard Form Submission Result To Current Or New Tab

Make HTML standard form submission result be loaded in either the current tab (SHIFT key held), or in a new window (CTRL key held), when submitting the form. This script won't work on forms which don't use HTML standard form submission. e.g. AJAX.

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

Dovrai installare un'estensione come Tampermonkey o Violentmonkey per installare questo 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        Override HTML Standard Form Submission Result To Current Or New Tab
// @namespace   https://greasyfork.org/en/users/85671-jcunews
// @version     1.0.2
// @license     AGPL v3
// @author      jcunews
// @description Make HTML standard form submission result be loaded in either the current tab (SHIFT key held), or in a new window (CTRL key held), when submitting the form. This script won't work on forms which don't use HTML standard form submission. e.g. AJAX.
// @match       *://*/*
// @grant       none
// @run-at      document-start
// ==/UserScript==

(keys => {
  keys = {Shift: false, Control: false, Alt: false};
  function ovr(f, n) {
    f.dataset.tgt = f.target;
    f.target = n;
    setTimeout(() => {
      f.target = f.dataset.tgt;
      delete f.dataset.tgt
    }, 100)
  }
  function chk(f) {
    if ((f.dataset.tgt === undefined) && !keys.Alt) {
      if (keys.Shift && !keys.Control) {
        ovr(f, "")
      } else if (keys.Control && !keys.Shift) ovr(f, "_blank")
    }
  }
  function reset() {
    keys = {Shift: false, Control: false, Alt: false}
  }
  addEventListener("submit", ev => chk(ev.target), true);
  addEventListener("click", ev => ev.target?.form && chk(ev.target.form), true);
  addEventListener("keydown", ev => {
    if (ev.key === "Enter") {
      if (ev.target?.form) chk(ev.target.form)
    } else if (ev.key in keys) keys[ev.key] = true
  }, true);
  addEventListener("keyup", ev => (ev.key in keys) && (keys[ev.key] = false), true);
  addEventListener("blur", reset);
  addEventListener("focus", reset)
})()