Greasy Fork

Greasy Fork is available in English.

强制所有链接在当前标签打开

强制所有链接在当前标签打开,本脚本采用白名单机制,需手动点击油猴脚本的菜单生效

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         强制所有链接在当前标签打开
// @namespace    http://greasyfork.icu/zh-CN/scripts/446917-%E5%BC%BA%E5%88%B6%E6%89%80%E6%9C%89%E9%93%BE%E6%8E%A5%E5%9C%A8%E5%BD%93%E5%89%8D%E6%A0%87%E7%AD%BE%E6%89%93%E5%BC%80
// @version      0.33
// @license MIT
// @description  强制所有链接在当前标签打开,本脚本采用白名单机制,需手动点击油猴脚本的菜单生效
// @grant unsafeWindow
// @author       meteora
// @match *://*/*
// @grant        GM_registerMenuCommand
// @grant GM_unregisterMenuCommand
// @grant GM_setValue
// @grant GM_getValue
// ==/UserScript==
// ==UserScript==
// @name         强制所有链接在当前标签打开
// @namespace    http://greasyfork.icu/zh-CN/scripts/446917-%E5%BC%BA%E5%88%B6%E6%89%80%E6%9C%89%E9%93%BE%E6%8E%A5%E5%9C%A8%E5%BD%93%E5%89%8D%E6%A0%87%E7%AD%BE%E6%89%93%E5%BC%80
// @version      0.29
// @license MIT
// @description  强制所有链接在当前标签打开,本脚本采用白名单机制,需手动点击油猴脚本的菜单生效
// @grant unsafeWindow
// @author       meteora
// @match *://*/*
// @grant        GM_registerMenuCommand
// @grant GM_unregisterMenuCommand
// @grant GM_setValue
// @grant GM_getValue
// @downloadURL https://update.greasyfork.icu/scripts/446917/%E5%BC%BA%E5%88%B6%E6%89%80%E6%9C%89%E9%93%BE%E6%8E%A5%E5%9C%A8%E5%BD%93%E5%89%8D%E6%A0%87%E7%AD%BE%E6%89%93%E5%BC%80.user.js
// @updateURL https://update.greasyfork.icu/scripts/446917/%E5%BC%BA%E5%88%B6%E6%89%80%E6%9C%89%E9%93%BE%E6%8E%A5%E5%9C%A8%E5%BD%93%E5%89%8D%E6%A0%87%E7%AD%BE%E6%89%93%E5%BC%80.meta.js
// ==/UserScript==

(function () {
  "use strict";
  // 白名单管理
  function whiteList() {
    let domainEnable;
    let subDomainEnable;
    //一级域名,例如:baidu.com
    let currentDomain = location.hostname.split(".").slice(-2).join(".");
    //二级域名,例如 www.baidu.com
    let currentSubDomain = location.hostname;
    let domainList = GM_getValue("n2GndfLDBAh_domainList");
    let subDomainList = GM_getValue("n2GndfLDBAh_subDomainList");
    if (domainList) {
      domainList = JSON.parse(domainList);
      for (let i = 0; i < domainList.length; i++) {
        const item = domainList[i];
        if (item === currentDomain) {
          domainEnable = true;
          GM_registerMenuCommand("排除 " + currentDomain, () => {
            domainList.splice(i, 1);
            GM_setValue("n2GndfLDBAh_domainList", JSON.stringify(domainList));
            unsafeWindow.location.reload();
          });
          break;
        }
      }
    }
    if (!domainEnable) {
      GM_registerMenuCommand("添加 " + currentDomain, () => {
        if (!domainList) {
          domainList = [];
        }
        domainList.push(currentDomain);
        GM_setValue("n2GndfLDBAh_domainList", JSON.stringify(domainList));
        unsafeWindow.location.reload();
      });
    }
    if (subDomainList) {
      subDomainList = JSON.parse(subDomainList);
      for (let i = 0; i < subDomainList.length; i++) {
        const item = subDomainList[i];
        if (item === currentSubDomain) {
          subDomainEnable = true;
          GM_registerMenuCommand("排除 " + currentSubDomain, () => {
            subDomainList.splice(i, 1);
            GM_setValue(
              "n2GndfLDBAh_subDomainList",
              JSON.stringify(subDomainList),
            );
            unsafeWindow.location.reload();
          });
          break;
        }
      }
    }
    if (!subDomainEnable && currentSubDomain !== currentDomain) {
      GM_registerMenuCommand("添加 " + currentSubDomain, () => {
        if (!subDomainList) {
          subDomainList = [];
        }
        subDomainList.push(currentSubDomain);
        GM_setValue("n2GndfLDBAh_subDomainList", JSON.stringify(subDomainList));
        unsafeWindow.location.reload();
      });
    }
    return domainEnable || subDomainEnable;
  }
  // const listener = function (e) {
  //   let dom = e.target;
  //   if (dom.nodeName === "A") {
  //     dom.target = "_self";
  //     return;
  //   }
  //   //循环迭代获取父节点
  //   for (let i = 0; i < 5; i++) {
  //     dom = dom.parentNode;
  //     //如果是a标签
  //     if (dom.nodeName === "A") {
  //       dom.target = "_self";
  //       return;
  //     }
  //   }
  // };
  // document.body.addEventListener("click", listener, true);

  //hook页面
  function hookPage() {
    document.head.appendChild(document.createElement("base")).target = "_self";
    //替换form标签里面的target属性
    let domList = document.querySelectorAll("form");
    for (let i = 0; i < domList.length; i++) {
      domList[i].target = "_self";
    }
    //替换a标签里面的target属性
    domList = document.querySelectorAll("a");
    for (let i = 0; i < domList.length; i++) {
      domList[i].target = "_self";
    }
    //对于调用window.open跳转的
    const open = unsafeWindow.open;
    const newOpen = function (
      url = false,
      target = "_self",
      windowFeatures = false,
    ) {
      if (url && windowFeatures) {
        open(url, "_self", windowFeatures);
      } else if (url) {
        open(url, "_self");
      } else {
        open();
      }
    };
    Object.defineProperty(unsafeWindow, "open", {
      value: newOpen,
    });
  }

  function main() {
    //排除iframe
    if (window.self !== window.top) {
      return;
    }
    //白名单管理
    if (!whiteList()) return false;
    hookPage();
    // 监听页面url变化
    unsafeWindow.addEventListener("popstate", function (event) {
      console.log("URL changed to: " + document.location.href);
      hookPage();
    });
    unsafeWindow.addEventListener("hashchange", function () {
      console.log("URL changed to: " + document.location.href);
      hookPage();
    });
    //覆写 window.top.history.pushState 方法
    let originalPushState = unsafeWindow.top.history.pushState;
    unsafeWindow.top.history.pushState = function () {
      originalPushState.apply(this, arguments);
      hookPage();
    };
  }
  const oldOnloadFun = window.onload;
  unsafeWindow.onload = function () {
    if (oldOnloadFun) {
      oldOnloadFun();
    }
    main();
  };
})();