2020-06-07 19:17:09
// ==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() })