External Link Auto Redirect

redirect to the real URL directly when clicking on a link that contains a redirect URL. Please manually add this site when entering the redirect page the first time

  1. // ==UserScript==
  2. // @name External Link Auto Redirect
  3. // @name:zh-CN 外链自动重定向
  4. // @namespace http://tampermonkey.net/
  5. // @version 1.4.1
  6. // @description redirect to the real URL directly when clicking on a link that contains a redirect URL. Please manually add this site when entering the redirect page the first time
  7. // @description:zh-CN 点击包含重定向 URL 的链接时,直接跳转到到真实的 URL,首次进入跳转页面,请手动添加此站点
  8. // @author uiliugang
  9. // @run-at document-start
  10. // @match *://*/*
  11. // @grant GM_registerMenuCommand
  12. // @grant GM_getValue
  13. // @grant GM_setValue
  14. // @grant GM_deleteValue
  15. // @license MIT
  16. // ==/UserScript==
  17.  
  18. (function () {
  19. 'use strict';
  20.  
  21. const httpPattern = /http/g;
  22. const domain = window.location.hostname;
  23. const firstHttpExcludeWords = ['portal','proxy','player','vpn','search','api','convert','sorry', 'qrcode', 'aptcha', 'account', 'login', 'sign', 'auth', 'logout', 'register', 'upload', 'share', 'live', 'watch'];
  24. const secondHttpExcludeWords = ['.m3u8', '.flv', '.ts']
  25. const isChinese = checkLocalChineseLanguage();
  26. insertMenu();
  27.  
  28. function checkLocalChineseLanguage() {
  29. const chineseLanguages = ["zh", "zh-CN", "zh-HK", "zh-TW", "zh-MO", "zh-SG", "zh-MY"];
  30. const lang = navigator.language || navigator.userLanguage || "en-US";
  31. return chineseLanguages.includes(lang);
  32. }
  33.  
  34. function insertMenu(){
  35. const addDomain = ` ${isChinese ? "启用: "+ domain: "Enabled: "+ domain}`;
  36. const deleteDomain = ` ${isChinese ? "关闭: "+ domain: "Disabled: "+ domain}`;
  37. GM_registerMenuCommand(addDomain, function() {
  38. GM_setValue(domain, "1");
  39. });
  40. GM_registerMenuCommand(deleteDomain, function() {
  41. GM_deleteValue(domain);
  42. });
  43. }
  44.  
  45. function isAllowedWebsites(domain){
  46. if (GM_getValue(domain, null)=="1") {
  47. return true;
  48. }
  49. return false;
  50. }
  51.  
  52. function parseUrl(redirectURL) {
  53. let index = findSecondHttpPosition(redirectURL);
  54. if (index !== -1) {
  55. let realUrl = redirectURL.substring(index);
  56. let firstHttp = redirectURL.substring(0, index).toLowerCase();
  57. let secondHttp = realUrl.toLowerCase();
  58.  
  59. for (const ext of firstHttpExcludeWords) {
  60. if (firstHttp.includes(ext)) {
  61. console.log(`firstHttpExcludeWord: ${ext}`);
  62. return null;
  63. }
  64. }
  65.  
  66. for (const ext of secondHttpExcludeWords) {
  67. if (secondHttp.includes(ext)) {
  68. console.log(`secondHttpExcludeWord: ${ext}`);
  69. return null;
  70. }
  71. }
  72.  
  73. realUrl = decodeURIComponent(realUrl);
  74. if (isValidUrl(realUrl)) {
  75. return realUrl;
  76. }
  77. }
  78. return null;
  79. }
  80.  
  81. function findSecondHttpPosition(redirectURL) {
  82. let match;
  83. let position = -1;
  84. let count = 0;
  85.  
  86. while ((match = httpPattern.exec(redirectURL)) !== null) {
  87. count++;
  88. if (count === 2) {
  89. position = match.index;
  90. return position;
  91. }
  92. }
  93. return -1;
  94. }
  95.  
  96. function isValidUrl(realUrl) {
  97. try {
  98. let url = new URL(realUrl);
  99. return true;
  100. } catch (e) {
  101. return false;
  102. }
  103. }
  104.  
  105. document.addEventListener('click', function (e) {
  106. const element = e.target.closest('a[href]');
  107. if (isAllowedWebsites(domain) && element) {
  108. const parsedUrl = parseUrl(element.href);
  109. if (parsedUrl) {
  110. e.preventDefault();
  111. window.open(parsedUrl, '_blank');
  112. }
  113. }
  114. });
  115.  
  116. if (isAllowedWebsites(domain)){
  117. let parsedUrl = parseUrl(window.location.href);
  118. if (parsedUrl) {
  119. window.location.replace(parsedUrl);
  120. console.log(`parsed URL: ${parsedUrl}`);
  121. }
  122. }
  123. })();

QingJ © 2025

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