Greasy Fork

来自缓存

Greasy Fork is available in English.

链接地址全在【当前/新建】标签页中打开

2020-06-07 19:17:09

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name        链接地址全在【当前/新建】标签页中打开
// @namespace   Open in self/new tab.
// @match       *://*/*
// @grant       GM_registerMenuCommand
// @grant       GM_unregisterMenuCommand
// @grant       GM_getValue
// @grant       GM_setValue
// @version     0.0.3
// @author      稻米鼠
// @description 2020-06-07 19:17:09
// ==/UserScript==

/** 获取是否显示页面工具栏 **/
let isShowPageBar = GM_getValue('inNewPage', true);
console.log(isShowPageBar)
const menuNames = ['【当前】在当前标签打开链接', '【当前】在新标签打开链接']

const main = ()=>{
  document.querySelectorAll('a').forEach(el=>{
    if(isShowPageBar){
      if(/^_blank$/i.test(el.target)) return
      el.target = '_blank'
    }else{
      if(/^(_self)?$/i.test(el.target)) return
      el.target = '_self'
    }
    console.log(el.innerText)
  })
}

const init = (caption, captionRemove)=>{
  GM_unregisterMenuCommand(captionRemove)
  GM_registerMenuCommand(caption, ()=>{
    isShowPageBar = !isShowPageBar
    GM_setValue('inNewPage', isShowPageBar)
    main()
    alert('当前页面立刻生效,其他页面刷新后生效。')
  })
}
if(isShowPageBar){
  init(menuNames[1], menuNames[0])
}else{
  init(menuNames[0], menuNames[1])
}
main()
document.addEventListener('DOMNodeInserted', (e)=>{
  main()
})
window.addEventListener('load', ()=>{
  main()
})