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

2020-06-07 19:17:09

You will need to install an extension such as Tampermonkey, Greasemonkey or Violentmonkey to install this script.

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

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

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

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

You will need to install a user script manager extension to install this script.

(I already have a user script manager, let me install it!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(I already have a user style manager, let me install it!)

// ==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()
})