Greasy Fork

来自缓存

Greasy Fork is available in English.

链接新标签页打开

a标签添加target=_blank属性

// ==UserScript==
// @name         链接新标签页打开
// @namespace    https://bestlzk.cn/
// @version      1.0
// @description  a标签添加target=_blank属性
// @author       bestlzk
// @license      MIT
// @match        *://*/*
// @grant        none
// @run-at       document-end
// ==/UserScript==

function updateLinks(selector) {
    document.querySelectorAll(selector).forEach(link => {
        if (!link.target) link.target = '_blank';
    });
}

// document.querySelectorAll('').forEach(link => {console.log(link.href)})
const linkUpdateConfig = {
    'news.ycombinator.com': [
        'td.title > span > a'
    ]
};

(function() {
    const hostname = location.hostname;
    const selectors = linkUpdateConfig[hostname];
    if (!Array.isArray(selectors)) return;

    selectors.forEach(updateLinks);

    const observer = new MutationObserver(() => {
        selectors.forEach(updateLinks);
    });

    observer.observe(document.body, { childList: true, subtree: true });
})();