Greasy Fork 还支持 简体中文。

Open the F**king URL Right Now

自动跳转某些网站不希望用户直达的外链

目前為 2024-11-23 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Open the F**king URL Right Now
  3. // @description 自动跳转某些网站不希望用户直达的外链
  4. // @author OldPanda
  5. // @match http*://c.pc.qq.com/*
  6. // @match http*://iphone.myzaker.com/zaker/link.php?*
  7. // @match http*://link.zhihu.com/?*
  8. // @match http*://t.cn/*
  9. // @match http*://www.360doc.cn/outlink.html?url=*
  10. // @match http://redir.yy.duowan.com/warning.php?url=*
  11. // @match http://www.360doc.com/content/*
  12. // @match https://afdian.com/link?target=*
  13. // @match https://afdian.net/link?target=*
  14. // @match https://bbs.acgrip.com/*
  15. // @match https://bbs.nga.cn/read.php?*
  16. // @match https://blog.51cto.com/transfer?*
  17. // @match https://blzxteam.com/gowild.htm?url=*
  18. // @match https://cloud.tencent.com/developer/tools/blog-entry?target=*
  19. // @match https://developers.weixin.qq.com/community/middlepage/href?href=*
  20. // @match https://docs.qq.com/scenario/link.html?u=*
  21. // @match https://docs.qq.com/scenario/link.html?url=*
  22. // @match https://game.bilibili.com/linkfilter/?url=*
  23. // @match https://gitee.com/link?target=*
  24. // @match https://hd.nowcoder.com/link.html?target=*
  25. // @match https://hellogithub.com/periodical/statistics/click?target=*
  26. // @match https://jump2.bdimg.com/safecheck/index?url=*
  27. // @match https://leetcode.cn/link/?target=*
  28. // @match https://link.csdn.net/?*target=*
  29. // @match https://link.juejin.cn/?target=*
  30. // @match https://link.ld246.com/forward?goto=*
  31. // @match https://link.logonews.cn/?*
  32. // @match https://link.uisdc.com/?redirect=*
  33. // @match https://mail.qq.com/cgi-bin/readtemplate*
  34. // @match https://mp.weixin.qq.com/s/*
  35. // @match https://mp.weixin.qq.com/s?*
  36. // @match https://nga.178.com/read.php?*
  37. // @match https://ref.gamer.com.tw/redir.php/url=*
  38. // @match https://ref.gamer.com.tw/redir.php?url=*
  39. // @match https://shimo.im/outlink/black?url=*
  40. // @match https://shimo.im/outlink/gray?url=*
  41. // @match https://sspai.com/link?target=*
  42. // @match https://steamcommunity.com/linkfilter/?url=*
  43. // @match https://steamcommunity.com/linkfilter/?u=*
  44. // @match https://support.qq.com/product/*/link-jump?jump=*
  45. // @match https://support.qq.com/products/*/link-jump?jump=*
  46. // @match https://t.me/iv?url=*
  47. // @match https://tieba.baidu.com/mo/q/checkurl?url=*
  48. // @match https://txc.qq.com/product/*/link-jump?jump=*
  49. // @match https://txc.qq.com/products/*/link-jump?jump=*
  50. // @match https://weibo.cn/sinaurl?*
  51. // @match https://weixin110.qq.com/cgi-bin/mmspamsupport-bin/newredirectconfirmcgi*
  52. // @match https://www.bookmarkearth.com/view/*
  53. // @match https://www.chinaz.com/go.shtml?url=*
  54. // @match https://www.coolapk.com/link?url=*
  55. // @match https://www.curseforge.com/linkout?remoteUrl=*
  56. // @match https://www.douban.com/link2/?url=*
  57. // @match https://www.gcores.com/link?target=*
  58. // @match https://www.instagram.com/linkshim/?u=*
  59. // @match https://www.jianshu.com/go-wild?*
  60. // @match https://www.kookapp.cn/go-wild.html?url=*
  61. // @match https://www.linkedin.com/safety/go?url=*
  62. // @match https://www.luogu.com.cn/discuss/*
  63. // @match https://www.luogu.com.cn/paste/*
  64. // @match https://www.nodeseek.com/jump?to=*
  65. // @match https://www.oschina.net/action/GoToLink?url=*
  66. // @match https://www.pixiv.net/jump.php?*
  67. // @match https://www.qcc.com/web/transfer-link?link=*
  68. // @match https://www.tianyancha.com/security?target=*
  69. // @match https://www.yuque.com/r/goto?url=*
  70. // @match https://*.infoq.cn/link?target=*
  71. // @match https://www.baike.com/redirect_link?url=*
  72. // @match https://www.youtube.com/redirect?*
  73. // @exclude https://mp.weixin.qq.com/cgi-bin/*
  74. // @version 1.14.1
  75. // @run-at document-idle
  76. // @namespace https://old-panda.com/
  77. // @require https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.1/jquery.min.js
  78. // @license GPLv3 License
  79. // ==/UserScript==
  80.  
  81. const $ = jQuery.noConflict(true);
  82.  
  83. /**
  84. * @enum {string}
  85. * @name fuckers
  86. * @description all link pattern needed deal with
  87. */
  88. const fuckers = {
  89. acgrip: { match: 'https://bbs.acgrip.com/', redirect: removeFwinDialog },
  90. afdian: { match: 'https://afdian.net/link?target=', redirect: "target" },
  91. afdian2: { match: 'https://afdian.com/link?target=', redirect: "target" },
  92. baike: { match: 'https://www.baike.com/redirect_link?url=', redirect: "url" },
  93. blzxteam: { match: 'https://blzxteam.com/gowild.htm?url=', redirect: function () { const url = $("div._2VEbEOHfDtVWiQAJxSIrVi_0").first().attr("title"); window.location.href = url } },
  94. bookmarkearth: { match: 'https://www.bookmarkearth.com/view/', redirect: function () { window.location.replace(document.querySelector("p.link").innerHTML) } },
  95. chinaz: { match: 'https://www.chinaz.com/go.shtml?url=', redirect: "url" },
  96. coolapk: { match: 'https://www.coolapk.com/link?url=', redirect: "url" },
  97. csdn: { match: 'https://link.csdn.net/?*target=', redirect: "target" },
  98. cto51: { match: 'https://blog.51cto.com/transfer?', redirect: function () { window.location.href = window.location.href.replace("https://blog.51cto.com/transfer?", "") } },
  99. curseforge: { match: 'https://www.curseforge.com/linkout?remoteUrl=', redirect: function() { redirect(decodeURIComponent(curURL), 'remoteUrl') } },
  100. dilian: { match: 'https://link.ld246.com/forward?goto=', redirect: "goto" },
  101. doc360_2: { match: 'http://www.360doc.cn/outlink.html?url=', redirect: "url" },
  102. doc360: { match: 'http://www.360doc.com/content/', redirect: function () { $("#articlecontent table tbody tr td#artContent").find("a").off("click") } },
  103. douban: { match: 'https://www.douban.com/link2/?url=', redirect: "url" },
  104. gamebilibili: { match: 'https://game.bilibili.com/linkfilter/?url=', redirect: "url" },
  105. gamertw: { match: 'https://ref.gamer.com.tw/redir.php/?url=', redirect: "url" },
  106. gamertw_2: { match: 'https://ref.gamer.com.tw/redir.php?url=', redirect: "url" },
  107. gcores: { match: 'https://www.gcores.com/link?target=', redirect: "target" },
  108. gitee: { match: 'https://gitee.com/link?target=', redirect: "target" },
  109. hellogithub: { match: 'https://hellogithub.com/periodical/statistics/click?target=', redirect: "target" },
  110. infoq: { match: 'https://(xie.infoq.cn/link|www.infoq.cn/link)?target=', redirect: "target", enableRegex: true },
  111. instagram: { match: 'https://www.instagram.com/linkshim/?u=', redirect: "url" },
  112. jianshu: { match: 'https://www.jianshu.com/go-wild?', redirect: "url" },
  113. juejin: { match: 'https://link.juejin.cn/?target=', redirect: "target" },
  114. kook: { match: 'https://www.kookapp.cn/go-wild.html?url=', redirect: "url" },
  115. leetcode: { match: 'https://leetcode.cn/link/?target', redirect: "target" },
  116. linkedin: { match: 'https://www.linkedin.com/safety/go?url=', redirect: "url" },
  117. logonews: { match: 'https://link.logonews.cn/?', redirect: "url" },
  118. luogu: { match: 'https://www.luogu.com.cn/paste/', redirect: function () { if (document.getElementById("url")) { window.location.href = $("#url").text() } } },
  119. luogu_2: { match: 'https://www.luogu.com.cn/discuss/', redirect: function () { if (document.getElementById("url")) { window.location.href = $("#url").text() } } },
  120. nga: { match: 'https://nga.178.com/read.php?', redirect: function () { $("#m_posts #m_posts_c a").prop("onclick", null).off("click") } },
  121. nga2: { match: 'https://bbs.nga.cn/read.php?', redirect: function () { $("#m_posts #m_posts_c a").prop("onclick", null).off("click") } },
  122. nodeseek: { match: 'https://www.nodeseek.com/jump?to=', redirect: "to" },
  123. nowcoder: { match: 'https://hd.nowcoder.com/link.html?target=', redirect: "target" },
  124. oschina: { match: 'https://www.oschina.net/action/GoToLink?url=', redirect: "url" },
  125. pixiv: { match: 'https://www.pixiv.net/jump.php?', redirect: function () { window.location.href = decodeURIComponent(curURL.match(/jump.php\?[url=]*(.*)/)[1].replace(/=$/, '')) } },
  126. qcc: { match: 'https://www.qcc.com/web/transfer-link?link=', redirect: "link" },
  127. qq: { match: 'https://c.pc.qq.com/(middleb|middlect|middlem|index).html', redirect: "pfurl", enableRegex: true },
  128. qq2: { match: 'https://c.pc.qq.com/(ios|pc|android).html', redirect: "url", enableRegex: true },
  129. qqdocs: { match: 'https://docs.qq.com/scenario/link.html?url=', redirect: "url" },
  130. qqmail: { match: 'https://mail.qq.com/cgi-bin/readtemplate', redirect: "gourl" },
  131. shimo: { match: 'https://shimo.im/outlink/black', redirect: "url" },
  132. shimo_2: { match: 'https://shimo.im/outlink/gray', redirect: "url" },
  133. sspai: { match: 'https://sspai.com/link?target=', redirect: "target" },
  134. steam: { match: 'https://steamcommunity.com/linkfilter/?url=', redirect: "url" },
  135. steam2: { match: 'https://steamcommunity.com/linkfilter/?u=', redirect: "u" },
  136. telegram: { match: 'https://t.me/iv?url=', redirect: "url" },
  137. tencentclouddev: { match: 'https://cloud.tencent.com/developer/tools/blog-entry?target=', redirect: "target" },
  138. tianyancha: { match: 'https://www.tianyancha.com/security?target=', redirect: "target" },
  139. tieba: { match: 'https://jump2.bdimg.com/safecheck/index?url=', redirect: function () { window.location.replace(document.getElementsByClassName('btn')[0].getAttribute('href')) } },
  140. tieba_2: { match: 'https://tieba.baidu.com/mo/q/checkurl?url=', redirect: "url" },
  141. txc: { match: 'https://(txc|support).qq.com/products?/(\\d+)/link-jump', enableRegex: true, redirect: 'jump' },
  142. uisdc: { match: 'https://link.uisdc.com/?redirect=', redirect: "redirect" },
  143. wechat1: { match: 'https://mp.weixin.qq.com/s/', redirect: enableURLs },
  144. wechat2: { match: 'https://weixin110.qq.com/cgi-bin/mmspamsupport-bin/newredirectconfirmcgi', redirect: function () { window.location.replace($(".weui-msg__desc").first().text()) } },
  145. wechat3: { match: 'https://mp.weixin.qq.com/s?', redirect: function () { let elem = $("#js_access_msg"); if (elem !== undefined && elem.attr("href") !== undefined) { window.location.replace(elem.attr("href")); } } },
  146. // https://t.cn/RgAKoPE
  147. // https://weibo.cn/sinaurl?luicode=10000011&lfid=230259&u=http%3A%2F%2Ft.cn%2FA6qHeVlf
  148. // https://weibo.cn/sinaurl?toasturl=https%3A%2F%2Ftime.geekbang.org%2F
  149. // https://weibo.cn/sinaurl?u=https%3A%2F%2Fwww.freebsd.org%2F
  150. weibo_1: { match: 'https://t.cn/', redirect: function () { const link = $(".wrap .link").first().text() || document.querySelector('.open-url').children[0].href; window.location.replace(link); } }, // 微博网页版
  151. weibo_2: { match: 'https://weibo.cn/sinaurl?u', redirect: "u" },
  152. weibo_3: { match: 'https://weibo.cn/sinaurl?toasturl', redirect: "toasturl" },
  153. weibo_4: { match: 'https://weibo.cn/sinaurl?', redirect: function () { const link = $(".wrap .link").first().text() || document.querySelector('.open-url').children[0].href; window.location.replace(link); } },
  154. weixindev: { match: 'https://developers.weixin.qq.com/community/middlepage/href?href=', redirect: "href" },
  155. yuque: { match: 'https://www.yuque.com/r/goto?url=', redirect: "url" },
  156. youtube: { match: 'https://www.youtube.com/redirect?', redirect: "q" },
  157. yy: { match: 'http://redir.yy.duowan.com/warning.php?url=', redirect: "url" },
  158. zaker: { match: 'http://iphone.myzaker.com/zaker/link.php?', redirect: function () { redirect(curURL, "url", true) } },
  159. // https://link.zhihu.com/?target=https%3A%2F%2Ftime.geekbang.org%2F
  160. // https://link.zhihu.com/?utm_oi=35221042888704&target=https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/import
  161. zhihu: { match: 'https://link.zhihu.com/?', redirect: "target" },
  162. }
  163.  
  164. const curURL = window.location.href;
  165. const urlPattern = /https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9@:%_\+.~#?&//=]*)/g;
  166.  
  167. /**
  168. * Return URL without "http://" or "https://" at the beginning
  169. * @param {String} str
  170. */
  171. function removeProtocol(str) {
  172. return str.replace(/^https?\??:\/\//gm, '');
  173. }
  174.  
  175. function rstrip(str, regex) {
  176. let i = str.length - 1;
  177. while (i >= 0) {
  178. if (!str[i].match(regex)) {
  179. break;
  180. }
  181. i--;
  182. }
  183. return str.substring(0, i + 1);
  184. }
  185.  
  186. /**
  187. * Split concatenated URL string into separate URLs.
  188. * @param {String} str
  189. */
  190. function splitMultiURLs(str) {
  191. //TODO: add comments
  192. let results = new Array();
  193. let entry = "";
  194. while (str.length > 0) {
  195. if (str.indexOf("http:") === -1 && str.indexOf("https:") === -1) {
  196. entry += str;
  197. str = "";
  198. results.push(rstrip(entry, /[@:%_\+~#?&=,$^\*]/g));
  199. break;
  200. }
  201.  
  202. if (str.startsWith("http:")) {
  203. entry += "http:";
  204. str = str.substring("http:".length);
  205. } else if (str.startsWith("https:")) {
  206. entry += "https:";
  207. str = str.substring("https:".length);
  208. } else {
  209. return results;
  210. }
  211.  
  212. let nextIndex = Math.min(
  213. str.indexOf("https:") === -1 ? Number.MAX_SAFE_INTEGER : str.indexOf("https:"),
  214. str.indexOf("http:") === -1 ? Number.MAX_SAFE_INTEGER : str.indexOf("http:")
  215. );
  216. if (nextIndex > 0) {
  217. entry += str.substring(0, nextIndex);
  218. str = str.substring(nextIndex);
  219. }
  220. results.push(rstrip(entry, /[@:%_\+~#?&=,$^\*]/g));
  221. entry = "";
  222. }
  223. return results;
  224. }
  225.  
  226. /**
  227. * Replace url with clickable `<a>` tag in html content.
  228. * @param {String} url
  229. */
  230. function replaceSingleURL(url) {
  231. $("#js_content").html((_, html) => {
  232. return html.replaceAll(url, `<a target="_blank" rel="noopener noreferrer" href="${url}">${url}</a>`);
  233. });
  234. }
  235.  
  236. /**
  237. * Make urls clickable again on Weixin Media Platform.
  238. */
  239. function enableURLs() {
  240. let existingLinks = new Set();
  241. $("a").each(function () {
  242. existingLinks.add(this.href);
  243. });
  244.  
  245. $("#js_content > section").each(function (_, obj) {
  246. // Don't do anything on code blocks
  247. let className = $(obj).attr('class');
  248. if (className != undefined && className.indexOf("code-snippet__js") != -1) {
  249. return;
  250. }
  251. let content = $(obj).text();
  252. let urls = content.matchAll(urlPattern);
  253. let replaced = new Set();
  254. for (let value of urls) {
  255. let urlStr = $.trim(value[0]);
  256. for (let url of splitMultiURLs(urlStr)) {
  257. if (!url || replaced.has(url) || url.includes("localhost") || url.includes("127.0.0.1") || existingLinks.has(url)) {
  258. continue;
  259. }
  260. if (url.endsWith(".") && url[url.length - 2].match(/\d/g)) {
  261. url = url.substring(0, url.length - 2);
  262. }
  263. replaceSingleURL(url);
  264. replaced.add(url);
  265. }
  266. }
  267. });
  268.  
  269. // Replace loading image with actual one
  270. $("img").each(function (_, obj) {
  271. if ($(obj)[0].currentSrc === "data:image/gif;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVQImWNgYGBgAAAABQABh6FO1AAAAABJRU5ErkJggg==") {
  272. $(obj).attr("src", $(obj)[0].dataset.src);
  273. } else {
  274. $(obj).attr("src", $(obj).attr("data-src"));
  275. $(obj).attr("style", "width: 100% !important; height: auto !important; display: initial; visibility: visible !important;");
  276. }
  277. });
  278. $("span.js_img_placeholder").remove();
  279. }
  280.  
  281. /**
  282. * Remove the fwin_dialog when clicking external links on Anime Subtitle Club
  283. */
  284. function removeFwinDialog() {
  285. $("a").each((_, elem) => {
  286. if (elem.href && elem.href.startsWith("http") && !elem.href.includes(window.location.host)) {
  287. elem.addEventListener("click", (_) => {
  288. window.open(elem.href, "_blank");
  289. hideMenu('fwin_dialog', 'dialog');
  290. })
  291. }
  292. })
  293. }
  294.  
  295. function redirect(fakeURLStr, trueURLParam, enableBase64 = false) {
  296. let fakeURL = new URL(fakeURLStr);
  297. let trueURL = fakeURL.searchParams.get(trueURLParam);
  298. if (trueURL.startsWith(fuckers.wechat1.match)) {
  299. // there could be multiple `&`s in url of a wechat link, so all of them
  300. // have to be included in the trueURL.
  301. trueURL = fakeURL.search.split(`${trueURLParam}=`).pop();
  302. } else {
  303. if (enableBase64) trueURL = window.atob(trueURL);
  304. if (trueURL.indexOf("http://") !== 0 && trueURL.indexOf("https://") !== 0) {
  305. trueURL = "https://" + trueURL;
  306. }
  307. }
  308. trueURL = decodeURIComponent(trueURL)
  309. window.location.replace(trueURL);
  310. }
  311.  
  312. /**
  313. * @function
  314. * @name match
  315. * @param {...string} pattern
  316. * @param {...boolean} enableRegex
  317. * @param {...boolean} checkProtocol
  318. * @description check if current URL matchs given patterns
  319. */
  320. function match(pattern, enableRegex = false, checkProtocol = false) {
  321. var curURLProto;
  322. if (checkProtocol) { curURLProto = curURL; }
  323. else {
  324. curURLProto = removeProtocol(curURL);
  325. pattern = removeProtocol(pattern);
  326. }
  327. if (enableRegex) {
  328. return curURLProto.search(pattern) > -1
  329. }
  330. else {
  331. return curURLProto.indexOf(pattern) === 0//Not Sure
  332. }
  333. }
  334.  
  335. (function () {
  336. 'use strict';
  337.  
  338. $(document).ready(function () {
  339. for (var i in fuckers) {
  340. if (match(fuckers[i].match, fuckers[i].enableRegex, fuckers[i].checkProtocol)) {
  341. switch (typeof (fuckers[i].redirect)) {
  342. case 'string':
  343. redirect(curURL, fuckers[i].redirect); break;
  344. case 'function':
  345. fuckers[i].redirect(); break;
  346. default:
  347. console.log(i + " redirect rule error!"); break;
  348. }
  349. }
  350. }
  351. });
  352.  
  353. })();