Reddit Links Open in Same Tab

Remove `target="_blank"` from links within Reddit posts on desktop, focused on next chapter links on r/HFY.

目前为 2023-10-26 提交的版本。查看 最新版本

// ==UserScript==
// @name         Reddit Links Open in Same Tab
// @namespace    ultrabenosaurus.Reddit
// @version      1.3
// @description  Remove `target="_blank"` from links within Reddit posts on desktop, focused on next chapter links on r/HFY.
// @author       Ultrabenosaurus
// @license      GNU AGPLv3
// @source       https://gf.qytechs.cn/en/users/437117-ultrabenosaurus?sort=name
// @match        https://www.reddit.com/r/*
// @match        https://old.reddit.com/r/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=reddit.com
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    setTimeout(function(){
        RemoveTargetFromLinks();
    }, 1000);
})();

function RemoveTargetFromLinks(){
    //console.log("RemoveTargetFromLinks", location.origin);
    var links=null;
    if("https://www.reddit.com"==location.origin){
        links=document.querySelectorAll('div[data-test-id="post-content"] div.RichTextJSON-root p > a[target="_blank"]');
        for (var lin in links) {
            if (links.hasOwnProperty(lin)) {
                links[lin].removeAttribute('target');
            }
        }
    } else if("https://old.reddit.com"==location.origin){
        links=document.querySelectorAll('div.content div.usertext-body a');
        console.log("old 1");
        for (var lin in links) {
            if (links.hasOwnProperty(lin)) {
                var linClone=links[lin].cloneNode(true);
                linClone.removeAttribute('target');
                linClone.setAttribute('target', '_self');
                if(-1<linClone.attributes.href.nodeValue.indexOf("www.reddit.com")){
                    linClone.attributes.href.nodeValue = linClone.attributes.href.nodeValue.replace("www.reddit.com", "old.reddit.com");
                }
                links[lin].parentNode.insertBefore(linClone, links[lin]);
                links[lin].remove();
            }
        }
    }
}

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址