URL Manager

支持 HTTP 到 HTTPS 和 HTTPS 到 HTTP 重定向,支持关键字替换功能

  1. // ==UserScript==
  2. // @name URL Manager
  3. // @namespace https://space.bilibili.com/398910090
  4. // @version 1.1
  5. // @author Ace
  6. // @description 支持 HTTP 到 HTTPS 和 HTTPS 到 HTTP 重定向,支持关键字替换功能
  7. // @match *://*/*
  8. // @grant GM_registerMenuCommand
  9. // @grant GM_getValue
  10. // @grant GM_setValue
  11. // ==/UserScript==
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. // 获取重定向站点列表
  17. let httpToHttpsSites = JSON.parse(GM_getValue("httpToHttpsSites", "[]"));
  18. let httpsToHttpSites = JSON.parse(GM_getValue("httpsToHttpSites", "[]"));
  19. let keywordReplacements = JSON.parse(GM_getValue("keywordReplacements", "{}"));
  20.  
  21. // 添加菜单选项
  22. GM_registerMenuCommand("将当前网站添加到 HTTP 重定向规则", () => toggleSite(httpToHttpsSites, "httpToHttpsSites"));
  23. GM_registerMenuCommand("将当前网站添加到 HTTPS 重定向规则", () => toggleSite(httpsToHttpSites, "httpsToHttpSites"));
  24. GM_registerMenuCommand("添加/删除网址替换规则", manageKeywordReplacements);
  25.  
  26. // 站点添加/删除功能
  27. function toggleSite(siteList, storageKey) {
  28. const site = window.location.hostname;
  29. const siteIndex = siteList.indexOf(site);
  30. const action = siteIndex === -1 ? "添加到" : "从";
  31. if (confirm(`确定要删除${action} ${site} 的规则吗?`)) {
  32. siteIndex === -1 ? siteList.push(site) : siteList.splice(siteIndex, 1);
  33. GM_setValue(storageKey, JSON.stringify(siteList));
  34. alert(`已${action === "添加到" ? "添加" : "删除"} ${site} 的规则。`);
  35. }
  36. }
  37.  
  38. // 添加/删除网址替换规则
  39. function manageKeywordReplacements() {
  40. const url = window.location.hostname;
  41. const keyword = prompt("请输入要替换的网址关键字:");
  42. if (!keyword) return;
  43. const replacement = prompt(`将 ${keyword} 替换成什么内容:`);
  44. if (replacement === null) return;
  45.  
  46. if (!keywordReplacements[url]) keywordReplacements[url] = [];
  47. keywordReplacements[url].push({ keyword, replacement });
  48. GM_setValue("keywordReplacements", JSON.stringify(keywordReplacements));
  49. alert(`已为 ${url} 设置替换规则:${keyword} -> ${replacement}`);
  50. }
  51.  
  52. // 执行 HTTP 到 HTTPS 或 HTTPS 到 HTTP 重定向
  53. function performRedirect() {
  54. const protocol = window.location.protocol;
  55. const host = window.location.hostname;
  56.  
  57. if (protocol === "http:" && httpToHttpsSites.includes(host)) {
  58. window.location.href = window.location.href.replace("http://", "https://");
  59. } else if (protocol === "https:" && httpsToHttpSites.includes(host)) {
  60. window.location.href = window.location.href.replace("https://", "http://");
  61. }
  62. }
  63.  
  64. // 关键字替换功能
  65. function applyKeywordReplacements() {
  66. const url = window.location.href;
  67. const host = window.location.hostname;
  68.  
  69. if (keywordReplacements[host]) {
  70. let newUrl = url;
  71. keywordReplacements[host].forEach(replacement => {
  72. newUrl = newUrl.replace(new RegExp(replacement.keyword, "g"), replacement.replacement);
  73. });
  74.  
  75. if (newUrl !== url) window.location.href = newUrl;
  76. }
  77. }
  78.  
  79. performRedirect();
  80. applyKeywordReplacements();
  81. })();