Redirect old Nexus Mods links

Redirects old and broken links from Nexus Mods to the new links

  1. // ==UserScript==
  2. // @name Redirect old Nexus Mods links
  3. // @description Redirects old and broken links from Nexus Mods to the new links
  4. // @version 0.2.2
  5. // @namespace Violentmonkey Scripts
  6. // @match https://*.nexusmods.com/*
  7. // @grant none
  8. // @run-at document-start
  9. // ==/UserScript==
  10.  
  11. (function() {
  12. var href = document.location.href;
  13. var oldhref = href;
  14. function replacelink(href) {
  15. var match;
  16. // http://www.newvegasnexus.com/downloads/file.php?id=36902
  17. match = href.match(/:\/\/(?:www\.)?([a-z0-9]+)nexus\.com\//);
  18. if (match) {
  19. href = href.replace(/:\/\/[^/]*\//, "://" + match[1] + ".nexusmods.com/");
  20. }
  21.  
  22. match = href.match(/:\/\/([a-z0-9]+)\.nexusmods\.com\/mods\//);
  23. if (match) {
  24. // https://staticdelivery.nexusmods.com/mods/110/images/98548/98548-1562953001-1271923712.jpeg
  25. if (match[1].startsWith("static"))
  26. return href;
  27. href = href.replace(/:\/\/[^/]*\/mods\//, "://www.nexusmods.com/" + match[1] + "/mods/");
  28. }
  29. match = href.match(/:\/\/([a-z0-9]+)\.nexusmods\.com\/downloads\/+file\.php.*?[?^]id=([0-9]+)/);
  30. if (match) {
  31. href = href.replace(/:\/\/[^/]*\/.*/, "://www.nexusmods.com/" + match[1] + "/mods/" + match[2]);
  32. }
  33. match = href.match(/(:\/\/(?:www\.)?nexusmods\.com\/[^/]*)\/+downloads\/+file\.php.*?[?^]id=([0-9]+)/);
  34. if (match) {
  35. href = href.replace(/:\/\/[^/]*\/.*/, match[1] + "/mods/" + match[2] + "?tab=files");
  36. }
  37. return href;
  38. }
  39. href = replacelink(href);
  40. if (href !== oldhref) {
  41. document.location = href;
  42. return;
  43. }
  44. function onload(cb) {
  45. if (document.readyState === "complete") {
  46. cb();
  47. } else {
  48. var state_cb = function() {
  49. if (document.readyState === "complete") {
  50. cb();
  51.  
  52. document.removeEventListener("readystatechange", state_cb);
  53. }
  54. };
  55.  
  56. document.addEventListener("readystatechange", state_cb);
  57. }
  58. }
  59. onload(function() {
  60. // Replace all links too (newvegasnexus etc. is auto-redirected, so the links themselves have to be modified)
  61. function replaceel(el) {
  62. if (!el)
  63. return;
  64. if (el.href && typeof el.href === "string") {
  65. var href = replacelink(el.href);
  66. if (href !== el.href)
  67. el.href = href;
  68. }
  69.  
  70. if (!el.childNodes || el.childNodes.length === 0) {
  71. var html;
  72. if ("innerHTML" in el) {
  73. html = el.innerHTML;
  74. } else if ("data" in el) {
  75. html = el.nodeValue;
  76. } else {
  77. return;
  78. }
  79.  
  80. var changed = false;
  81. var match = html.match(/(https?:\/\/[^\s"'<>]*)/g);
  82. if (!match)
  83. return;
  84. for (var i = 0; i < match.length; i++) {
  85. var newhref = replacelink(match[i]);
  86. if (newhref !== match[i]) {
  87. html = html.split(match[i]).join(newhref);
  88. changed = true;
  89. }
  90. }
  91. if (changed) {
  92. if ("innerHTML" in el) {
  93. el.innerHTML = html;
  94. } else if ("data" in el) {
  95. el.nodeValue = html;
  96. }
  97. }
  98. } else if (el.childNodes && el.childNodes.length > 0) {
  99. for (var i = 0; i < el.childNodes.length; i++) {
  100. if (el.childNodes[i].nodeName === "#text") {
  101. replaceel(el.childNodes[i]);
  102. }
  103. }
  104. }
  105. }
  106. var els = document.querySelectorAll("*");
  107. for (var i = 0; i < els.length; i++) {
  108. replaceel(els[i]);
  109. }
  110. var observer = new MutationObserver(function(mutations, observer) {
  111. for (var i = 0; i < mutations.length; i++) {
  112. if (mutations[i].addedNodes) {
  113. for (var x = 0; x < mutations[i].addedNodes.length; x++) {
  114. replaceel(mutations[i].addedNodes[x]);
  115. }
  116. }
  117. }
  118. });
  119. observer.observe(document, {childList: true, subtree: true});
  120. });
  121. })();

QingJ © 2025

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