Gamesquad URL Converter

Automatically redirect old URLs of Gamesquad Forum to new ones, inspired by @JRV

  1. // ==UserScript==
  2. // @name Gamesquad URL Converter
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1.3.2
  5. // @license GPLv3
  6. // @description Automatically redirect old URLs of Gamesquad Forum to new ones, inspired by @JRV
  7. // @author micalex
  8. // @match *://*.gamesquad.com/*
  9. // @grant none
  10. // ==/UserScript==
  11. (function() {
  12. const url = window.location.href;
  13. const regexMap = {
  14. google: /http:\/\/forums.gamesquad.com\/showthread.php\?([0-9]+)-/,
  15. archive: /http:\/\/forums.gamesquad.com\/archive\/index.php\/t-([0-9]+)/,
  16. gamesquad: /http:\/\/forums.gamesquad.com\/showthread.php\?t=([0-9]+)/,
  17. oldNewGamesquad: /http:\/\/www.gamesquad.com\/xenforo\/index.php\?[^.]+\.([0-9]+)/,
  18. oldPostUrl: /http:\/\/forums.gamesquad.com\/showpost.php\?p=([0-9]+)/,
  19. forumdisplay: /http:\/\/forums.gamesquad.com\/forumdisplay.php\?(\d+)-(.+)/,
  20. member: /http:\/\/forums.gamesquad.com\/member.php\?(\d+)-(.+)/
  21. };
  22. const baseNewUrlMap = {
  23. google: 'https://www.gamesquad.com/forums/index.php?threads/',
  24. archive: 'https://www.gamesquad.com/forums/index.php?threads/',
  25. gamesquad: 'https://www.gamesquad.com/forums/index.php?threads/',
  26. oldNewGamesquad: 'https://www.gamesquad.com/forums/index.php?threads/',
  27. oldPostUrl: 'https://www.gamesquad.com/forums/index.php?posts/',
  28. forumdisplay: 'https://gamesquad.com/forums/index.php?forums/',
  29. member: 'https://www.gamesquad.com/forums/index.php?members/'
  30. };
  31.  
  32. const xhr = new XMLHttpRequest();
  33. xhr.open('HEAD', url, true);
  34. xhr.onload = function() {
  35. if (xhr.status === 404) {
  36. const match = Object.entries(regexMap).find(([key, regex]) => url.match(regex));
  37. if (match) {
  38. let newUrl = '';
  39. const rule = match[0];
  40. if (rule === 'forumdisplay') {
  41. const matches = url.match(regexMap.forumdisplay);
  42. const forumId = matches[1];
  43. let forumName = matches[2];
  44. forumName = forumName.toLowerCase().startsWith('asl-') ? forumName.slice(4).toLowerCase() : forumName.toLowerCase();
  45. newUrl = baseNewUrlMap.forumdisplay + forumName + '.' + forumId + '/';
  46. } else if (rule === 'member') {
  47. const matches = url.match(regexMap.member);
  48. const memberId = matches[1];
  49. const memberName = matches[2].toLowerCase();
  50. newUrl = baseNewUrlMap.member + memberName + '.' + memberId + '/';
  51. } else {
  52. const threadId = url.match(regexMap[rule])[1];
  53. const baseNewUrl = baseNewUrlMap[rule];
  54. newUrl = baseNewUrl + threadId;
  55. const query = url.indexOf('?') > -1 ? url.substring(url.indexOf('?') + 1) : '';
  56. const queryParts = query ? query.split('&') : [];
  57. const newQuery = queryParts.slice(1).join('&');
  58. newUrl = newUrl + (newQuery ? '&' + newQuery : '');
  59. }
  60. document.body.innerHTML = '';
  61. document.title = 'Detected old URL, redirecting';
  62. const container = document.createElement('div');
  63. container.className = 'container';
  64. document.body.appendChild(container);
  65. const text = document.createElement('div');
  66. text.className = 'text';
  67. text.textContent = 'Detected old URL, redirecting';
  68. let dots = 0;
  69. const interval = setInterval(() => {
  70. dots = (dots + 1) % 4;
  71. text.textContent = `Detected old URL, redirecting${'.'.repeat(dots)}`;
  72. }, 500);
  73. container.appendChild(text);
  74. setTimeout(() => {
  75. clearInterval(interval);
  76. window.location.href = newUrl;
  77. }, 2000);
  78. } else if (!document.title.includes('Oops!')) {
  79. document.body.innerHTML = '';
  80. document.title = 'Unable to match string as old gamesquad URL.';
  81. const container = document.createElement('div');
  82. container.className = 'container';
  83. document.body.appendChild(container);
  84. const errorMessage = document.createElement('div');
  85. errorMessage.className = 'text';
  86. errorMessage.textContent = 'Unable to match string as old gamesquad URL.';
  87. container.appendChild(errorMessage);
  88. const backButton = document.createElement('button');
  89. backButton.className = 'back-button';
  90. const action = window.history.length > 1 ? { text: 'Back', handler: () => window.history.go(-1) } : { text: 'Close', handler: () => window.close() };
  91. backButton.textContent = action.text;
  92. backButton.onclick = action.handler;
  93. container.appendChild(backButton);
  94. const style = document.createElement('style');
  95. style.textContent = `
  96. .container {
  97. display: flex;
  98. flex-direction: column;
  99. align-items: center;
  100. justify-content: center;
  101. height: 100vh;
  102. }
  103. .text {
  104. font-size: 24px;
  105. font-weight: bold;
  106. color: #000;
  107. text-align: center;
  108. margin-bottom: 20px;
  109. }
  110. .back-button {
  111. padding: 10px 20px;
  112. border: none;
  113. border-radius: 5px;
  114. background-color: #4CAF50;
  115. color: #fff;
  116. cursor: pointer;
  117. margin: 0 auto;
  118. display: block;
  119. }
  120. `;
  121. document.head.appendChild(style);
  122. }
  123. } else {
  124. return;
  125. }
  126. };
  127. xhr.send();
  128. })();