Enforce `target="_self"` on links within Reddit posts and messages on desktop, delevoped for next chapter links on r/HFY.
当前为
// ==UserScript==
// @name Reddit Links Open in Same Tab
// @namespace ultrabenosaurus.Reddit
// @version 1.4
// @description Enforce `target="_self"` on links within Reddit posts and messages on desktop, delevoped for next chapter links on r/HFY.
// @author Ultrabenosaurus
// @license GNU AGPLv3
// @source https://greasyfork.org/en/users/437117-ultrabenosaurus?sort=name
// @match https://www.reddit.com/r/*
// @match https://www.reddit.com/message/*
// @match https://old.reddit.com/r/*
// @match https://old.reddit.com/message/*
// @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"], div.content div.md-container a[href*="www.reddit.com"]');
for (var lin in links) {
if (links.hasOwnProperty(lin)) {
links[lin].removeAttribute('target');
links[lin].setAttribute('target', '_self');
}
}
} else if("https://old.reddit.com"==location.origin){
links=document.querySelectorAll('div.content div.usertext-body a, div.content div.md-container a[href*="www.reddit.com"]');
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();
}
}
}
}