Open the F**king URL Right Now

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

当前为 2021-03-12 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Open the F**king URL Right Now
  3. // @description 自动跳转某些网站不希望用户直达的外链
  4. // @author OldPanda
  5. // @match http://t.cn/*
  6. // @match https://weibo.cn/sinaurl?*
  7. // @match https://www.jianshu.com/go-wild?*
  8. // @match https://link.zhihu.com/?*
  9. // @match https://www.douban.com/link2/?url=*
  10. // @match https://link.ld246.com/forward?goto=*
  11. // @match https://mp.weixin.qq.com/*
  12. // @match http://redir.yy.duowan.com/warning.php?url=*
  13. // @match https://weixin110.qq.com/cgi-bin/mmspamsupport-bin/newredirectconfirmcgi*
  14. // @match https://link.csdn.net/?target=*
  15. // @match https://steamcommunity.com/linkfilter/?url=*
  16. // @match https://game.bilibili.com/linkfilter/?url=*
  17. // @match https://www.oschina.net/action/GoToLink?url=*
  18. // @match https://developers.weixin.qq.com/community/middlepage/href?href=*
  19. // @match https://docs.qq.com/scenario/link.html?url=*
  20. // @match https://www.pixiv.net/jump.php?url=*
  21. // @match https://www.chinaz.com/go.shtml?url=*
  22. // @match http://www.360doc.com/content/*
  23. // @match https://nga.178.com/read.php?*
  24. // @match https://bbs.nga.cn/read.php?*
  25. // @version 0.8.1
  26. // @run-at document-idle
  27. // @namespace https://old-panda.com/
  28. // @require https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js
  29. // @license GPLv3 License
  30. // ==/UserScript==
  31.  
  32. (function () {
  33. 'use strict';
  34.  
  35. const curURL = window.location.href
  36.  
  37. function rstrip(str, regex) {
  38. let i = str.length - 1;
  39. while (i >= 0) {
  40. if (!str[i].match(regex)) {
  41. break;
  42. }
  43. i--;
  44. }
  45. return str.substring(0, i + 1);
  46. }
  47.  
  48. /**
  49. * Split concatenated URL string into separate URLs.
  50. * @param {String} str
  51. */
  52. function splitMultiURLs(str) {
  53. let results = new Array();
  54. let entry = "";
  55. while (str.length > 0) {
  56. if (str.indexOf("http:") === -1 && str.indexOf("https:") === -1) {
  57. entry += str;
  58. str = "";
  59. results.push(rstrip(entry, /[@:%_\+~#?&=,$^\*]/g));
  60. break;
  61. }
  62.  
  63. if (str.startsWith("http:")) {
  64. entry += "http:";
  65. str = str.substring("http:".length);
  66. } else if (str.startsWith("https:")) {
  67. entry += "https:";
  68. str = str.substring("https:".length);
  69. } else {
  70. return results;
  71. }
  72.  
  73. let nextIndex = Math.min(
  74. str.indexOf("https:") === -1 ? Number.MAX_SAFE_INTEGER : str.indexOf("https:"),
  75. str.indexOf("http:") === -1 ? Number.MAX_SAFE_INTEGER : str.indexOf("http:")
  76. );
  77. if (nextIndex > 0) {
  78. entry += str.substring(0, nextIndex);
  79. str = str.substring(nextIndex);
  80. }
  81. results.push(rstrip(entry, /[@:%_\+~#?&=,$^\*]/g));
  82. entry = "";
  83. }
  84. return results;
  85. }
  86.  
  87. /**
  88. * Replace url with clickable `<a>` tag in html content.
  89. * @param {String} url
  90. */
  91. function replaceSingleURL(url) {
  92. $("#js_content").html((_, html) => {
  93. return html.replace(url, `<a target="_blank" rel="noopener noreferrer" href="${url}">${url}</a>`);
  94. });
  95. }
  96.  
  97. /**
  98. * Make urls clickable again on Weixin Media Platform.
  99. */
  100. function enableURLs() {
  101. let existingLinks = new Set();
  102. $("a").each(function () {
  103. existingLinks.add(this.href);
  104. });
  105.  
  106. let content = $("#js_content").text();
  107. let urls = content.matchAll(/https?:\/\/(www\.)?[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_\+.~#?&//=]*)/g);
  108. let replaced = new Set();
  109. for (let value of urls) {
  110. let urlStr = $.trim(value[0]);
  111. for (let url of splitMultiURLs(urlStr)) {
  112. if (!url || replaced.has(url) || url.includes("localhost") || url.includes("127.0.0.1") || existingLinks.has(url)) {
  113. continue;
  114. }
  115. if (url.endsWith(".") && url[url.length - 2].match(/\d/g)) {
  116. url = url.substring(0, url.length - 2);
  117. }
  118. replaceSingleURL(url);
  119. replaced.add(url);
  120. }
  121. }
  122. }
  123.  
  124. function redirect(fakeURLStr, trueURLParam) {
  125. let fakeURL = new URL(fakeURLStr);
  126. let trueURL = fakeURL.searchParams.get(trueURLParam);
  127. window.location.replace(trueURL);
  128. }
  129.  
  130. /**
  131. * @function
  132. * @name match
  133. * @param {...string} patterns
  134. * @description check if current URL matchs given patterns
  135. */
  136. const match = (...patterns) => patterns.some(p => curURL.includes(p))
  137.  
  138. /**
  139. * @enum {string}
  140. * @name fuckers
  141. * @description all link pattern needed deal with
  142. */
  143. const fuckers = {
  144. weibo: 'http://t.cn/', // 微博网页版
  145. weibo2: 'https://weibo.cn/sinaurl?',
  146. // http://t.cn/RgAKoPE
  147. // https://weibo.cn/sinaurl?u=https%3A%2F%2Fwww.freebsd.org%2F
  148. // https://weibo.cn/sinaurl?toasturl=https%3A%2F%2Ftime.geekbang.org%2F
  149. // https://weibo.cn/sinaurl?luicode=10000011&lfid=230259&u=http%3A%2F%2Ft.cn%2FA6qHeVlf
  150. jianshu: 'https://www.jianshu.com/go-wild?',
  151. zhihu: 'https://link.zhihu.com/?',
  152. // https://link.zhihu.com/?target=https%3A%2F%2Ftime.geekbang.org%2F
  153. // https://link.zhihu.com/?target=https%3A%2F%2Fwww.freebsd.org%2F
  154. // https://link.zhihu.com/?utm_oi=35221042888704&target=https://developer.mozilla.org/zh-CN/docs/Web/JavaScript/Reference/Statements/import
  155. douban: 'https://www.douban.com/link2/?url=',
  156. dilian: 'https://link.ld246.com/forward?goto=',
  157. theWorst: 'https://mp.weixin.qq.com/',
  158. theWorst2: 'https://weixin110.qq.com/cgi-bin/mmspamsupport-bin/newredirectconfirmcgi',
  159. yy: 'http://redir.yy.duowan.com/warning.php?url=',
  160. csdn: 'https://link.csdn.net/?target=',
  161. steam: 'https://steamcommunity.com/linkfilter/?url=',
  162. gamebilibili: 'game.bilibili.com/linkfilter/?url=',
  163. oschina: 'https://www.oschina.net/action/GoToLink?url=',
  164. weixindev: 'https://developers.weixin.qq.com/community/middlepage/href?href=',
  165. qqdocs: 'https://docs.qq.com/scenario/link.html?url=',
  166. pixiv: 'https://www.pixiv.net/jump.php?url=',
  167. chinaz: 'https://www.chinaz.com/go.shtml?url=',
  168. doc360: 'http://www.360doc.com/content/',
  169. nga: 'https://nga.178.com/read.php?',
  170. nga2: 'https://bbs.nga.cn/read.php?'
  171. }
  172.  
  173. $(document).ready(function () {
  174. if (match(fuckers.weibo, fuckers.weibo2)) {
  175. const link = $(".wrap .link").first().text() || document.querySelector('.open-url').children[0].href
  176. window.location.replace(link);
  177. }
  178. if (match(fuckers.jianshu)) {
  179. redirect(curURL, "url");
  180. }
  181. if (match(fuckers.zhihu, fuckers.zhihu2)) {
  182. redirect(curURL, "target");
  183. }
  184. if (match(fuckers.douban)) {
  185. redirect(curURL, "url");
  186. }
  187. if (match(fuckers.dilian)) {
  188. redirect(curURL, "goto");
  189. }
  190. if (match(fuckers.theWorst)) {
  191. enableURLs();
  192. }
  193. if (match(fuckers.yy)) {
  194. redirect(curURL, "url");
  195. }
  196. if (match(fuckers.theWorst2)) {
  197. window.location.replace($(".weui-msg__desc").first().text());
  198. }
  199. if (match(fuckers.csdn)) {
  200. redirect(curURL, "target");
  201. }
  202. if (match(fuckers.steam)) {
  203. redirect(curURL, "url");
  204. }
  205. if (match(fuckers.gamebilibili)) {
  206. redirect(curURL, "url");
  207. }
  208. if (match(fuckers.oschina)) {
  209. redirect(curURL, "url");
  210. }
  211. if (match(fuckers.weixindev)) {
  212. redirect(curURL, "href");
  213. }
  214. if (match(fuckers.qqdocs)) {
  215. redirect(curURL, "url");
  216. }
  217. if (match(fuckers.pixiv)) {
  218. redirect(curURL, "url");
  219. }
  220. if (match(fuckers.chinaz)) {
  221. redirect(curURL, "url");
  222. }
  223. if (match(fuckers.doc360)) {
  224. $("#articlecontent table tbody tr td#artContent").find("a").off("click");
  225. }
  226. if (match(fuckers.nga, fuckers.nga2)) {
  227. $("#m_posts #m_posts_c a").prop("onclick", null).off("click");
  228. }
  229. });
  230.  
  231. })();