Redirect old Nexus Mods links

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

目前为 2019-06-13 提交的版本。查看 最新版本

  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.1
  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. if (match[1].startsWith("static"))
  25. return;
  26. href = href.replace(/:\/\/[^/]*\/mods\//, "://www.nexusmods.com/" + match[1] + "/mods/");
  27. }
  28. match = href.match(/:\/\/([a-z0-9]+)\.nexusmods\.com\/downloads\/+file\.php.*?[?^]id=([0-9]+)/);
  29. if (match) {
  30. href = href.replace(/:\/\/[^/]*\/.*/, "://www.nexusmods.com/" + match[1] + "/mods/" + match[2]);
  31. }
  32. match = href.match(/(:\/\/(?:www\.)?nexusmods\.com\/[^/]*)\/+downloads\/+file\.php.*?[?^]id=([0-9]+)/);
  33. if (match) {
  34. href = href.replace(/:\/\/[^/]*\/.*/, match[1] + "/mods/" + match[2] + "?tab=files");
  35. }
  36. return href;
  37. }
  38. href = replacelink(href);
  39. if (href !== oldhref) {
  40. document.location = href;
  41. return;
  42. }
  43. function onload(cb) {
  44. if (document.readyState === "complete") {
  45. cb();
  46. } else {
  47. var state_cb = function() {
  48. if (document.readyState === "complete") {
  49. cb();
  50.  
  51. document.removeEventListener("readystatechange", state_cb);
  52. }
  53. };
  54.  
  55. document.addEventListener("readystatechange", state_cb);
  56. }
  57. }
  58. onload(function() {
  59. // Replace all links too (newvegasnexus etc. is auto-redirected, so the links themselves have to be modified)
  60. function replaceel(el) {
  61. if (!el)
  62. return;
  63. if (el.href && typeof el.href === "string") {
  64. var href = replacelink(el.href);
  65. if (href !== el.href)
  66. el.href = href;
  67. }
  68.  
  69. if (!el.childNodes || el.childNodes.length === 0) {
  70. var html;
  71. if ("innerHTML" in el) {
  72. html = el.innerHTML;
  73. } else if ("data" in el) {
  74. html = el.nodeValue;
  75. } else {
  76. return;
  77. }
  78.  
  79. var changed = false;
  80. var match = html.match(/(https?:\/\/[^\s"'<>]*)/g);
  81. if (!match)
  82. return;
  83. for (var i = 0; i < match.length; i++) {
  84. var newhref = replacelink(match[i]);
  85. if (newhref !== match[i]) {
  86. html = html.split(match[i]).join(newhref);
  87. changed = true;
  88. }
  89. }
  90. if (changed) {
  91. if ("innerHTML" in el) {
  92. el.innerHTML = html;
  93. } else if ("data" in el) {
  94. el.nodeValue = html;
  95. }
  96. }
  97. } else if (el.childNodes && el.childNodes.length > 0) {
  98. for (var i = 0; i < el.childNodes.length; i++) {
  99. if (el.childNodes[i].nodeName === "#text") {
  100. replaceel(el.childNodes[i]);
  101. }
  102. }
  103. }
  104. }
  105. var els = document.querySelectorAll("*");
  106. for (var i = 0; i < els.length; i++) {
  107. replaceel(els[i]);
  108. }
  109. var observer = new MutationObserver(function(mutations, observer) {
  110. for (var i = 0; i < mutations.length; i++) {
  111. if (mutations[i].addedNodes) {
  112. for (var x = 0; x < mutations[i].addedNodes.length; x++) {
  113. replaceel(mutations[i].addedNodes[x]);
  114. }
  115. }
  116. }
  117. });
  118. observer.observe(document, {childList: true, subtree: true});
  119. });
  120. })();