Reddit Links Open in Same Tab

Enforce `target="_self"` on links within Reddit posts and messages on desktop, delevoped for next chapter links on r/HFY.

当前为 2023-10-28 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Reddit Links Open in Same Tab
  3. // @namespace ultrabenosaurus.Reddit
  4. // @version 1.5
  5. // @description Enforce `target="_self"` on links within Reddit posts and messages on desktop, delevoped for next chapter links on r/HFY.
  6. // @author Ultrabenosaurus
  7. // @license GNU AGPLv3
  8. // @source https://greasyfork.org/en/users/437117-ultrabenosaurus?sort=name
  9. // @match https://www.reddit.com/r/*
  10. // @match https://www.reddit.com/message/*
  11. // @match https://old.reddit.com/r/*
  12. // @match https://old.reddit.com/message/*
  13. // @icon https://www.google.com/s2/favicons?sz=64&domain=reddit.com
  14. // @grant none
  15. // ==/UserScript==
  16.  
  17. (function() {
  18. 'use strict';
  19.  
  20. setTimeout(function(){
  21. RemoveTargetFromLinks();
  22. }, 1000);
  23. })();
  24.  
  25. function RemoveTargetFromLinks(){
  26. //console.log("RemoveTargetFromLinks", location.origin);
  27. var links=null;
  28. if("https://www.reddit.com"==location.origin){
  29. 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"]');
  30. for (var lin in links) {
  31. if (links.hasOwnProperty(lin)) {
  32. links[lin].removeAttribute('target');
  33. links[lin].setAttribute('target', '_self');
  34. }
  35. }
  36. } else if("https://old.reddit.com"==location.origin){
  37. links=document.querySelectorAll('div.content div.usertext-body a, div.content div.md-container a[href*="www.reddit.com"]');
  38. for (var lin in links) {
  39. if (links.hasOwnProperty(lin)) {
  40. var linClone=links[lin].cloneNode(true);
  41. linClone.removeAttribute('target');
  42. linClone.setAttribute('target', '_self');
  43. if(-1<linClone.attributes.href.nodeValue.indexOf("//www.reddit.com")){
  44. linClone.attributes.href.nodeValue = linClone.attributes.href.nodeValue.replace("www.reddit.com", "old.reddit.com");
  45. }
  46. if(-1<linClone.attributes.href.nodeValue.indexOf("//reddit.com")){
  47. linClone.attributes.href.nodeValue = linClone.attributes.href.nodeValue.replace("reddit.com", "old.reddit.com");
  48. }
  49. if(-1<linClone.attributes.href.nodeValue.indexOf("//redd.it/")){
  50. linClone.attributes.href.nodeValue = linClone.attributes.href.nodeValue.replace("redd.it", "old.reddit.com/comments");
  51. }
  52. links[lin].parentNode.insertBefore(linClone, links[lin]);
  53. links[lin].remove();
  54. }
  55. }
  56. }
  57. }