Rules-For-Quantumult-X/Scripts/AdBlock/Wechat/UnlockLink.js

166 lines
86 KiB
JavaScript
Raw Normal View History

2023-11-24 19:39:48 +08:00
let persisVal = read("UnblockURLinWeChat");
let useCache = persisVal.useCache === "true"; //是否在微信中用快照显示被封禁的链接
let forceRedirect = persisVal.forceRedirect === "true"; //是否在微信中进行强制重定向,允许的情况下可能出现循环重定向
let wechatExportKey = persisVal.wechatExportKey || ""; //微信的一个 key暂未研究如何生成测试中仅 macOS 微信打开链接跳转浏览器时会缺失,导致无法解析原始链接
if (typeof $argument != "undefined") {
let arg = Object.fromEntries($argument.split("&").map((item) => item.split("=")));
useCache = arg.useCache === "true";
forceRedirect = arg.forceRedirect === "ture";
2023-11-24 19:39:48 +08:00
}
const respBody = $response.body;
//const cacheURL = "https://webcache.googleusercontent.com/search?q=cache:";
const cacheURL = "https://web.archive.org/web/20991231999999/";
const alipayScheme = "alipays://platformapi/startapp?appId=20000067&url=";
const isQuanX = typeof $notify != "undefined";
const isSurgeiOS =
"undefined" !== typeof $environment &&
$environment["surge-version"] &&
$environment.system == "iOS";
const isLooniOS = typeof $loon != "undefined" && /iPhone/.test($loon);
const isStashiOS =
"undefined" !== typeof $environment &&
$environment["stash-version"] &&
$environment.system == "iOS";
const isShadowrocket = "undefined" !== typeof $rocket;
const isLanceX = "undefined" != typeof $native;
const redirectStatus = isQuanX ? "HTTP/1.1 302 Temporary Redirect" : 302;
const cgiDataReg = /var cgiData = ([\s\S]*);\s*<\/script>/;
let cgiData = JSON.parse(cgiDataReg.exec(respBody)[1].replace(/\\/g, ""));
// HTML entities by https://github.com/mathiasbynens/he
// prettier-ignore
(function(r){var e="object"==typeof exports&&exports,a="object"==typeof module&&module&&module.exports==e&&module,t="object"==typeof global&&global;t.global!==t&&t.window!==t||(r=t);var o=/[\uD800-\uDBFF][\uDC00-\uDFFF]/g,s=/[\x01-\x7F]/g,u=/[\x01-\t\x0B\f\x0E-\x1F\x7F\x81\x8D\x8F\x90\x9D\xA0-\uFFFF]/g,c=/<\u20D2|=\u20E5|>\u20D2|\u205F\u200A|\u219D\u0338|\u2202\u0338|\u2220\u20D2|\u2229\uFE00|\u222A\uFE00|\u223C\u20D2|\u223D\u0331|\u223E\u0333|\u2242\u0338|\u224B\u0338|\u224D\u20D2|\u224E\u0338|\u224F\u0338|\u2250\u0338|\u2261\u20E5|\u2264\u20D2|\u2265\u20D2|\u2266\u0338|\u2267\u0338|\u2268\uFE00|\u2269\uFE00|\u226A\u0338|\u226A\u20D2|\u226B\u0338|\u226B\u20D2|\u227F\u0338|\u2282\u20D2|\u2283\u20D2|\u228A\uFE00|\u228B\uFE00|\u228F\u0338|\u2290\u0338|\u2293\uFE00|\u2294\uFE00|\u22B4\u20D2|\u22B5\u20D2|\u22D8\u0338|\u22D9\u0338|\u22DA\uFE00|\u22DB\uFE00|\u22F5\u0338|\u22F9\u0338|\u2933\u0338|\u29CF\u0338|\u29D0\u0338|\u2A6D\u0338|\u2A70\u0338|\u2A7D\u0338|\u2A7E\u0338|\u2AA1\u0338|\u2AA2\u0338|\u2AAC\uFE00|\u2AAD\uFE00|\u2AAF\u0338|\u2AB0\u0338|\u2AC5\u0338|\u2AC6\u0338|\u2ACB\uFE00|\u2ACC\uFE00|\u2AFD\u20E5|[\xA0-\u0113\u0116-\u0122\u0124-\u012B\u012E-\u014D\u0150-\u017E\u0192\u01B5\u01F5\u0237\u02C6\u02C7\u02D8-\u02DD\u0311\u0391-\u03A1\u03A3-\u03A9\u03B1-\u03C9\u03D1\u03D2\u03D5\u03D6\u03DC\u03DD\u03F0\u03F1\u03F5\u03F6\u0401-\u040C\u040E-\u044F\u0451-\u045C\u045E\u045F\u2002-\u2005\u2007-\u2010\u2013-\u2016\u2018-\u201A\u201C-\u201E\u2020-\u2022\u2025\u2026\u2030-\u2035\u2039\u203A\u203E\u2041\u2043\u2044\u204F\u2057\u205F-\u2063\u20AC\u20DB\u20DC\u2102\u2105\u210A-\u2113\u2115-\u211E\u2122\u2124\u2127-\u2129\u212C\u212D\u212F-\u2131\u2133-\u2138\u2145-\u2148\u2153-\u215E\u2190-\u219B\u219D-\u21A7\u21A9-\u21AE\u21B0-\u21B3\u21B5-\u21B7\u21BA-\u21DB\u21DD\u21E4\u21E5\u21F5\u21FD-\u2205\u2207-\u2209\u220B\u220C\u220F-\u2214\u2216-\u2218\u221A\u221D-\u2238\u223A-\u2257\u2259\u225A\u225C\u225F-\u2262\u2264-\u228B\u228D-\u229B\u229D-\u22A5\u22A7-\u22B0\u22B2-\u22BB\u22BD-\u22DB\u22DE-\u22E3\u22E6-\u22F7\u22F9-\u22FE\u2305\u2306\u2308-\u2310\u2312\u2313\u2315\u2316\u231C-\u231F\u2322\u2323\u232D\u232E\u2336\u233D\u233F\u237C\u23B0\u23B1\u23B4-\u23B6\u23DC-\u23DF\u23E2\u23E7\u2423\u24C8\u2500\u2502\u250C\u2510\u2514\u2518\u251C\u2524\u252C\u2534\u253C\u2550-\u256C\u2580\u2584\u2588\u2591-\u2593\u25A1\u25AA\u25AB\u25AD\u25AE\u25B1\u25B3-\u25B5\u25B8\u25B9\u25BD-\u25BF\u25C2\u25C3\u25CA\u25CB\u25EC\u25EF\u25F8-\u25FC\u2605\u2606\u260E\u2640\u2642\u2660\u2663\u2665\u2666\u266A\u266D-\u266F\u2713\u2717\u2720\u2736\u2758\u2772\u2773\u27C8\u27C9\u27E6-\u27ED\u27F5-\u27FA\u27FC\u27FF\u2902-\u2905\u290C-\u2913\u2916\u2919-\u2920\u2923-\u292A\u2933\u2935-\u2939\u293C\u293D\u2945\u2948-\u294B\u294E-\u2976\u2978\u2979\u297B-\u297F\u2985\u2986\u298B-\u2996\u299A\u299C\u299D\u29A4-\u29B7\u29B9\u29BB\u29BC\u29BE-\u29C5\u29C9\u29CD-\u29D0\u29DC-\u29DE\u29E3-\u29E5\u29EB\u29F4\u29F6\u2A00-\u2A02\u2A04\u2A06\u2A0C\u2A0D\u2A10-\u2A17\u2A22-\u2A27\u2A29\u2A2A\u2A2D-\u2A31\u2A33-\u2A3C\u2A3F\u2A40\u2A42-\u2A4D\u2A50\u2A53-\u2A58\u2A5A-\u2A5D\u2A5F\u2A66\u2A6A\u2A6D-\u2A75\u2A77-\u2A9A\u2A9D-\u2AA2\u2AA4-\u2AB0\u2AB3-\u2AC8\u2ACB\u2ACC\u2ACF-\u2ADB\u2AE4\u2AE6-\u2AE9\u2AEB-\u2AF3\u2AFD\uFB00-\uFB04]|\uD835[\uDC9C\uDC9E\uDC9F\uDCA2\uDCA5\uDCA6\uDCA9-\uDCAC\uDCAE-\uDCB9\uDCBB\uDCBD-\uDCC3\uDCC5-\uDCCF\uDD04\uDD05\uDD07-\uDD0A\uDD0D-\uDD14\uDD16-\uDD1C\uDD1E-\uDD39\uDD3B-\uDD3E\uDD40-\uDD44\uDD46\uDD4A-\uDD50\uDD52-\uDD6B]/g,l={"­":"shy","":"zwnj","":"zwj","":"lrm","":"ic","":"it","":"af","":"rlm","":"ZeroWidthSpace","":"NoBreak","̑":"DownBreve","⃛":"tdot","⃜":"DotDot","\t":"Tab","\n":"NewLine","":"puncsp","":"MediumSpace","":"thinsp","":"hairsp","":"emsp13","":"ensp","":"emsp14","":"emsp","":"numsp"," ":"nbsp","":"ThickSpace","‾":"oline",_:"lowbar","":"dash","":"ndash","—":"mdash","―":"horbar",",":"comma",";":"semi","⁏":"bsemi",":":"colon","⩴":"Colone","!":"excl","¡":"iexcl","?":"quest","¿":"iquest",".":"period","‥":"nldr","…":"mldr","·":"middot","'":"apos","<EFBFBD>
2023-11-24 19:39:48 +08:00
// base64 by https://github.com/dankogai/js-base64
// prettier-ignore
!function(t,n){var r,e;"object"==typeof exports&&"undefined"!=typeof module?module.exports=n():"function"==typeof define&&define.amd?define(n):(r=t.Base64,(e=n()).noConflict=function(){return t.Base64=r,e},t.Meteor&&(Base64=e),t.Base64=e)}("undefined"!=typeof self?self:"undefined"!=typeof window?window:"undefined"!=typeof global?global:this,(function(){"use strict";var t,n="3.7.2",r="function"==typeof atob,e="function"==typeof btoa,o="function"==typeof Buffer,u="function"==typeof TextDecoder?new TextDecoder:void 0,i="function"==typeof TextEncoder?new TextEncoder:void 0,f=Array.prototype.slice.call("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="),c=(t={},f.forEach((function(n,r){return t[n]=r})),t),a=/^(?:[A-Za-z\d+\/]{4})*?(?:[A-Za-z\d+\/]{2}(?:==)?|[A-Za-z\d+\/]{3}=?)?$/,d=String.fromCharCode.bind(String),s="function"==typeof Uint8Array.from?Uint8Array.from.bind(Uint8Array):function(t,n){return void 0===n&&(n=function(t){return t}),new Uint8Array(Array.prototype.slice.call(t,0).map(n))},l=function(t){return t.replace(/=/g,"").replace(/[+\/]/g,(function(t){return"+"==t?"-":"_"}))},h=function(t){return t.replace(/[^A-Za-z0-9\+\/]/g,"")},p=function(t){for(var n,r,e,o,u="",i=t.length%3,c=0;c<t.length;){if((r=t.charCodeAt(c++))>255||(e=t.charCodeAt(c++))>255||(o=t.charCodeAt(c++))>255)throw new TypeError("invalid character found");u+=f[(n=r<<16|e<<8|o)>>18&63]+f[n>>12&63]+f[n>>6&63]+f[63&n]}return i?u.slice(0,i-3)+"===".substring(i):u},y=e?function(t){return btoa(t)}:o?function(t){return Buffer.from(t,"binary").toString("base64")}:p,A=o?function(t){return Buffer.from(t).toString("base64")}:function(t){for(var n=[],r=0,e=t.length;r<e;r+=4096)n.push(d.apply(null,t.subarray(r,r+4096)));return y(n.join(""))},b=function(t,n){return void 0===n&&(n=!1),n?l(A(t)):A(t)},g=function(t){if(t.length<2)return(n=t.charCodeAt(0))<128?t:n<2048?d(192|n>>>6)+d(128|63&n):d(224|n>>>12&15)+d(128|n>>>6&63)+d(128|63&n);var n=65536+1024*(t.charCodeAt(0)-55296)+(t.charCodeAt(1)-56320);return d(240|n>>>18&7)+d(128|n>>>12&63)+d(128|n>>>6&63)+d(128|63&n)},B=/[\uD800-\uDBFF][\uDC00-\uDFFFF]|[^\x00-\x7F]/g,x=function(t){return t.replace(B,g)},C=o?function(t){return Buffer.from(t,"utf8").toString("base64")}:i?function(t){return A(i.encode(t))}:function(t){return y(x(t))},m=function(t,n){return void 0===n&&(n=!1),n?l(C(t)):C(t)},v=function(t){return m(t,!0)},U=/[\xC0-\xDF][\x80-\xBF]|[\xE0-\xEF][\x80-\xBF]{2}|[\xF0-\xF7][\x80-\xBF]{3}/g,F=function(t){switch(t.length){case 4:var n=((7&t.charCodeAt(0))<<18|(63&t.charCodeAt(1))<<12|(63&t.charCodeAt(2))<<6|63&t.charCodeAt(3))-65536;return d(55296+(n>>>10))+d(56320+(1023&n));case 3:return d((15&t.charCodeAt(0))<<12|(63&t.charCodeAt(1))<<6|63&t.charCodeAt(2));default:return d((31&t.charCodeAt(0))<<6|63&t.charCodeAt(1))}},w=function(t){return t.replace(U,F)},S=function(t){if(t=t.replace(/\s+/g,""),!a.test(t))throw new TypeError("malformed base64.");t+="==".slice(2-(3&t.length));for(var n,r,e,o="",u=0;u<t.length;)n=c[t.charAt(u++)]<<18|c[t.charAt(u++)]<<12|(r=c[t.charAt(u++)])<<6|(e=c[t.charAt(u++)]),o+=64===r?d(n>>16&255):64===e?d(n>>16&255,n>>8&255):d(n>>16&255,n>>8&255,255&n);return o},E=r?function(t){return atob(h(t))}:o?function(t){return Buffer.from(t,"base64").toString("binary")}:S,D=o?function(t){return s(Buffer.from(t,"base64"))}:function(t){return s(E(t),(function(t){return t.charCodeAt(0)}))},R=function(t){return D(T(t))},z=o?function(t){return Buffer.from(t,"base64").toString("utf8")}:u?function(t){return u.decode(D(t))}:function(t){return w(E(t))},T=function(t){return h(t.replace(/[-_]/g,(function(t){return"-"==t?"+":"/"})))},Z=function(t){return z(T(t))},j=function(t){return{value:t,enumerable:!1,writable:!0,configurable:!0}},I=function(){var t=function(t,n){return Object.defineProperty(String.prototype,t,j(n))};t("fromBase64",(function(){return Z(this)})),t("toBase64",(function(t){return m(this,t)})),t("toBase64URI",(function(){return m(this,!0)})),t("toBase64URL",(function(){return m(this,!0)})),t("toUint8Array",(function(){return R(this)}))},O=function(){var t=function(t,n){ret
2023-11-24 19:39:48 +08:00
if (cgiData.type === "gray" || cgiData.type === "newgray" || cgiData.type === "empty") {
let trueURL = he.decode(
`${cgiData.hasOwnProperty("url") ? cgiData.url : /http(.*)/.exec(cgiData.desc)[0]}`
);
trueURL = trueURL.indexOf("http") == 0 ? trueURL : "http://" + trueURL;
if (/qr\.alipay/.test(trueURL)) {
notify("", "点击跳转到支付宝打开", trueURL, alipayScheme + encodeURIComponent(trueURL));
2023-11-24 19:39:48 +08:00
$done({});
} else {
if (trueURL.includes('https://spotify.link')) {
const pattern = /\$full_url=([^&]+)/;
trueURL = decodeURIComponent(trueURL).match(pattern)[1];
}
notify("", "点击跳转到浏览器打开", trueURL, trueURL);
if (forceRedirect) {
2023-11-24 19:39:48 +08:00
let redirect = {
status: redirectStatus,
headers: {
Location: trueURL,
},
2023-11-24 19:39:48 +08:00
};
if (isQuanX) redirect.body = respBody;
$done(redirect);
} else $done({});
}
} else if (cgiData.type === "block") {
!(async () => {
let url = cgiData.btns[0].url.replace("newreadtemplate", "redirecthelpcgi");
if (!/exportkey=(.+)/.test(url)) {
if (wechatExportKey) {
url += wechatExportKey;
} else {
$done({});
}
2023-11-24 19:39:48 +08:00
}
await get(url).then((resp) => {
let obj = JSON.parse(resp.body);
if (obj.hasOwnProperty("btns")) {
let trueURL = decodeURIComponent(/url=(.*)/.exec(obj.btns[0].url)[1]).replace(
/&block_?type(.*)/,
""
);
trueURL = trueURL.includes(".") ? trueURL : Base64.decode(trueURL);
trueURL = trueURL.indexOf("http") == 0 ? trueURL : "http://" + trueURL;
if (!trueURL.includes("web.archive.org/web")) {
notify("", "点击跳转到浏览器打开", trueURL, trueURL);
if (useCache) {
let cacheLink = cacheURL + trueURL;
let redirect = {
status: redirectStatus,
headers: {
Location: cacheLink,
},
};
if (isQuanX) redirect.body = respBody;
$done(redirect);
} else {
$done({});
}
} else {
$done({});
}
resolve(trueURL);
}
$done({});
resolve();
});
})();
2023-11-24 19:39:48 +08:00
} else {
$done({});
2023-11-24 19:39:48 +08:00
}
function notify(title = "", subtitle = "", content = "", open_url) {
if (isQuanX && /iOS/.test($environment.version)) {
let opts = {};
if (open_url) opts["open-url"] = open_url;
if (JSON.stringify(opts) == "{}") {
$notify(title, subtitle, content);
} else {
$notify(title, subtitle, content, opts);
}
} else if (isSurgeiOS || isStashiOS || isLanceX) {
let opts = {};
if (open_url) opts["url"] = open_url;
if (JSON.stringify(opts) == "{}") {
$notification.post(title, subtitle, content);
} else {
$notification.post(title, subtitle, content, opts);
}
} else if (isLooniOS) {
let opts = {};
if (open_url) opts["openUrl"] = open_url;
if (JSON.stringify(opts) == "{}") {
$notification.post(title, subtitle, content);
} else {
$notification.post(title, subtitle, content, opts);
}
} else if (isShadowrocket) {
if (open_url) {
$notification.post(title, subtitle, content, open_url);
} else {
$notification.post(title, subtitle, content);
}
2023-11-24 19:39:48 +08:00
}
}
function get(options) {
if (isQuanX) {
if (typeof options == "string") options = { url: options, method: "GET" };
return $task.fetch(options);
} else {
return new Promise((resolve, reject) => {
$httpClient.get(options, (err, response, body) => {
if (err) reject(err);
else resolve({ statusCode: response.status, headers: response.headers, body });
});
});
}
2023-11-24 19:39:48 +08:00
}
function read(key) {
if (typeof $notify != "undefined") {
return JSON.parse($prefs.valueForKey(key) || "{}");
} else {
return JSON.parse($persistentStore.read(key) || "{}");
}
2023-11-24 19:39:48 +08:00
}