Prompt On New Tab

Display a confirmation dialog when the site wants to open a new tab. This won't work if the user opens a link in a new tab using web browser's "Open in a new tab", "Open in background tab", or similar which are web browser internal features or browser extension features.

目前为 2018-02-12 提交的版本。查看 最新版本

// ==UserScript==
// @name         Prompt On New Tab
// @namespace    https://gf.qytechs.cn/en/users/85671-jcunews
// @version      1.0.1
// @license      GNU AGPLv3
// @description  Display a confirmation dialog when the site wants to open a new tab. This won't work if the user opens a link in a new tab using web browser's "Open in a new tab", "Open in background tab", or similar which are web browser internal features or browser extension features.
// @author       jcunews
// @match        *://*/*
// @grant        none
// @run-at       document-start
// ==/UserScript==

(function(open_, anchors, forms) {
  function dummy(){}

  open_ = window.open;
  window.open = function(url) {
    if (!confirm("This site want to open a new tab.\nDo you want to allow it?\n\nURL:\n" + url)) {
      var loc = {};
      return {
        document: {
          close: dummy,
          location: loc,
          open: dummy,
          write: dummy
        },
        location: loc
      };
    }
  };

  anchors = document.getElementsByTagName("A");
  function onAnchorClick(ev) {
    if (this.href && (/^https?:/).test(this.href) && this.target &&
      !confirm("This site want to open a new tab.\nDo you want to allow it?\n\nURL:\n" + this.href)) {
      if (ev && ev.preventDefault) {
        ev.preventDefault();
        ev.stopPropagation();
        ev.stopImmediatePropagation();
      }
    }
  }

  forms = document.getElementsByTagName("FORM");
  function onFormSubmit(ev) {
    if (this.action && this.target &&
      !confirm("This site want to submit a form in a new tab.\nDo you want to allow it?\n\nURL:\n" + this.action)) {
      if (ev && ev.preventDefault) {
        ev.preventDefault();
        ev.stopPropagation();
        ev.stopImmediatePropagation();
      }
    } else if (!ev || !ev.preventDefault) {
      return this.submit_pont.apply(this, arguments);
    }
  }

  setInterval(function(i) {
    for (i = anchors.length-1; i >= 0; i--) {
      if (!anchors[i].listended) {
        anchors[i].listended = 1;
        anchors[i].addEventListener("click", onAnchorClick, true);
      } else break;
    }
    for (i = forms.length-1; i >= 0; i--) {
      if (!forms[i].listended) {
        forms[i].listended = 1;
        forms[i].submit_pont = forms[i].submit;
        forms[i].submit = onFormSubmit;
        forms[i].addEventListener("submit", onFormSubmit, true);
      } else break;
    }
  }, 500);
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址