URL Redirector

Redirects specified URLs to new destinations

  1. // ==UserScript==
  2. // @name URL Redirector
  3. // @namespace http://your-namespace.com
  4. // @version 1.0
  5. // @description Redirects specified URLs to new destinations
  6. // @match http://*/*
  7. // @match https://*/*
  8. // @grant GM_setValue
  9. // @grant GM_getValue
  10. // @license MIT
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // 获取已保存的重定向规则
  17. var redirectRules = GM_getValue('redirectRules', []);
  18.  
  19. // 显示用户界面
  20. function showUserInterface() {
  21. var uiContainer = document.createElement('div');
  22. uiContainer.style.position = 'fixed';
  23. uiContainer.style.top = '20px';
  24. uiContainer.style.right = '20px';
  25. uiContainer.style.padding = '10px';
  26. uiContainer.style.backgroundColor = '#f9f9f9';
  27. uiContainer.style.border = '1px solid #ddd';
  28.  
  29. // 添加标题
  30. var title = document.createElement('h2');
  31. title.innerHTML = 'URL Redirector';
  32. uiContainer.appendChild(title);
  33.  
  34. // 添加规则列表
  35. var ruleList = document.createElement('ul');
  36. uiContainer.appendChild(ruleList);
  37.  
  38. // 添加规则输入字段和按钮
  39. var fromInput = document.createElement('input');
  40. fromInput.placeholder = 'From URL';
  41. uiContainer.appendChild(fromInput);
  42.  
  43. var toInput = document.createElement('input');
  44. toInput.placeholder = 'To URL';
  45. uiContainer.appendChild(toInput);
  46.  
  47. var addButton = document.createElement('button');
  48. addButton.innerHTML = 'Add Rule';
  49. addButton.addEventListener('click', function() {
  50. var fromUrl = fromInput.value.trim();
  51. var toUrl = toInput.value.trim();
  52.  
  53. if (fromUrl !== '' && toUrl !== '') {
  54. // 添加新规则到数组
  55. redirectRules.push({
  56. from: fromUrl,
  57. to: toUrl
  58. });
  59.  
  60. // 清空输入字段
  61. fromInput.value = '';
  62. toInput.value = '';
  63.  
  64. // 更新规则列表
  65. updateRuleList();
  66. }
  67. });
  68. uiContainer.appendChild(addButton);
  69.  
  70. // 更新规则列表
  71. function updateRuleList() {
  72. ruleList.innerHTML = '';
  73.  
  74. // 创建规则列表项
  75. for (var i = 0; i < redirectRules.length; i++) {
  76. var rule = redirectRules[i];
  77.  
  78. var listItem = document.createElement('li');
  79. listItem.innerHTML = rule.from + ' -> ' + rule.to;
  80.  
  81. var deleteButton = document.createElement('button');
  82. deleteButton.innerHTML = 'Delete';
  83. deleteButton.addEventListener('click', (function(index) {
  84. return function() {
  85. // 从数组中删除规则
  86. redirectRules.splice(index, 1);
  87.  
  88. // 更新规则列表
  89. updateRuleList();
  90. };
  91. })(i));
  92.  
  93. listItem.appendChild(deleteButton);
  94. ruleList.appendChild(listItem);
  95. }
  96.  
  97. // 保存更新后的规则数组
  98. GM_setValue('redirectRules', redirectRules);
  99. }
  100.  
  101. // 初始化规则列表
  102. updateRuleList();
  103.  
  104. // 将用户界面添加到页面
  105. document.body.appendChild(uiContainer);
  106. }
  107.  
  108. // 检查当前URL是否需要重定向
  109. function checkForRedirect() {
  110. var currentUrl = window.location.href;
  111.  
  112. // 检查每个规则是否匹配当前URL
  113. for (var i = 0; i < redirectRules.length; i++) {
  114. var rule = redirectRules[i];
  115.  
  116. if (currentUrl.includes(rule.from)) {
  117. // 如果匹配,则重定向到新目标URL
  118. var newUrl = currentUrl.replace(rule.from, rule.to);
  119. window.location.href = newUrl;
  120. break;
  121. }
  122. }
  123. }
  124.  
  125. // 在页面加载完成后检查重定向
  126. window.addEventListener('load', function() {
  127. // 显示用户界面
  128. showUserInterface();
  129.  
  130. // 检查重定向
  131. checkForRedirect();
  132. });
  133. })();