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.

  1. // ==UserScript==
  2. // @name Reddit Links Open in Same Tab
  3. // @namespace ultrabenosaurus.Reddit
  4. // @version 1.8
  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. if("https://www.reddit.com"==location.origin && -1<location.search.indexOf("share_id=")){
  20. location.href = location.href.replace(location.search, '').replace("www.reddit.com", "old.reddit.com");
  21. }
  22.  
  23. setTimeout(function(){
  24. RemoveTargetFromLinks();
  25. }, 1000);
  26. })();
  27.  
  28. function RemoveTargetFromLinks(){
  29. //console.log("RemoveTargetFromLinks", location.origin);
  30. var links=null;
  31. if("https://www.reddit.com"==location.origin){
  32. 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"]');
  33. for (var lin in links) {
  34. if (links.hasOwnProperty(lin)) {
  35. links[lin].removeAttribute('target');
  36. links[lin].setAttribute('target', '_self');
  37. }
  38. }
  39. } else if("https://old.reddit.com"==location.origin){
  40. links=document.querySelectorAll('div.content div.usertext-body a[href], div.content div.md-container a[href*="www.reddit.com"]');
  41. for (var lin in links) {
  42. if (links.hasOwnProperty(lin)) {
  43. try{
  44. var linClone=links[lin].cloneNode(true);
  45. linClone.removeAttribute('target');
  46. linClone.setAttribute('target', '_self');
  47. //console.log(linClone.attributes.href);
  48. if(-1==linClone.attributes.href.nodeValue.indexOf("/s/")){
  49. if(-1<linClone.attributes.href.nodeValue.indexOf("//www.reddit.com")){
  50. linClone.attributes.href.nodeValue = linClone.attributes.href.nodeValue.replace("www.reddit.com", "old.reddit.com");
  51. }
  52. if(-1<linClone.attributes.href.nodeValue.indexOf("//reddit.com")){
  53. linClone.attributes.href.nodeValue = linClone.attributes.href.nodeValue.replace("reddit.com", "old.reddit.com");
  54. }
  55. if(-1<linClone.attributes.href.nodeValue.indexOf("//redd.it/")){
  56. linClone.attributes.href.nodeValue = linClone.attributes.href.nodeValue.replace("redd.it", "old.reddit.com/comments");
  57. }
  58. }
  59. links[lin].parentNode.insertBefore(linClone, links[lin]);
  60. links[lin].remove();
  61. }catch(e){
  62. console.log(e);
  63. }
  64. }
  65. }
  66. }
  67. }