@whitesev/utils 2.9.4 → 2.9.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.umd.js CHANGED
@@ -7,7 +7,7 @@
7
7
  class ColorConversion {
8
8
  /**
9
9
  * 判断是否是16进制颜色
10
- * @param str
10
+ * @param str 十六进制颜色,如`#000000`
11
11
  */
12
12
  isHex(str) {
13
13
  if (typeof str !== "string") {
@@ -22,8 +22,8 @@
22
22
  * 16进制颜色转rgba
23
23
  *
24
24
  * 例如:`#ff0000` 转为 `rgba(123,123,123, 0.4)`
25
- * @param hex
26
- * @param opacity
25
+ * @param hex 十六进制颜色,如`#000000`
26
+ * @param opacity 透明度,0~1
27
27
  */
28
28
  hexToRgba(hex, opacity) {
29
29
  if (!this.isHex(hex)) {
@@ -35,16 +35,16 @@
35
35
  }
36
36
  /**
37
37
  * hex转rgb
38
- * @param str
38
+ * @param hex 十六进制颜色,如`#000000`
39
39
  */
40
- hexToRgb(str) {
41
- if (!this.isHex(str)) {
42
- throw new TypeError(`输入错误的hex:${str}`);
40
+ hexToRgb(hex) {
41
+ if (!this.isHex(hex)) {
42
+ throw new TypeError(`输入错误的hex:${hex}`);
43
43
  }
44
44
  /* replace替换查找的到的字符串 */
45
- str = str.replace("#", "");
45
+ hex = hex.replace("#", "");
46
46
  /* match得到查询数组 */
47
- const hxs = str.match(/../g);
47
+ const hxs = hex.match(/../g);
48
48
  for (let index = 0; index < 3; index++) {
49
49
  const value = parseInt(hxs[index], 16);
50
50
  Reflect.set(hxs, index, value);
@@ -53,9 +53,10 @@
53
53
  }
54
54
  /**
55
55
  * rgb转hex
56
- * @param redValue
57
- * @param greenValue
58
- * @param blueValue
56
+ * @param redValue 红色值
57
+ * @param greenValue 绿色值
58
+ * @param blueValue 蓝色值
59
+ * @returns hex
59
60
  */
60
61
  rgbToHex(redValue, greenValue, blueValue) {
61
62
  /* 验证输入的rgb值是否合法 */
@@ -72,30 +73,42 @@
72
73
  }
73
74
  /**
74
75
  * 获取颜色变暗或亮
75
- * @param color 颜色
76
- * @param level 0~1.0
76
+ * @param color hex颜色,如`#000000`
77
+ * @param level 0~1.0 系数越大,颜色越变暗
77
78
  */
78
79
  getDarkColor(color, level) {
79
80
  if (!this.isHex(color)) {
80
81
  throw new TypeError(`输入错误的hex:${color}`);
81
82
  }
83
+ if (typeof level !== "number") {
84
+ level = Number(level);
85
+ }
86
+ if (isNaN(level)) {
87
+ throw new TypeError(`输入错误的level:${level}`);
88
+ }
82
89
  const rgbc = this.hexToRgb(color);
83
90
  for (let index = 0; index < 3; index++) {
84
91
  const rgbcItemValue = rgbc[index];
85
- const value = Math.floor(Number(rgbcItemValue) * (1 - Number(level)));
92
+ const value = Math.floor(Number(rgbcItemValue) * (1 - level));
86
93
  Reflect.set(rgbc, index, value);
87
94
  }
88
95
  return this.rgbToHex(rgbc[0], rgbc[1], rgbc[2]);
89
96
  }
90
97
  /**
91
98
  * 获取颜色变亮
92
- * @param color 颜色
93
- * @param level 0~1.0
99
+ * @param color hex颜色,如`#000000`
100
+ * @param level 0~1.0 系数越大,颜色越变亮
94
101
  */
95
102
  getLightColor(color, level) {
96
103
  if (!this.isHex(color)) {
97
104
  throw new TypeError(`输入错误的hex:${color}`);
98
105
  }
106
+ if (typeof level !== "number") {
107
+ level = Number(level);
108
+ }
109
+ if (isNaN(level)) {
110
+ throw new TypeError(`输入错误的level:${level}`);
111
+ }
99
112
  const rgbc = this.hexToRgb(color);
100
113
  for (let index = 0; index < 3; index++) {
101
114
  const rgbcItemValue = Number(rgbc[index]);
@@ -579,7 +592,7 @@
579
592
  }
580
593
  }
581
594
  /**
582
- * 获取多组Cookie
595
+ * 获取多组Cookie
583
596
  * @param option 配置
584
597
  **/
585
598
  getList(option) {
@@ -692,7 +705,8 @@
692
705
  }
693
706
  }
694
707
  /**
695
- * 解析cookie字符串
708
+ * 解析cookie字符串,按`;`分割
709
+ *
696
710
  * 例如:document.cookie
697
711
  * @param cookieStr
698
712
  */
@@ -717,1001 +731,981 @@
717
731
  }
718
732
  }
719
733
 
720
- /* eslint-disable */
721
- // ==UserScript==
722
- // @name ajaxHooker
723
- // @author cxxjackie
724
- // @version 1.4.8
725
- // @supportURL https://bbs.tampermonkey.net.cn/thread-3284-1-1.html
726
- // @license GNU LGPL-3.0
727
- // ==/UserScript==
728
-
729
- const ajaxHooker = function () {
730
- const version = "1.4.8";
731
- const hookInst = {
732
- hookFns: [],
733
- filters: [],
734
- };
735
- const win = window.unsafeWindow || document.defaultView || window;
736
- let winAh = win.__ajaxHooker;
737
- const resProto = win.Response.prototype;
738
- const xhrResponses = ["response", "responseText", "responseXML"];
739
- const fetchResponses = ["arrayBuffer", "blob", "formData", "json", "text"];
740
- const xhrExtraProps = ["responseType", "timeout", "withCredentials"];
741
- const fetchExtraProps = [
742
- "cache",
743
- "credentials",
744
- "integrity",
745
- "keepalive",
746
- "mode",
747
- "priority",
748
- "redirect",
749
- "referrer",
750
- "referrerPolicy",
751
- "signal",
752
- ];
753
- const xhrAsyncEvents = ["readystatechange", "load", "loadend"];
754
- const getType = {}.toString.call.bind({}.toString);
755
- const getDescriptor = Object.getOwnPropertyDescriptor.bind(Object);
756
- const emptyFn = () => {};
757
- const errorFn = (e) => console.error(e);
758
- function isThenable(obj) {
759
- return obj && ["object", "function"].includes(typeof obj) && typeof obj.then === "function";
760
- }
761
- function catchError(fn, ...args) {
762
- try {
763
- const result = fn(...args);
764
- if (isThenable(result)) return result.then(null, errorFn);
765
- return result;
766
- } catch (err) {
767
- console.error(err);
768
- }
769
- }
770
- function defineProp(obj, prop, getter, setter) {
771
- Object.defineProperty(obj, prop, {
772
- configurable: true,
773
- enumerable: true,
774
- get: getter,
775
- set: setter,
776
- });
777
- }
778
- function readonly(obj, prop, value = obj[prop]) {
779
- defineProp(obj, prop, () => value, emptyFn);
780
- }
781
- function writable(obj, prop, value = obj[prop]) {
782
- Object.defineProperty(obj, prop, {
783
- configurable: true,
784
- enumerable: true,
785
- writable: true,
786
- value: value,
787
- });
788
- }
789
- function parseHeaders(obj) {
790
- const headers = {};
791
- switch (getType(obj)) {
792
- case "[object String]":
793
- for (const line of obj.trim().split(/[\r\n]+/)) {
794
- const [header, value] = line.split(/(?<=^[^:]+)\s*:\s*/);
795
- if (!value) continue;
796
- const lheader = header.toLowerCase();
797
- headers[lheader] = lheader in headers ? `${headers[lheader]}, ${value}` : value;
798
- }
799
- break;
800
- case "[object Headers]":
801
- for (const [key, val] of obj) {
802
- headers[key] = val;
803
- }
804
- break;
805
- case "[object Object]":
806
- return { ...obj };
807
- }
808
- return headers;
809
- }
810
- function stopImmediatePropagation() {
811
- this.ajaxHooker_isStopped = true;
812
- }
813
- class SyncThenable {
814
- then(fn) {
815
- fn && fn();
816
- return new SyncThenable();
817
- }
818
- }
819
- class AHRequest {
820
- constructor(request) {
821
- this.request = request;
822
- this.requestClone = { ...this.request };
823
- }
824
- _recoverRequestKey(key) {
825
- if (key in this.requestClone) this.request[key] = this.requestClone[key];
826
- else delete this.request[key];
827
- }
828
- shouldFilter(filters) {
829
- const { type, url, method, async } = this.request;
830
- return (
831
- filters.length &&
832
- !filters.find((obj) => {
833
- switch (true) {
834
- case obj.type && obj.type !== type:
835
- case getType(obj.url) === "[object String]" && !url.includes(obj.url):
836
- case getType(obj.url) === "[object RegExp]" && !obj.url.test(url):
837
- case obj.method && obj.method.toUpperCase() !== method.toUpperCase():
838
- case "async" in obj && obj.async !== async:
839
- return false;
840
- }
841
- return true;
842
- })
843
- );
844
- }
845
- waitForRequestKeys() {
846
- if (!this.request.async) {
847
- win.__ajaxHooker.hookInsts.forEach(({ hookFns, filters }) => {
848
- if (this.shouldFilter(filters)) return;
849
- hookFns.forEach((fn) => {
850
- if (getType(fn) === "[object Function]") catchError(fn, this.request);
851
- });
852
- for (const key in this.request) {
853
- if (isThenable(this.request[key])) this._recoverRequestKey(key);
854
- }
855
- });
856
- return new SyncThenable();
857
- }
858
- const promises = [];
859
- const ignoreKeys = new Set(["type", "async", "response"]);
860
- win.__ajaxHooker.hookInsts.forEach(({ hookFns, filters }) => {
861
- if (this.shouldFilter(filters)) return;
862
- promises.push(
863
- Promise.all(hookFns.map((fn) => catchError(fn, this.request))).then(() => {
864
- const requestKeys = [];
865
- for (const key in this.request) !ignoreKeys.has(key) && requestKeys.push(key);
866
- return Promise.all(
867
- requestKeys.map((key) =>
868
- Promise.resolve(this.request[key]).then(
869
- (val) => (this.request[key] = val),
870
- () => this._recoverRequestKey(key)
871
- )
872
- )
873
- );
874
- })
875
- );
876
- });
877
- return Promise.all(promises);
878
- }
879
- waitForResponseKeys(response) {
880
- const responseKeys = this.request.type === "xhr" ? xhrResponses : fetchResponses;
881
- if (!this.request.async) {
882
- if (getType(this.request.response) === "[object Function]") {
883
- catchError(this.request.response, response);
884
- responseKeys.forEach((key) => {
885
- if ("get" in getDescriptor(response, key) || isThenable(response[key])) {
886
- delete response[key];
887
- }
888
- });
889
- }
890
- return new SyncThenable();
891
- }
892
- return Promise.resolve(catchError(this.request.response, response)).then(() =>
893
- Promise.all(
894
- responseKeys.map((key) => {
895
- const descriptor = getDescriptor(response, key);
896
- if (descriptor && "value" in descriptor) {
897
- return Promise.resolve(descriptor.value).then(
898
- (val) => (response[key] = val),
899
- () => delete response[key]
900
- );
901
- } else {
902
- delete response[key];
903
- }
904
- })
905
- )
906
- );
907
- }
908
- }
909
- const proxyHandler = {
910
- get(target, prop) {
911
- const descriptor = getDescriptor(target, prop);
912
- if (descriptor && !descriptor.configurable && !descriptor.writable && !descriptor.get)
913
- return target[prop];
914
- const ah = target.__ajaxHooker;
915
- if (ah && ah.proxyProps) {
916
- if (prop in ah.proxyProps) {
917
- const pDescriptor = ah.proxyProps[prop];
918
- if ("get" in pDescriptor) return pDescriptor.get();
919
- if (typeof pDescriptor.value === "function") return pDescriptor.value.bind(ah);
920
- return pDescriptor.value;
921
- }
922
- if (typeof target[prop] === "function") return target[prop].bind(target);
923
- }
924
- return target[prop];
925
- },
926
- set(target, prop, value) {
927
- const descriptor = getDescriptor(target, prop);
928
- if (descriptor && !descriptor.configurable && !descriptor.writable && !descriptor.set) return true;
929
- const ah = target.__ajaxHooker;
930
- if (ah && ah.proxyProps && prop in ah.proxyProps) {
931
- const pDescriptor = ah.proxyProps[prop];
932
- pDescriptor.set ? pDescriptor.set(value) : (pDescriptor.value = value);
933
- } else {
934
- target[prop] = value;
935
- }
936
- return true;
937
- },
938
- };
939
- class XhrHooker {
940
- constructor(xhr) {
941
- const ah = this;
942
- Object.assign(ah, {
943
- originalXhr: xhr,
944
- proxyXhr: new Proxy(xhr, proxyHandler),
945
- resThenable: new SyncThenable(),
946
- proxyProps: {},
947
- proxyEvents: {},
948
- });
949
- xhr.addEventListener("readystatechange", (e) => {
950
- if (ah.proxyXhr.readyState === 4 && ah.request && typeof ah.request.response === "function") {
951
- const response = {
952
- finalUrl: ah.proxyXhr.responseURL,
953
- status: ah.proxyXhr.status,
954
- responseHeaders: parseHeaders(ah.proxyXhr.getAllResponseHeaders()),
955
- };
956
- const tempValues = {};
957
- for (const key of xhrResponses) {
958
- try {
959
- tempValues[key] = ah.originalXhr[key];
960
- } catch (err) {}
961
- defineProp(
962
- response,
963
- key,
964
- () => {
965
- return (response[key] = tempValues[key]);
966
- },
967
- (val) => {
968
- delete response[key];
969
- response[key] = val;
970
- }
971
- );
972
- }
973
- ah.resThenable = new AHRequest(ah.request).waitForResponseKeys(response).then(() => {
974
- for (const key of xhrResponses) {
975
- ah.proxyProps[key] = {
976
- get: () => {
977
- if (!(key in response)) response[key] = tempValues[key];
978
- return response[key];
979
- },
980
- };
981
- }
982
- });
983
- }
984
- ah.dispatchEvent(e);
985
- });
986
- xhr.addEventListener("load", (e) => ah.dispatchEvent(e));
987
- xhr.addEventListener("loadend", (e) => ah.dispatchEvent(e));
988
- for (const evt of xhrAsyncEvents) {
989
- const onEvt = "on" + evt;
990
- ah.proxyProps[onEvt] = {
991
- get: () => ah.proxyEvents[onEvt] || null,
992
- set: (val) => ah.addEvent(onEvt, val),
993
- };
994
- }
995
- for (const method of ["setRequestHeader", "addEventListener", "removeEventListener", "open", "send"]) {
996
- ah.proxyProps[method] = { value: ah[method] };
997
- }
998
- }
999
- toJSON() {} // Converting circular structure to JSON
1000
- addEvent(type, event) {
1001
- if (type.startsWith("on")) {
1002
- this.proxyEvents[type] = typeof event === "function" ? event : null;
1003
- } else {
1004
- if (typeof event === "object" && event !== null) event = event.handleEvent;
1005
- if (typeof event !== "function") return;
1006
- this.proxyEvents[type] = this.proxyEvents[type] || new Set();
1007
- this.proxyEvents[type].add(event);
1008
- }
1009
- }
1010
- removeEvent(type, event) {
1011
- if (type.startsWith("on")) {
1012
- this.proxyEvents[type] = null;
1013
- } else {
1014
- if (typeof event === "object" && event !== null) event = event.handleEvent;
1015
- this.proxyEvents[type] && this.proxyEvents[type].delete(event);
1016
- }
1017
- }
1018
- dispatchEvent(e) {
1019
- e.stopImmediatePropagation = stopImmediatePropagation;
1020
- defineProp(e, "target", () => this.proxyXhr);
1021
- defineProp(e, "currentTarget", () => this.proxyXhr);
1022
- defineProp(e, "srcElement", () => this.proxyXhr);
1023
- this.proxyEvents[e.type] &&
1024
- this.proxyEvents[e.type].forEach((fn) => {
1025
- this.resThenable.then(() => !e.ajaxHooker_isStopped && fn.call(this.proxyXhr, e));
1026
- });
1027
- if (e.ajaxHooker_isStopped) return;
1028
- const onEvent = this.proxyEvents["on" + e.type];
1029
- onEvent && this.resThenable.then(onEvent.bind(this.proxyXhr, e));
1030
- }
1031
- setRequestHeader(header, value) {
1032
- this.originalXhr.setRequestHeader(header, value);
1033
- if (!this.request) return;
1034
- const headers = this.request.headers;
1035
- headers[header] = header in headers ? `${headers[header]}, ${value}` : value;
1036
- }
1037
- addEventListener(...args) {
1038
- if (xhrAsyncEvents.includes(args[0])) {
1039
- this.addEvent(args[0], args[1]);
1040
- } else {
1041
- this.originalXhr.addEventListener(...args);
1042
- }
1043
- }
1044
- removeEventListener(...args) {
1045
- if (xhrAsyncEvents.includes(args[0])) {
1046
- this.removeEvent(args[0], args[1]);
1047
- } else {
1048
- this.originalXhr.removeEventListener(...args);
1049
- }
1050
- }
1051
- open(method, url, async = true, ...args) {
1052
- this.request = {
1053
- type: "xhr",
1054
- url: url.toString(),
1055
- method: method.toUpperCase(),
1056
- abort: false,
1057
- headers: {},
1058
- data: null,
1059
- response: null,
1060
- async: !!async,
1061
- };
1062
- this.openArgs = args;
1063
- this.resThenable = new SyncThenable();
1064
- ["responseURL", "readyState", "status", "statusText", ...xhrResponses].forEach((key) => {
1065
- delete this.proxyProps[key];
1066
- });
1067
- return this.originalXhr.open(method, url, async, ...args);
1068
- }
1069
- send(data) {
1070
- const ah = this;
1071
- const xhr = ah.originalXhr;
1072
- const request = ah.request;
1073
- if (!request) return xhr.send(data);
1074
- request.data = data;
1075
- new AHRequest(request).waitForRequestKeys().then(() => {
1076
- if (request.abort) {
1077
- if (typeof request.response === "function") {
1078
- Object.assign(ah.proxyProps, {
1079
- responseURL: { value: request.url },
1080
- readyState: { value: 4 },
1081
- status: { value: 200 },
1082
- statusText: { value: "OK" },
1083
- });
1084
- xhrAsyncEvents.forEach((evt) => xhr.dispatchEvent(new Event(evt)));
1085
- }
1086
- } else {
1087
- xhr.open(request.method, request.url, request.async, ...ah.openArgs);
1088
- for (const header in request.headers) {
1089
- xhr.setRequestHeader(header, request.headers[header]);
1090
- }
1091
- for (const prop of xhrExtraProps) {
1092
- if (prop in request) xhr[prop] = request[prop];
1093
- }
1094
- xhr.send(request.data);
1095
- }
1096
- });
1097
- }
1098
- }
1099
- function fakeXHR() {
1100
- const xhr = new winAh.realXHR();
1101
- if ("__ajaxHooker" in xhr) console.warn("检测到不同版本的ajaxHooker,可能发生冲突!");
1102
- xhr.__ajaxHooker = new XhrHooker(xhr);
1103
- return xhr.__ajaxHooker.proxyXhr;
1104
- }
1105
- fakeXHR.prototype = win.XMLHttpRequest.prototype;
1106
- Object.keys(win.XMLHttpRequest).forEach((key) => (fakeXHR[key] = win.XMLHttpRequest[key]));
1107
- function fakeFetch(url, options = {}) {
1108
- if (!url) return winAh.realFetch.call(win, url, options);
1109
- return new Promise(async (resolve, reject) => {
1110
- const init = {};
1111
- if (getType(url) === "[object Request]") {
1112
- init.method = url.method;
1113
- init.headers = url.headers;
1114
- if (url.body) init.body = await url.arrayBuffer();
1115
- for (const prop of fetchExtraProps) init[prop] = url[prop];
1116
- url = url.url;
1117
- }
1118
- url = url.toString();
1119
- Object.assign(init, options);
1120
- init.method = init.method || "GET";
1121
- init.headers = init.headers || {};
1122
- const request = {
1123
- type: "fetch",
1124
- url: url,
1125
- method: init.method.toUpperCase(),
1126
- abort: false,
1127
- headers: parseHeaders(init.headers),
1128
- data: init.body,
1129
- response: null,
1130
- async: true,
1131
- };
1132
- const req = new AHRequest(request);
1133
- await req.waitForRequestKeys();
1134
- if (request.abort) {
1135
- if (typeof request.response === "function") {
1136
- const response = {
1137
- finalUrl: request.url,
1138
- status: 200,
1139
- responseHeaders: {},
1140
- };
1141
- await req.waitForResponseKeys(response);
1142
- const key = fetchResponses.find((k) => k in response);
1143
- let val = response[key];
1144
- if (key === "json" && typeof val === "object") {
1145
- val = catchError(JSON.stringify.bind(JSON), val);
1146
- }
1147
- const res = new Response(val, {
1148
- status: 200,
1149
- statusText: "OK",
1150
- });
1151
- defineProp(res, "type", () => "basic");
1152
- defineProp(res, "url", () => request.url);
1153
- resolve(res);
1154
- } else {
1155
- reject(new DOMException("aborted", "AbortError"));
1156
- }
1157
- return;
1158
- }
1159
- init.method = request.method;
1160
- init.headers = request.headers;
1161
- init.body = request.data;
1162
- for (const prop of fetchExtraProps) {
1163
- if (prop in request) init[prop] = request[prop];
1164
- }
1165
- winAh.realFetch.call(win, request.url, init).then((res) => {
1166
- if (typeof request.response === "function") {
1167
- const response = {
1168
- finalUrl: res.url,
1169
- status: res.status,
1170
- responseHeaders: parseHeaders(res.headers),
1171
- };
1172
- if (res.ok) {
1173
- fetchResponses.forEach(
1174
- (key) =>
1175
- (res[key] = function () {
1176
- if (key in response) return Promise.resolve(response[key]);
1177
- return resProto[key].call(this).then((val) => {
1178
- response[key] = val;
1179
- return req
1180
- .waitForResponseKeys(response)
1181
- .then(() => (key in response ? response[key] : val));
1182
- });
1183
- })
1184
- );
1185
- } else {
1186
- catchError(request.response, response);
1187
- }
1188
- }
1189
- resolve(res);
1190
- }, reject);
1191
- });
1192
- }
1193
- function fakeFetchClone() {
1194
- const descriptors = Object.getOwnPropertyDescriptors(this);
1195
- const res = winAh.realFetchClone.call(this);
1196
- Object.defineProperties(res, descriptors);
1197
- return res;
1198
- }
1199
- winAh = win.__ajaxHooker = winAh || {
1200
- version,
1201
- fakeXHR,
1202
- fakeFetch,
1203
- fakeFetchClone,
1204
- realXHR: win.XMLHttpRequest,
1205
- realFetch: win.fetch,
1206
- realFetchClone: resProto.clone,
1207
- hookInsts: new Set(),
1208
- };
1209
- if (winAh.version !== version) console.warn("检测到不同版本的ajaxHooker,可能发生冲突!");
1210
- win.XMLHttpRequest = winAh.fakeXHR;
1211
- win.fetch = winAh.fakeFetch;
1212
- resProto.clone = winAh.fakeFetchClone;
1213
- winAh.hookInsts.add(hookInst);
1214
- // 针对头条、抖音 secsdk.umd.js 的兼容性处理
1215
- class AHFunction extends Function {
1216
- call(thisArg, ...args) {
1217
- if (thisArg && thisArg.__ajaxHooker && thisArg.__ajaxHooker.proxyXhr === thisArg) {
1218
- thisArg = thisArg.__ajaxHooker.originalXhr;
1219
- }
1220
- return Reflect.apply(this, thisArg, args);
1221
- }
1222
- apply(thisArg, args) {
1223
- if (thisArg && thisArg.__ajaxHooker && thisArg.__ajaxHooker.proxyXhr === thisArg) {
1224
- thisArg = thisArg.__ajaxHooker.originalXhr;
1225
- }
1226
- return Reflect.apply(this, thisArg, args || []);
1227
- }
1228
- }
1229
- function hookSecsdk(csrf) {
1230
- Object.setPrototypeOf(csrf.nativeXMLHttpRequestSetRequestHeader, AHFunction.prototype);
1231
- Object.setPrototypeOf(csrf.nativeXMLHttpRequestOpen, AHFunction.prototype);
1232
- Object.setPrototypeOf(csrf.nativeXMLHttpRequestSend, AHFunction.prototype);
1233
- }
1234
- if (win.secsdk) {
1235
- if (win.secsdk.csrf && win.secsdk.csrf.nativeXMLHttpRequestOpen) hookSecsdk(win.secsdk.csrf);
1236
- } else {
1237
- defineProp(win, "secsdk", emptyFn, (secsdk) => {
1238
- delete win.secsdk;
1239
- win.secsdk = secsdk;
1240
- defineProp(secsdk, "csrf", emptyFn, (csrf) => {
1241
- delete secsdk.csrf;
1242
- secsdk.csrf = csrf;
1243
- if (csrf.nativeXMLHttpRequestOpen) hookSecsdk(csrf);
1244
- });
1245
- });
1246
- }
1247
- return {
1248
- hook: (fn) => hookInst.hookFns.push(fn),
1249
- filter: (arr) => {
1250
- if (Array.isArray(arr)) hookInst.filters = arr;
1251
- },
1252
- protect: () => {
1253
- readonly(win, "XMLHttpRequest", winAh.fakeXHR);
1254
- readonly(win, "fetch", winAh.fakeFetch);
1255
- readonly(resProto, "clone", winAh.fakeFetchClone);
1256
- },
1257
- unhook: () => {
1258
- winAh.hookInsts.delete(hookInst);
1259
- if (!winAh.hookInsts.size) {
1260
- writable(win, "XMLHttpRequest", winAh.realXHR);
1261
- writable(win, "fetch", winAh.realFetch);
1262
- writable(resProto, "clone", winAh.realFetchClone);
1263
- delete win.__ajaxHooker;
1264
- }
1265
- },
1266
- };
734
+ /* eslint-disable */
735
+ // ==UserScript==
736
+ // @name ajaxHooker
737
+ // @author cxxjackie
738
+ // @version 1.4.8
739
+ // @supportURL https://bbs.tampermonkey.net.cn/thread-3284-1-1.html
740
+ // @license GNU LGPL-3.0
741
+ // ==/UserScript==
742
+ const ajaxHooker = function () {
743
+ const version = "1.4.8";
744
+ const hookInst = {
745
+ hookFns: [],
746
+ filters: [],
747
+ };
748
+ const win = window.unsafeWindow || document.defaultView || window;
749
+ let winAh = win.__ajaxHooker;
750
+ const resProto = win.Response.prototype;
751
+ const xhrResponses = ["response", "responseText", "responseXML"];
752
+ const fetchResponses = ["arrayBuffer", "blob", "formData", "json", "text"];
753
+ const xhrExtraProps = ["responseType", "timeout", "withCredentials"];
754
+ const fetchExtraProps = [
755
+ "cache",
756
+ "credentials",
757
+ "integrity",
758
+ "keepalive",
759
+ "mode",
760
+ "priority",
761
+ "redirect",
762
+ "referrer",
763
+ "referrerPolicy",
764
+ "signal",
765
+ ];
766
+ const xhrAsyncEvents = ["readystatechange", "load", "loadend"];
767
+ const getType = {}.toString.call.bind({}.toString);
768
+ const getDescriptor = Object.getOwnPropertyDescriptor.bind(Object);
769
+ const emptyFn = () => { };
770
+ const errorFn = (e) => console.error(e);
771
+ function isThenable(obj) {
772
+ return obj && ["object", "function"].includes(typeof obj) && typeof obj.then === "function";
773
+ }
774
+ function catchError(fn, ...args) {
775
+ try {
776
+ const result = fn(...args);
777
+ if (isThenable(result))
778
+ return result.then(null, errorFn);
779
+ return result;
780
+ }
781
+ catch (err) {
782
+ console.error(err);
783
+ }
784
+ }
785
+ function defineProp(obj, prop, getter, setter) {
786
+ Object.defineProperty(obj, prop, {
787
+ configurable: true,
788
+ enumerable: true,
789
+ get: getter,
790
+ set: setter,
791
+ });
792
+ }
793
+ function readonly(obj, prop, value = obj[prop]) {
794
+ defineProp(obj, prop, () => value, emptyFn);
795
+ }
796
+ function writable(obj, prop, value = obj[prop]) {
797
+ Object.defineProperty(obj, prop, {
798
+ configurable: true,
799
+ enumerable: true,
800
+ writable: true,
801
+ value: value,
802
+ });
803
+ }
804
+ function parseHeaders(obj) {
805
+ const headers = {};
806
+ switch (getType(obj)) {
807
+ case "[object String]":
808
+ for (const line of obj.trim().split(/[\r\n]+/)) {
809
+ const [header, value] = line.split(/(?<=^[^:]+)\s*:\s*/);
810
+ if (!value)
811
+ continue;
812
+ const lheader = header.toLowerCase();
813
+ headers[lheader] = lheader in headers ? `${headers[lheader]}, ${value}` : value;
814
+ }
815
+ break;
816
+ case "[object Headers]":
817
+ for (const [key, val] of obj) {
818
+ headers[key] = val;
819
+ }
820
+ break;
821
+ case "[object Object]":
822
+ return { ...obj };
823
+ }
824
+ return headers;
825
+ }
826
+ function stopImmediatePropagation() {
827
+ this.ajaxHooker_isStopped = true;
828
+ }
829
+ class SyncThenable {
830
+ then(fn) {
831
+ fn && fn();
832
+ return new SyncThenable();
833
+ }
834
+ }
835
+ class AHRequest {
836
+ constructor(request) {
837
+ this.request = request;
838
+ this.requestClone = { ...this.request };
839
+ }
840
+ _recoverRequestKey(key) {
841
+ if (key in this.requestClone)
842
+ this.request[key] = this.requestClone[key];
843
+ else
844
+ delete this.request[key];
845
+ }
846
+ shouldFilter(filters) {
847
+ const { type, url, method, async } = this.request;
848
+ return (filters.length &&
849
+ !filters.find((obj) => {
850
+ switch (true) {
851
+ case obj.type && obj.type !== type:
852
+ case getType(obj.url) === "[object String]" && !url.includes(obj.url):
853
+ case getType(obj.url) === "[object RegExp]" && !obj.url.test(url):
854
+ case obj.method && obj.method.toUpperCase() !== method.toUpperCase():
855
+ case "async" in obj && obj.async !== async:
856
+ return false;
857
+ }
858
+ return true;
859
+ }));
860
+ }
861
+ waitForRequestKeys() {
862
+ if (!this.request.async) {
863
+ win.__ajaxHooker.hookInsts.forEach(({ hookFns, filters }) => {
864
+ if (this.shouldFilter(filters))
865
+ return;
866
+ hookFns.forEach((fn) => {
867
+ if (getType(fn) === "[object Function]")
868
+ catchError(fn, this.request);
869
+ });
870
+ for (const key in this.request) {
871
+ if (isThenable(this.request[key]))
872
+ this._recoverRequestKey(key);
873
+ }
874
+ });
875
+ return new SyncThenable();
876
+ }
877
+ const promises = [];
878
+ const ignoreKeys = new Set(["type", "async", "response"]);
879
+ win.__ajaxHooker.hookInsts.forEach(({ hookFns, filters }) => {
880
+ if (this.shouldFilter(filters))
881
+ return;
882
+ promises.push(Promise.all(hookFns.map((fn) => catchError(fn, this.request))).then(() => {
883
+ const requestKeys = [];
884
+ for (const key in this.request)
885
+ !ignoreKeys.has(key) && requestKeys.push(key);
886
+ return Promise.all(requestKeys.map((key) => Promise.resolve(this.request[key]).then((val) => (this.request[key] = val), () => this._recoverRequestKey(key))));
887
+ }));
888
+ });
889
+ return Promise.all(promises);
890
+ }
891
+ waitForResponseKeys(response) {
892
+ const responseKeys = this.request.type === "xhr" ? xhrResponses : fetchResponses;
893
+ if (!this.request.async) {
894
+ if (getType(this.request.response) === "[object Function]") {
895
+ catchError(this.request.response, response);
896
+ responseKeys.forEach((key) => {
897
+ if ("get" in getDescriptor(response, key) || isThenable(response[key])) {
898
+ delete response[key];
899
+ }
900
+ });
901
+ }
902
+ return new SyncThenable();
903
+ }
904
+ return Promise.resolve(catchError(this.request.response, response)).then(() => Promise.all(responseKeys.map((key) => {
905
+ const descriptor = getDescriptor(response, key);
906
+ if (descriptor && "value" in descriptor) {
907
+ return Promise.resolve(descriptor.value).then((val) => (response[key] = val), () => delete response[key]);
908
+ }
909
+ else {
910
+ delete response[key];
911
+ }
912
+ })));
913
+ }
914
+ }
915
+ const proxyHandler = {
916
+ get(target, prop) {
917
+ const descriptor = getDescriptor(target, prop);
918
+ if (descriptor && !descriptor.configurable && !descriptor.writable && !descriptor.get)
919
+ return target[prop];
920
+ const ah = target.__ajaxHooker;
921
+ if (ah && ah.proxyProps) {
922
+ if (prop in ah.proxyProps) {
923
+ const pDescriptor = ah.proxyProps[prop];
924
+ if ("get" in pDescriptor)
925
+ return pDescriptor.get();
926
+ if (typeof pDescriptor.value === "function")
927
+ return pDescriptor.value.bind(ah);
928
+ return pDescriptor.value;
929
+ }
930
+ if (typeof target[prop] === "function")
931
+ return target[prop].bind(target);
932
+ }
933
+ return target[prop];
934
+ },
935
+ set(target, prop, value) {
936
+ const descriptor = getDescriptor(target, prop);
937
+ if (descriptor && !descriptor.configurable && !descriptor.writable && !descriptor.set)
938
+ return true;
939
+ const ah = target.__ajaxHooker;
940
+ if (ah && ah.proxyProps && prop in ah.proxyProps) {
941
+ const pDescriptor = ah.proxyProps[prop];
942
+ pDescriptor.set ? pDescriptor.set(value) : (pDescriptor.value = value);
943
+ }
944
+ else {
945
+ target[prop] = value;
946
+ }
947
+ return true;
948
+ },
949
+ };
950
+ class XhrHooker {
951
+ constructor(xhr) {
952
+ const ah = this;
953
+ Object.assign(ah, {
954
+ originalXhr: xhr,
955
+ proxyXhr: new Proxy(xhr, proxyHandler),
956
+ resThenable: new SyncThenable(),
957
+ proxyProps: {},
958
+ proxyEvents: {},
959
+ });
960
+ xhr.addEventListener("readystatechange", (e) => {
961
+ if (ah.proxyXhr.readyState === 4 && ah.request && typeof ah.request.response === "function") {
962
+ const response = {
963
+ finalUrl: ah.proxyXhr.responseURL,
964
+ status: ah.proxyXhr.status,
965
+ responseHeaders: parseHeaders(ah.proxyXhr.getAllResponseHeaders()),
966
+ };
967
+ const tempValues = {};
968
+ for (const key of xhrResponses) {
969
+ try {
970
+ tempValues[key] = ah.originalXhr[key];
971
+ }
972
+ catch (err) { }
973
+ defineProp(response, key, () => {
974
+ return (response[key] = tempValues[key]);
975
+ }, (val) => {
976
+ delete response[key];
977
+ response[key] = val;
978
+ });
979
+ }
980
+ ah.resThenable = new AHRequest(ah.request).waitForResponseKeys(response).then(() => {
981
+ for (const key of xhrResponses) {
982
+ ah.proxyProps[key] = {
983
+ get: () => {
984
+ if (!(key in response))
985
+ response[key] = tempValues[key];
986
+ return response[key];
987
+ },
988
+ };
989
+ }
990
+ });
991
+ }
992
+ ah.dispatchEvent(e);
993
+ });
994
+ xhr.addEventListener("load", (e) => ah.dispatchEvent(e));
995
+ xhr.addEventListener("loadend", (e) => ah.dispatchEvent(e));
996
+ for (const evt of xhrAsyncEvents) {
997
+ const onEvt = "on" + evt;
998
+ ah.proxyProps[onEvt] = {
999
+ get: () => ah.proxyEvents[onEvt] || null,
1000
+ set: (val) => ah.addEvent(onEvt, val),
1001
+ };
1002
+ }
1003
+ for (const method of ["setRequestHeader", "addEventListener", "removeEventListener", "open", "send"]) {
1004
+ ah.proxyProps[method] = { value: ah[method] };
1005
+ }
1006
+ }
1007
+ toJSON() { } // Converting circular structure to JSON
1008
+ addEvent(type, event) {
1009
+ if (type.startsWith("on")) {
1010
+ this.proxyEvents[type] = typeof event === "function" ? event : null;
1011
+ }
1012
+ else {
1013
+ if (typeof event === "object" && event !== null)
1014
+ event = event.handleEvent;
1015
+ if (typeof event !== "function")
1016
+ return;
1017
+ this.proxyEvents[type] = this.proxyEvents[type] || new Set();
1018
+ this.proxyEvents[type].add(event);
1019
+ }
1020
+ }
1021
+ removeEvent(type, event) {
1022
+ if (type.startsWith("on")) {
1023
+ this.proxyEvents[type] = null;
1024
+ }
1025
+ else {
1026
+ if (typeof event === "object" && event !== null)
1027
+ event = event.handleEvent;
1028
+ this.proxyEvents[type] && this.proxyEvents[type].delete(event);
1029
+ }
1030
+ }
1031
+ dispatchEvent(e) {
1032
+ e.stopImmediatePropagation = stopImmediatePropagation;
1033
+ defineProp(e, "target", () => this.proxyXhr);
1034
+ defineProp(e, "currentTarget", () => this.proxyXhr);
1035
+ defineProp(e, "srcElement", () => this.proxyXhr);
1036
+ this.proxyEvents[e.type] &&
1037
+ this.proxyEvents[e.type].forEach((fn) => {
1038
+ this.resThenable.then(() => !e.ajaxHooker_isStopped && fn.call(this.proxyXhr, e));
1039
+ });
1040
+ if (e.ajaxHooker_isStopped)
1041
+ return;
1042
+ const onEvent = this.proxyEvents["on" + e.type];
1043
+ onEvent && this.resThenable.then(onEvent.bind(this.proxyXhr, e));
1044
+ }
1045
+ setRequestHeader(header, value) {
1046
+ this.originalXhr.setRequestHeader(header, value);
1047
+ if (!this.request)
1048
+ return;
1049
+ const headers = this.request.headers;
1050
+ headers[header] = header in headers ? `${headers[header]}, ${value}` : value;
1051
+ }
1052
+ addEventListener(...args) {
1053
+ if (xhrAsyncEvents.includes(args[0])) {
1054
+ this.addEvent(args[0], args[1]);
1055
+ }
1056
+ else {
1057
+ this.originalXhr.addEventListener(...args);
1058
+ }
1059
+ }
1060
+ removeEventListener(...args) {
1061
+ if (xhrAsyncEvents.includes(args[0])) {
1062
+ this.removeEvent(args[0], args[1]);
1063
+ }
1064
+ else {
1065
+ this.originalXhr.removeEventListener(...args);
1066
+ }
1067
+ }
1068
+ open(method, url, async = true, ...args) {
1069
+ this.request = {
1070
+ type: "xhr",
1071
+ url: url.toString(),
1072
+ method: method.toUpperCase(),
1073
+ abort: false,
1074
+ headers: {},
1075
+ data: null,
1076
+ response: null,
1077
+ async: !!async,
1078
+ };
1079
+ this.openArgs = args;
1080
+ this.resThenable = new SyncThenable();
1081
+ ["responseURL", "readyState", "status", "statusText", ...xhrResponses].forEach((key) => {
1082
+ delete this.proxyProps[key];
1083
+ });
1084
+ return this.originalXhr.open(method, url, async, ...args);
1085
+ }
1086
+ send(data) {
1087
+ const ah = this;
1088
+ const xhr = ah.originalXhr;
1089
+ const request = ah.request;
1090
+ if (!request)
1091
+ return xhr.send(data);
1092
+ request.data = data;
1093
+ new AHRequest(request).waitForRequestKeys().then(() => {
1094
+ if (request.abort) {
1095
+ if (typeof request.response === "function") {
1096
+ Object.assign(ah.proxyProps, {
1097
+ responseURL: { value: request.url },
1098
+ readyState: { value: 4 },
1099
+ status: { value: 200 },
1100
+ statusText: { value: "OK" },
1101
+ });
1102
+ xhrAsyncEvents.forEach((evt) => xhr.dispatchEvent(new Event(evt)));
1103
+ }
1104
+ }
1105
+ else {
1106
+ xhr.open(request.method, request.url, request.async, ...ah.openArgs);
1107
+ for (const header in request.headers) {
1108
+ xhr.setRequestHeader(header, request.headers[header]);
1109
+ }
1110
+ for (const prop of xhrExtraProps) {
1111
+ if (prop in request)
1112
+ xhr[prop] = request[prop];
1113
+ }
1114
+ xhr.send(request.data);
1115
+ }
1116
+ });
1117
+ }
1118
+ }
1119
+ function fakeXHR() {
1120
+ const xhr = new winAh.realXHR();
1121
+ if ("__ajaxHooker" in xhr)
1122
+ console.warn("检测到不同版本的ajaxHooker,可能发生冲突!");
1123
+ xhr.__ajaxHooker = new XhrHooker(xhr);
1124
+ return xhr.__ajaxHooker.proxyXhr;
1125
+ }
1126
+ fakeXHR.prototype = win.XMLHttpRequest.prototype;
1127
+ Object.keys(win.XMLHttpRequest).forEach((key) => (fakeXHR[key] = win.XMLHttpRequest[key]));
1128
+ function fakeFetch(url, options = {}) {
1129
+ if (!url)
1130
+ return winAh.realFetch.call(win, url, options);
1131
+ return new Promise(async (resolve, reject) => {
1132
+ const init = {};
1133
+ if (getType(url) === "[object Request]") {
1134
+ init.method = url.method;
1135
+ init.headers = url.headers;
1136
+ if (url.body)
1137
+ init.body = await url.arrayBuffer();
1138
+ for (const prop of fetchExtraProps)
1139
+ init[prop] = url[prop];
1140
+ url = url.url;
1141
+ }
1142
+ url = url.toString();
1143
+ Object.assign(init, options);
1144
+ init.method = init.method || "GET";
1145
+ init.headers = init.headers || {};
1146
+ const request = {
1147
+ type: "fetch",
1148
+ url: url,
1149
+ method: init.method.toUpperCase(),
1150
+ abort: false,
1151
+ headers: parseHeaders(init.headers),
1152
+ data: init.body,
1153
+ response: null,
1154
+ async: true,
1155
+ };
1156
+ const req = new AHRequest(request);
1157
+ await req.waitForRequestKeys();
1158
+ if (request.abort) {
1159
+ if (typeof request.response === "function") {
1160
+ const response = {
1161
+ finalUrl: request.url,
1162
+ status: 200,
1163
+ responseHeaders: {},
1164
+ };
1165
+ await req.waitForResponseKeys(response);
1166
+ const key = fetchResponses.find((k) => k in response);
1167
+ let val = response[key];
1168
+ if (key === "json" && typeof val === "object") {
1169
+ val = catchError(JSON.stringify.bind(JSON), val);
1170
+ }
1171
+ const res = new Response(val, {
1172
+ status: 200,
1173
+ statusText: "OK",
1174
+ });
1175
+ defineProp(res, "type", () => "basic");
1176
+ defineProp(res, "url", () => request.url);
1177
+ resolve(res);
1178
+ }
1179
+ else {
1180
+ reject(new DOMException("aborted", "AbortError"));
1181
+ }
1182
+ return;
1183
+ }
1184
+ init.method = request.method;
1185
+ init.headers = request.headers;
1186
+ init.body = request.data;
1187
+ for (const prop of fetchExtraProps) {
1188
+ if (prop in request)
1189
+ init[prop] = request[prop];
1190
+ }
1191
+ winAh.realFetch.call(win, request.url, init).then((res) => {
1192
+ if (typeof request.response === "function") {
1193
+ const response = {
1194
+ finalUrl: res.url,
1195
+ status: res.status,
1196
+ responseHeaders: parseHeaders(res.headers),
1197
+ };
1198
+ if (res.ok) {
1199
+ fetchResponses.forEach((key) => (res[key] = function () {
1200
+ if (key in response)
1201
+ return Promise.resolve(response[key]);
1202
+ return resProto[key].call(this).then((val) => {
1203
+ response[key] = val;
1204
+ return req
1205
+ .waitForResponseKeys(response)
1206
+ .then(() => (key in response ? response[key] : val));
1207
+ });
1208
+ }));
1209
+ }
1210
+ else {
1211
+ catchError(request.response, response);
1212
+ }
1213
+ }
1214
+ resolve(res);
1215
+ }, reject);
1216
+ });
1217
+ }
1218
+ function fakeFetchClone() {
1219
+ const descriptors = Object.getOwnPropertyDescriptors(this);
1220
+ const res = winAh.realFetchClone.call(this);
1221
+ Object.defineProperties(res, descriptors);
1222
+ return res;
1223
+ }
1224
+ winAh = win.__ajaxHooker = winAh || {
1225
+ version,
1226
+ fakeXHR,
1227
+ fakeFetch,
1228
+ fakeFetchClone,
1229
+ realXHR: win.XMLHttpRequest,
1230
+ realFetch: win.fetch,
1231
+ realFetchClone: resProto.clone,
1232
+ hookInsts: new Set(),
1233
+ };
1234
+ if (winAh.version !== version)
1235
+ console.warn("检测到不同版本的ajaxHooker,可能发生冲突!");
1236
+ win.XMLHttpRequest = winAh.fakeXHR;
1237
+ win.fetch = winAh.fakeFetch;
1238
+ resProto.clone = winAh.fakeFetchClone;
1239
+ winAh.hookInsts.add(hookInst);
1240
+ // 针对头条、抖音 secsdk.umd.js 的兼容性处理
1241
+ class AHFunction extends Function {
1242
+ call(thisArg, ...args) {
1243
+ if (thisArg && thisArg.__ajaxHooker && thisArg.__ajaxHooker.proxyXhr === thisArg) {
1244
+ thisArg = thisArg.__ajaxHooker.originalXhr;
1245
+ }
1246
+ return Reflect.apply(this, thisArg, args);
1247
+ }
1248
+ apply(thisArg, args) {
1249
+ if (thisArg && thisArg.__ajaxHooker && thisArg.__ajaxHooker.proxyXhr === thisArg) {
1250
+ thisArg = thisArg.__ajaxHooker.originalXhr;
1251
+ }
1252
+ return Reflect.apply(this, thisArg, args || []);
1253
+ }
1254
+ }
1255
+ function hookSecsdk(csrf) {
1256
+ Object.setPrototypeOf(csrf.nativeXMLHttpRequestSetRequestHeader, AHFunction.prototype);
1257
+ Object.setPrototypeOf(csrf.nativeXMLHttpRequestOpen, AHFunction.prototype);
1258
+ Object.setPrototypeOf(csrf.nativeXMLHttpRequestSend, AHFunction.prototype);
1259
+ }
1260
+ if (win.secsdk) {
1261
+ if (win.secsdk.csrf && win.secsdk.csrf.nativeXMLHttpRequestOpen)
1262
+ hookSecsdk(win.secsdk.csrf);
1263
+ }
1264
+ else {
1265
+ defineProp(win, "secsdk", emptyFn, (secsdk) => {
1266
+ delete win.secsdk;
1267
+ win.secsdk = secsdk;
1268
+ defineProp(secsdk, "csrf", emptyFn, (csrf) => {
1269
+ delete secsdk.csrf;
1270
+ secsdk.csrf = csrf;
1271
+ if (csrf.nativeXMLHttpRequestOpen)
1272
+ hookSecsdk(csrf);
1273
+ });
1274
+ });
1275
+ }
1276
+ return {
1277
+ hook: (fn) => hookInst.hookFns.push(fn),
1278
+ filter: (arr) => {
1279
+ if (Array.isArray(arr))
1280
+ hookInst.filters = arr;
1281
+ },
1282
+ protect: () => {
1283
+ readonly(win, "XMLHttpRequest", winAh.fakeXHR);
1284
+ readonly(win, "fetch", winAh.fakeFetch);
1285
+ readonly(resProto, "clone", winAh.fakeFetchClone);
1286
+ },
1287
+ unhook: () => {
1288
+ winAh.hookInsts.delete(hookInst);
1289
+ if (!winAh.hookInsts.size) {
1290
+ writable(win, "XMLHttpRequest", winAh.realXHR);
1291
+ writable(win, "fetch", winAh.realFetch);
1292
+ writable(resProto, "clone", winAh.realFetchClone);
1293
+ delete win.__ajaxHooker;
1294
+ }
1295
+ },
1296
+ };
1267
1297
  };
1268
1298
 
1269
- // ==UserScript==
1270
- // @name ajaxHooker
1271
- // @author cxxjackie
1272
- // @version 1.2.4
1273
- // @supportURL https://bbs.tampermonkey.net.cn/thread-3284-1-1.html
1274
- // ==/UserScript==
1275
-
1276
- const AjaxHooker1_2_4 = function () {
1277
- return (function () {
1278
- const win = window.unsafeWindow || document.defaultView || window;
1279
- const hookFns = [];
1280
- const realXhr = win.XMLHttpRequest;
1281
- const resProto = win.Response.prototype;
1282
- const toString = Object.prototype.toString;
1283
- const realFetch = win.fetch;
1284
- const xhrResponses = ["response", "responseText", "responseXML"];
1285
- const fetchResponses = ["arrayBuffer", "blob", "formData", "json", "text"];
1286
- const xhrAsyncEvents = ["readystatechange", "load", "loadend"];
1287
- let filter;
1288
- function emptyFn() {}
1289
- function errorFn(err) {
1290
- console.error(err);
1291
- }
1292
- function defineProp(obj, prop, getter, setter) {
1293
- Object.defineProperty(obj, prop, {
1294
- configurable: true,
1295
- enumerable: true,
1296
- get: getter,
1297
- set: setter,
1298
- });
1299
- }
1300
- function readonly(obj, prop, value = obj[prop]) {
1301
- defineProp(obj, prop, () => value, emptyFn);
1302
- }
1303
- function writable(obj, prop, value = obj[prop]) {
1304
- Object.defineProperty(obj, prop, {
1305
- configurable: true,
1306
- enumerable: true,
1307
- writable: true,
1308
- value: value,
1309
- });
1310
- }
1311
- function toFilterObj(obj) {
1312
- return {
1313
- type: obj.type,
1314
- url: obj.url,
1315
- method: obj.method && obj.method.toUpperCase(),
1316
- };
1317
- }
1318
- function shouldFilter(type, url, method) {
1319
- return (
1320
- filter &&
1321
- !filter.find(
1322
- (obj) =>
1323
- (!obj.type || obj.type === type) &&
1324
- (!obj.url ||
1325
- (toString.call(obj.url) === "[object String]"
1326
- ? url.includes(obj.url)
1327
- : obj.url.test(url))) &&
1328
- (!obj.method || obj.method === method.toUpperCase())
1329
- )
1330
- );
1331
- }
1332
- function lookupGetter(obj, prop) {
1333
- let getter;
1334
- let proto = obj;
1335
- while (proto) {
1336
- const descriptor = Object.getOwnPropertyDescriptor(proto, prop);
1337
- getter = descriptor && descriptor.get;
1338
- if (getter) break;
1339
- proto = Object.getPrototypeOf(proto);
1340
- }
1341
- return getter ? getter.bind(obj) : emptyFn;
1342
- }
1343
- function waitForHookFns(request) {
1344
- return Promise.all(
1345
- hookFns.map((fn) => Promise.resolve(fn(request)).then(emptyFn, errorFn))
1346
- );
1347
- }
1348
- function waitForRequestKeys(request, requestClone) {
1349
- return Promise.all(
1350
- ["url", "method", "abort", "headers", "data"].map((key) => {
1351
- return Promise.resolve(request[key]).then(
1352
- (val) => (request[key] = val),
1353
- () => (request[key] = requestClone[key])
1354
- );
1355
- })
1356
- );
1357
- }
1358
- function fakeEventSIP() {
1359
- this.ajaxHooker_stopped = true;
1360
- }
1361
- function xhrDelegateEvent(e) {
1362
- const xhr = e.target;
1363
- e.stopImmediatePropagation = fakeEventSIP;
1364
- xhr.__ajaxHooker.hookedEvents[e.type].forEach(
1365
- (fn) => !e.ajaxHooker_stopped && fn.call(xhr, e)
1366
- );
1367
- const onEvent = xhr.__ajaxHooker.hookedEvents["on" + e.type];
1368
- typeof onEvent === "function" && onEvent.call(xhr, e);
1369
- }
1370
- function xhrReadyStateChange(e) {
1371
- if (e.target.readyState === 4) {
1372
- e.target.dispatchEvent(
1373
- new CustomEvent("ajaxHooker_responseReady", { detail: e })
1374
- );
1375
- } else {
1376
- e.target.__ajaxHooker.delegateEvent(e);
1377
- }
1378
- }
1379
- function xhrLoadAndLoadend(e) {
1380
- e.target.__ajaxHooker.delegateEvent(e);
1381
- }
1382
- function fakeXhrOpen(method, url, ...args) {
1383
- const ah = this.__ajaxHooker;
1384
- ah.url = url.toString();
1385
- ah.method = method.toUpperCase();
1386
- ah.openArgs = args;
1387
- ah.headers = {};
1388
- return ah.originalMethods.open(method, url, ...args);
1389
- }
1390
- function fakeXhr() {
1391
- const xhr = new realXhr();
1392
- let ah = xhr.__ajaxHooker;
1393
- if (!ah) {
1394
- ah = xhr.__ajaxHooker = {
1395
- headers: {},
1396
- hookedEvents: {
1397
- readystatechange: new Set(),
1398
- load: new Set(),
1399
- loadend: new Set(),
1400
- },
1401
- delegateEvent: xhrDelegateEvent,
1402
- originalGetters: {},
1403
- originalMethods: {},
1404
- };
1405
- xhr.addEventListener("readystatechange", xhrReadyStateChange);
1406
- xhr.addEventListener("load", xhrLoadAndLoadend);
1407
- xhr.addEventListener("loadend", xhrLoadAndLoadend);
1408
- for (const key of xhrResponses) {
1409
- ah.originalGetters[key] = lookupGetter(xhr, key);
1410
- }
1411
- for (const method of [
1412
- "open",
1413
- "setRequestHeader",
1414
- "addEventListener",
1415
- "removeEventListener",
1416
- ]) {
1417
- ah.originalMethods[method] = xhr[method].bind(xhr);
1418
- }
1419
- xhr.open = fakeXhrOpen;
1420
- xhr.setRequestHeader = (header, value) => {
1421
- ah.originalMethods.setRequestHeader(header, value);
1422
- if (xhr.readyState === 1) {
1423
- if (ah.headers[header]) {
1424
- ah.headers[header] += ", " + value;
1425
- } else {
1426
- ah.headers[header] = value;
1427
- }
1428
- }
1429
- };
1430
- xhr.addEventListener = function (...args) {
1431
- if (xhrAsyncEvents.includes(args[0])) {
1432
- ah.hookedEvents[args[0]].add(args[1]);
1433
- } else {
1434
- ah.originalMethods.addEventListener(...args);
1435
- }
1436
- };
1437
- xhr.removeEventListener = function (...args) {
1438
- if (xhrAsyncEvents.includes(args[0])) {
1439
- ah.hookedEvents[args[0]].delete(args[1]);
1440
- } else {
1441
- ah.originalMethods.removeEventListener(...args);
1442
- }
1443
- };
1444
- xhrAsyncEvents.forEach((evt) => {
1445
- const onEvt = "on" + evt;
1446
- defineProp(
1447
- xhr,
1448
- onEvt,
1449
- () => {
1450
- return ah.hookedEvents[onEvt] || null;
1451
- },
1452
- (val) => {
1453
- ah.hookedEvents[onEvt] = typeof val === "function" ? val : null;
1454
- }
1455
- );
1456
- });
1457
- }
1458
- const realSend = xhr.send.bind(xhr);
1459
- xhr.send = function (data) {
1460
- if (xhr.readyState !== 1) return realSend(data);
1461
- ah.delegateEvent = xhrDelegateEvent;
1462
- xhrResponses.forEach((prop) => {
1463
- delete xhr[prop]; // delete descriptor
1464
- });
1465
- if (shouldFilter("xhr", ah.url, ah.method)) {
1466
- xhr.addEventListener("ajaxHooker_responseReady", (e) => {
1467
- ah.delegateEvent(e.detail);
1468
- });
1469
- return realSend(data);
1470
- }
1471
- try {
1472
- const request = {
1473
- type: "xhr",
1474
- url: ah.url,
1475
- method: ah.method,
1476
- abort: false,
1477
- headers: ah.headers,
1478
- data: data,
1479
- response: null,
1480
- };
1481
- const requestClone = { ...request };
1482
- waitForHookFns(request).then(() => {
1483
- waitForRequestKeys(request, requestClone).then(() => {
1484
- if (request.abort) return;
1485
- ah.originalMethods.open(
1486
- request.method,
1487
- request.url,
1488
- ...ah.openArgs
1489
- );
1490
- for (const header in request.headers) {
1491
- ah.originalMethods.setRequestHeader(
1492
- header,
1493
- request.headers[header]
1494
- );
1495
- }
1496
- data = request.data;
1497
- xhr.addEventListener("ajaxHooker_responseReady", (e) => {
1498
- try {
1499
- if (typeof request.response === "function") {
1500
- const arg = {
1501
- finalUrl: xhr.responseURL,
1502
- status: xhr.status,
1503
- responseHeaders: {},
1504
- };
1505
- for (const line of xhr
1506
- .getAllResponseHeaders()
1507
- .trim()
1508
- .split(/[\r\n]+/)) {
1509
- const parts = line.split(/:\s*/);
1510
- if (parts.length === 2) {
1511
- const lheader = parts[0].toLowerCase();
1512
- if (arg.responseHeaders[lheader]) {
1513
- arg.responseHeaders[lheader] += ", " + parts[1];
1514
- } else {
1515
- arg.responseHeaders[lheader] = parts[1];
1516
- }
1517
- }
1518
- }
1519
- xhrResponses.forEach((prop) => {
1520
- defineProp(
1521
- arg,
1522
- prop,
1523
- () => {
1524
- return (arg[prop] = ah.originalGetters[prop]());
1525
- },
1526
- (val) => {
1527
- delete arg[prop];
1528
- arg[prop] = val;
1529
- }
1530
- );
1531
- defineProp(xhr, prop, () => {
1532
- const val = ah.originalGetters[prop]();
1533
- xhr.dispatchEvent(
1534
- new CustomEvent("ajaxHooker_readResponse", {
1535
- detail: { prop, val },
1536
- })
1537
- );
1538
- return val;
1539
- });
1540
- });
1541
- xhr.addEventListener("ajaxHooker_readResponse", (e) => {
1542
- arg[e.detail.prop] = e.detail.val;
1543
- });
1544
- const resPromise = Promise.resolve(
1545
- request.response(arg)
1546
- ).then(() => {
1547
- const task = [];
1548
- xhrResponses.forEach((prop) => {
1549
- const descriptor = Object.getOwnPropertyDescriptor(
1550
- arg,
1551
- prop
1552
- );
1553
- if (descriptor && "value" in descriptor) {
1554
- task.push(
1555
- Promise.resolve(descriptor.value).then((val) => {
1556
- arg[prop] = val;
1557
- defineProp(xhr, prop, () => {
1558
- xhr.dispatchEvent(
1559
- new CustomEvent("ajaxHooker_readResponse", {
1560
- detail: { prop, val },
1561
- })
1562
- );
1563
- return val;
1564
- });
1565
- }, emptyFn)
1566
- );
1567
- }
1568
- });
1569
- return Promise.all(task);
1570
- }, errorFn);
1571
- const eventsClone = {};
1572
- xhrAsyncEvents.forEach((type) => {
1573
- eventsClone[type] = new Set([...ah.hookedEvents[type]]);
1574
- eventsClone["on" + type] = ah.hookedEvents["on" + type];
1575
- });
1576
- ah.delegateEvent = (event) =>
1577
- resPromise.then(() => {
1578
- event.stopImmediatePropagation = fakeEventSIP;
1579
- eventsClone[event.type].forEach(
1580
- (fn) =>
1581
- !event.ajaxHooker_stopped && fn.call(xhr, event)
1582
- );
1583
- const onEvent = eventsClone["on" + event.type];
1584
- typeof onEvent === "function" &&
1585
- onEvent.call(xhr, event);
1586
- });
1587
- }
1588
- } catch (err) {
1589
- console.error(err);
1590
- }
1591
- ah.delegateEvent(e.detail);
1592
- });
1593
- realSend(data);
1594
- });
1595
- });
1596
- } catch (err) {
1597
- console.error(err);
1598
- realSend(data);
1599
- }
1600
- };
1601
- return xhr;
1602
- }
1603
- function hookFetchResponse(response, arg, callback) {
1604
- fetchResponses.forEach((prop) => {
1605
- response[prop] = () =>
1606
- new Promise((resolve, reject) => {
1607
- resProto[prop].call(response).then((res) => {
1608
- if (prop in arg) {
1609
- resolve(arg[prop]);
1610
- } else {
1611
- try {
1612
- arg[prop] = res;
1613
- Promise.resolve(callback(arg)).then(() => {
1614
- if (prop in arg) {
1615
- Promise.resolve(arg[prop]).then(
1616
- (val) => resolve((arg[prop] = val)),
1617
- () => resolve(res)
1618
- );
1619
- } else {
1620
- resolve(res);
1621
- }
1622
- }, errorFn);
1623
- } catch (err) {
1624
- console.error(err);
1625
- resolve(res);
1626
- }
1627
- }
1628
- }, reject);
1629
- });
1630
- });
1631
- }
1632
- function fakeFetch(url, init) {
1633
- if (url && typeof url.toString === "function") {
1634
- url = url.toString();
1635
- init = init || {};
1636
- init.method = init.method || "GET";
1637
- init.headers = init.headers || {};
1638
- if (shouldFilter("fetch", url, init.method))
1639
- return realFetch.call(win, url, init);
1640
- const request = {
1641
- type: "fetch",
1642
- url: url,
1643
- method: init.method.toUpperCase(),
1644
- abort: false,
1645
- headers: {},
1646
- data: init.body,
1647
- response: null,
1648
- };
1649
- if (toString.call(init.headers) === "[object Headers]") {
1650
- for (const [key, val] of init.headers) {
1651
- request.headers[key] = val;
1652
- }
1653
- } else {
1654
- request.headers = { ...init.headers };
1655
- }
1656
- const requestClone = { ...request };
1657
- return new Promise((resolve, reject) => {
1658
- try {
1659
- waitForHookFns(request).then(() => {
1660
- waitForRequestKeys(request, requestClone).then(() => {
1661
- if (request.abort) return reject("aborted");
1662
- url = request.url;
1663
- init.method = request.method;
1664
- init.headers = request.headers;
1665
- init.body = request.data;
1666
- realFetch.call(win, url, init).then((response) => {
1667
- if (typeof request.response === "function") {
1668
- const arg = {
1669
- finalUrl: response.url,
1670
- status: response.status,
1671
- responseHeaders: {},
1672
- };
1673
- for (const [key, val] of response.headers) {
1674
- arg.responseHeaders[key] = val;
1675
- }
1676
- hookFetchResponse(response, arg, request.response);
1677
- response.clone = () => {
1678
- const resClone = resProto.clone.call(response);
1679
- hookFetchResponse(resClone, arg, request.response);
1680
- return resClone;
1681
- };
1682
- }
1683
- resolve(response);
1684
- }, reject);
1685
- });
1686
- });
1687
- } catch (err) {
1688
- console.error(err);
1689
- return realFetch.call(win, url, init);
1690
- }
1691
- });
1692
- } else {
1693
- return realFetch.call(win, url, init);
1694
- }
1695
- }
1696
- win.XMLHttpRequest = fakeXhr;
1697
- Object.keys(realXhr).forEach((key) => (fakeXhr[key] = realXhr[key]));
1698
- fakeXhr.prototype = realXhr.prototype;
1699
- win.fetch = fakeFetch;
1700
- return {
1701
- hook: (fn) => hookFns.push(fn),
1702
- filter: (arr) => {
1703
- filter = Array.isArray(arr) && arr.map(toFilterObj);
1704
- },
1705
- protect: () => {
1706
- readonly(win, "XMLHttpRequest", fakeXhr);
1707
- readonly(win, "fetch", fakeFetch);
1708
- },
1709
- unhook: () => {
1710
- writable(win, "XMLHttpRequest", realXhr);
1711
- writable(win, "fetch", realFetch);
1712
- },
1713
- };
1714
- })();
1299
+ // ==UserScript==
1300
+ // @name ajaxHooker
1301
+ // @author cxxjackie
1302
+ // @version 1.2.4
1303
+ // @supportURL https://bbs.tampermonkey.net.cn/thread-3284-1-1.html
1304
+ // ==/UserScript==
1305
+ const AjaxHooker1_2_4 = function () {
1306
+ return (function () {
1307
+ const win = window.unsafeWindow || document.defaultView || window;
1308
+ const hookFns = [];
1309
+ const realXhr = win.XMLHttpRequest;
1310
+ const resProto = win.Response.prototype;
1311
+ const toString = Object.prototype.toString;
1312
+ const realFetch = win.fetch;
1313
+ const xhrResponses = ["response", "responseText", "responseXML"];
1314
+ const fetchResponses = ["arrayBuffer", "blob", "formData", "json", "text"];
1315
+ const xhrAsyncEvents = ["readystatechange", "load", "loadend"];
1316
+ let filter;
1317
+ function emptyFn() { }
1318
+ function errorFn(err) {
1319
+ console.error(err);
1320
+ }
1321
+ function defineProp(obj, prop, getter, setter) {
1322
+ Object.defineProperty(obj, prop, {
1323
+ configurable: true,
1324
+ enumerable: true,
1325
+ get: getter,
1326
+ set: setter,
1327
+ });
1328
+ }
1329
+ function readonly(obj, prop, value = obj[prop]) {
1330
+ defineProp(obj, prop, () => value, emptyFn);
1331
+ }
1332
+ function writable(obj, prop, value = obj[prop]) {
1333
+ Object.defineProperty(obj, prop, {
1334
+ configurable: true,
1335
+ enumerable: true,
1336
+ writable: true,
1337
+ value: value,
1338
+ });
1339
+ }
1340
+ function toFilterObj(obj) {
1341
+ return {
1342
+ type: obj.type,
1343
+ url: obj.url,
1344
+ method: obj.method && obj.method.toUpperCase(),
1345
+ };
1346
+ }
1347
+ function shouldFilter(type, url, method) {
1348
+ return (filter &&
1349
+ !filter.find((obj) => (!obj.type || obj.type === type) &&
1350
+ (!obj.url ||
1351
+ (toString.call(obj.url) === "[object String]"
1352
+ ? url.includes(obj.url)
1353
+ : obj.url.test(url))) &&
1354
+ (!obj.method || obj.method === method.toUpperCase())));
1355
+ }
1356
+ function lookupGetter(obj, prop) {
1357
+ let getter;
1358
+ let proto = obj;
1359
+ while (proto) {
1360
+ const descriptor = Object.getOwnPropertyDescriptor(proto, prop);
1361
+ getter = descriptor && descriptor.get;
1362
+ if (getter)
1363
+ break;
1364
+ proto = Object.getPrototypeOf(proto);
1365
+ }
1366
+ return getter ? getter.bind(obj) : emptyFn;
1367
+ }
1368
+ function waitForHookFns(request) {
1369
+ return Promise.all(hookFns.map((fn) => Promise.resolve(fn(request)).then(emptyFn, errorFn)));
1370
+ }
1371
+ function waitForRequestKeys(request, requestClone) {
1372
+ return Promise.all(["url", "method", "abort", "headers", "data"].map((key) => {
1373
+ return Promise.resolve(request[key]).then((val) => (request[key] = val), () => (request[key] = requestClone[key]));
1374
+ }));
1375
+ }
1376
+ function fakeEventSIP() {
1377
+ this.ajaxHooker_stopped = true;
1378
+ }
1379
+ function xhrDelegateEvent(e) {
1380
+ const xhr = e.target;
1381
+ e.stopImmediatePropagation = fakeEventSIP;
1382
+ xhr.__ajaxHooker.hookedEvents[e.type].forEach((fn) => !e.ajaxHooker_stopped && fn.call(xhr, e));
1383
+ const onEvent = xhr.__ajaxHooker.hookedEvents["on" + e.type];
1384
+ typeof onEvent === "function" && onEvent.call(xhr, e);
1385
+ }
1386
+ function xhrReadyStateChange(e) {
1387
+ if (e.target.readyState === 4) {
1388
+ e.target.dispatchEvent(new CustomEvent("ajaxHooker_responseReady", { detail: e }));
1389
+ }
1390
+ else {
1391
+ e.target.__ajaxHooker.delegateEvent(e);
1392
+ }
1393
+ }
1394
+ function xhrLoadAndLoadend(e) {
1395
+ e.target.__ajaxHooker.delegateEvent(e);
1396
+ }
1397
+ function fakeXhrOpen(method, url, ...args) {
1398
+ const ah = this.__ajaxHooker;
1399
+ ah.url = url.toString();
1400
+ ah.method = method.toUpperCase();
1401
+ ah.openArgs = args;
1402
+ ah.headers = {};
1403
+ return ah.originalMethods.open(method, url, ...args);
1404
+ }
1405
+ function fakeXhr() {
1406
+ const xhr = new realXhr();
1407
+ let ah = xhr.__ajaxHooker;
1408
+ if (!ah) {
1409
+ ah = xhr.__ajaxHooker = {
1410
+ headers: {},
1411
+ hookedEvents: {
1412
+ readystatechange: new Set(),
1413
+ load: new Set(),
1414
+ loadend: new Set(),
1415
+ },
1416
+ delegateEvent: xhrDelegateEvent,
1417
+ originalGetters: {},
1418
+ originalMethods: {},
1419
+ };
1420
+ xhr.addEventListener("readystatechange", xhrReadyStateChange);
1421
+ xhr.addEventListener("load", xhrLoadAndLoadend);
1422
+ xhr.addEventListener("loadend", xhrLoadAndLoadend);
1423
+ for (const key of xhrResponses) {
1424
+ ah.originalGetters[key] = lookupGetter(xhr, key);
1425
+ }
1426
+ for (const method of [
1427
+ "open",
1428
+ "setRequestHeader",
1429
+ "addEventListener",
1430
+ "removeEventListener",
1431
+ ]) {
1432
+ ah.originalMethods[method] = xhr[method].bind(xhr);
1433
+ }
1434
+ xhr.open = fakeXhrOpen;
1435
+ xhr.setRequestHeader = (header, value) => {
1436
+ ah.originalMethods.setRequestHeader(header, value);
1437
+ if (xhr.readyState === 1) {
1438
+ if (ah.headers[header]) {
1439
+ ah.headers[header] += ", " + value;
1440
+ }
1441
+ else {
1442
+ ah.headers[header] = value;
1443
+ }
1444
+ }
1445
+ };
1446
+ xhr.addEventListener = function (...args) {
1447
+ if (xhrAsyncEvents.includes(args[0])) {
1448
+ ah.hookedEvents[args[0]].add(args[1]);
1449
+ }
1450
+ else {
1451
+ ah.originalMethods.addEventListener(...args);
1452
+ }
1453
+ };
1454
+ xhr.removeEventListener = function (...args) {
1455
+ if (xhrAsyncEvents.includes(args[0])) {
1456
+ ah.hookedEvents[args[0]].delete(args[1]);
1457
+ }
1458
+ else {
1459
+ ah.originalMethods.removeEventListener(...args);
1460
+ }
1461
+ };
1462
+ xhrAsyncEvents.forEach((evt) => {
1463
+ const onEvt = "on" + evt;
1464
+ defineProp(xhr, onEvt, () => {
1465
+ return ah.hookedEvents[onEvt] || null;
1466
+ }, (val) => {
1467
+ ah.hookedEvents[onEvt] = typeof val === "function" ? val : null;
1468
+ });
1469
+ });
1470
+ }
1471
+ const realSend = xhr.send.bind(xhr);
1472
+ xhr.send = function (data) {
1473
+ if (xhr.readyState !== 1)
1474
+ return realSend(data);
1475
+ ah.delegateEvent = xhrDelegateEvent;
1476
+ xhrResponses.forEach((prop) => {
1477
+ delete xhr[prop]; // delete descriptor
1478
+ });
1479
+ if (shouldFilter("xhr", ah.url, ah.method)) {
1480
+ xhr.addEventListener("ajaxHooker_responseReady", (e) => {
1481
+ ah.delegateEvent(e.detail);
1482
+ });
1483
+ return realSend(data);
1484
+ }
1485
+ try {
1486
+ const request = {
1487
+ type: "xhr",
1488
+ url: ah.url,
1489
+ method: ah.method,
1490
+ abort: false,
1491
+ headers: ah.headers,
1492
+ data: data,
1493
+ response: null,
1494
+ };
1495
+ const requestClone = { ...request };
1496
+ waitForHookFns(request).then(() => {
1497
+ waitForRequestKeys(request, requestClone).then(() => {
1498
+ if (request.abort)
1499
+ return;
1500
+ ah.originalMethods.open(request.method, request.url, ...ah.openArgs);
1501
+ for (const header in request.headers) {
1502
+ ah.originalMethods.setRequestHeader(header, request.headers[header]);
1503
+ }
1504
+ data = request.data;
1505
+ xhr.addEventListener("ajaxHooker_responseReady", (e) => {
1506
+ try {
1507
+ if (typeof request.response === "function") {
1508
+ const arg = {
1509
+ finalUrl: xhr.responseURL,
1510
+ status: xhr.status,
1511
+ responseHeaders: {},
1512
+ };
1513
+ for (const line of xhr
1514
+ .getAllResponseHeaders()
1515
+ .trim()
1516
+ .split(/[\r\n]+/)) {
1517
+ const parts = line.split(/:\s*/);
1518
+ if (parts.length === 2) {
1519
+ const lheader = parts[0].toLowerCase();
1520
+ if (arg.responseHeaders[lheader]) {
1521
+ arg.responseHeaders[lheader] += ", " + parts[1];
1522
+ }
1523
+ else {
1524
+ arg.responseHeaders[lheader] = parts[1];
1525
+ }
1526
+ }
1527
+ }
1528
+ xhrResponses.forEach((prop) => {
1529
+ defineProp(arg, prop, () => {
1530
+ return (arg[prop] = ah.originalGetters[prop]());
1531
+ }, (val) => {
1532
+ delete arg[prop];
1533
+ arg[prop] = val;
1534
+ });
1535
+ defineProp(xhr, prop, () => {
1536
+ const val = ah.originalGetters[prop]();
1537
+ xhr.dispatchEvent(new CustomEvent("ajaxHooker_readResponse", {
1538
+ detail: { prop, val },
1539
+ }));
1540
+ return val;
1541
+ });
1542
+ });
1543
+ xhr.addEventListener("ajaxHooker_readResponse", (e) => {
1544
+ arg[e.detail.prop] = e.detail.val;
1545
+ });
1546
+ const resPromise = Promise.resolve(request.response(arg)).then(() => {
1547
+ const task = [];
1548
+ xhrResponses.forEach((prop) => {
1549
+ const descriptor = Object.getOwnPropertyDescriptor(arg, prop);
1550
+ if (descriptor && "value" in descriptor) {
1551
+ task.push(Promise.resolve(descriptor.value).then((val) => {
1552
+ arg[prop] = val;
1553
+ defineProp(xhr, prop, () => {
1554
+ xhr.dispatchEvent(new CustomEvent("ajaxHooker_readResponse", {
1555
+ detail: { prop, val },
1556
+ }));
1557
+ return val;
1558
+ });
1559
+ }, emptyFn));
1560
+ }
1561
+ });
1562
+ return Promise.all(task);
1563
+ }, errorFn);
1564
+ const eventsClone = {};
1565
+ xhrAsyncEvents.forEach((type) => {
1566
+ eventsClone[type] = new Set([...ah.hookedEvents[type]]);
1567
+ eventsClone["on" + type] = ah.hookedEvents["on" + type];
1568
+ });
1569
+ ah.delegateEvent = (event) => resPromise.then(() => {
1570
+ event.stopImmediatePropagation = fakeEventSIP;
1571
+ eventsClone[event.type].forEach((fn) => !event.ajaxHooker_stopped && fn.call(xhr, event));
1572
+ const onEvent = eventsClone["on" + event.type];
1573
+ typeof onEvent === "function" &&
1574
+ onEvent.call(xhr, event);
1575
+ });
1576
+ }
1577
+ }
1578
+ catch (err) {
1579
+ console.error(err);
1580
+ }
1581
+ ah.delegateEvent(e.detail);
1582
+ });
1583
+ realSend(data);
1584
+ });
1585
+ });
1586
+ }
1587
+ catch (err) {
1588
+ console.error(err);
1589
+ realSend(data);
1590
+ }
1591
+ };
1592
+ return xhr;
1593
+ }
1594
+ function hookFetchResponse(response, arg, callback) {
1595
+ fetchResponses.forEach((prop) => {
1596
+ response[prop] = () => new Promise((resolve, reject) => {
1597
+ resProto[prop].call(response).then((res) => {
1598
+ if (prop in arg) {
1599
+ resolve(arg[prop]);
1600
+ }
1601
+ else {
1602
+ try {
1603
+ arg[prop] = res;
1604
+ Promise.resolve(callback(arg)).then(() => {
1605
+ if (prop in arg) {
1606
+ Promise.resolve(arg[prop]).then((val) => resolve((arg[prop] = val)), () => resolve(res));
1607
+ }
1608
+ else {
1609
+ resolve(res);
1610
+ }
1611
+ }, errorFn);
1612
+ }
1613
+ catch (err) {
1614
+ console.error(err);
1615
+ resolve(res);
1616
+ }
1617
+ }
1618
+ }, reject);
1619
+ });
1620
+ });
1621
+ }
1622
+ function fakeFetch(url, init) {
1623
+ if (url && typeof url.toString === "function") {
1624
+ url = url.toString();
1625
+ init = init || {};
1626
+ init.method = init.method || "GET";
1627
+ init.headers = init.headers || {};
1628
+ if (shouldFilter("fetch", url, init.method))
1629
+ return realFetch.call(win, url, init);
1630
+ const request = {
1631
+ type: "fetch",
1632
+ url: url,
1633
+ method: init.method.toUpperCase(),
1634
+ abort: false,
1635
+ headers: {},
1636
+ data: init.body,
1637
+ response: null,
1638
+ };
1639
+ if (toString.call(init.headers) === "[object Headers]") {
1640
+ for (const [key, val] of init.headers) {
1641
+ request.headers[key] = val;
1642
+ }
1643
+ }
1644
+ else {
1645
+ request.headers = { ...init.headers };
1646
+ }
1647
+ const requestClone = { ...request };
1648
+ return new Promise((resolve, reject) => {
1649
+ try {
1650
+ waitForHookFns(request).then(() => {
1651
+ waitForRequestKeys(request, requestClone).then(() => {
1652
+ if (request.abort)
1653
+ return reject("aborted");
1654
+ url = request.url;
1655
+ init.method = request.method;
1656
+ init.headers = request.headers;
1657
+ init.body = request.data;
1658
+ realFetch.call(win, url, init).then((response) => {
1659
+ if (typeof request.response === "function") {
1660
+ const arg = {
1661
+ finalUrl: response.url,
1662
+ status: response.status,
1663
+ responseHeaders: {},
1664
+ };
1665
+ for (const [key, val] of response.headers) {
1666
+ arg.responseHeaders[key] = val;
1667
+ }
1668
+ hookFetchResponse(response, arg, request.response);
1669
+ response.clone = () => {
1670
+ const resClone = resProto.clone.call(response);
1671
+ hookFetchResponse(resClone, arg, request.response);
1672
+ return resClone;
1673
+ };
1674
+ }
1675
+ resolve(response);
1676
+ }, reject);
1677
+ });
1678
+ });
1679
+ }
1680
+ catch (err) {
1681
+ console.error(err);
1682
+ return realFetch.call(win, url, init);
1683
+ }
1684
+ });
1685
+ }
1686
+ else {
1687
+ return realFetch.call(win, url, init);
1688
+ }
1689
+ }
1690
+ win.XMLHttpRequest = fakeXhr;
1691
+ Object.keys(realXhr).forEach((key) => (fakeXhr[key] = realXhr[key]));
1692
+ fakeXhr.prototype = realXhr.prototype;
1693
+ win.fetch = fakeFetch;
1694
+ return {
1695
+ hook: (fn) => hookFns.push(fn),
1696
+ filter: (arr) => {
1697
+ filter = Array.isArray(arr) && arr.map(toFilterObj);
1698
+ },
1699
+ protect: () => {
1700
+ readonly(win, "XMLHttpRequest", fakeXhr);
1701
+ readonly(win, "fetch", fakeFetch);
1702
+ },
1703
+ unhook: () => {
1704
+ writable(win, "XMLHttpRequest", realXhr);
1705
+ writable(win, "fetch", realFetch);
1706
+ },
1707
+ };
1708
+ })();
1715
1709
  };
1716
1710
 
1717
1711
  class GMMenu {
@@ -2129,31 +2123,31 @@
2129
2123
  }
2130
2124
  /**
2131
2125
  * 设置当enable为true时默认显示在菜单中前面的emoji图标
2132
- * @param emojiString
2126
+ * @param emojiString emoji字符串
2133
2127
  */
2134
2128
  setEnableTrueEmoji(emojiString) {
2135
2129
  if (typeof emojiString !== "string") {
2136
- throw new Error("参数emojiString必须是string类型");
2130
+ throw new TypeError("参数emojiString必须是string类型");
2137
2131
  }
2138
2132
  this.MenuHandle.$emoji.success = emojiString;
2139
2133
  }
2140
2134
  /**
2141
2135
  * 设置当enable为false时默认显示在菜单中前面的emoji图标
2142
- * @param emojiString
2136
+ * @param emojiString emoji字符串
2143
2137
  */
2144
2138
  setEnableFalseEmoji(emojiString) {
2145
2139
  if (typeof emojiString !== "string") {
2146
- throw new Error("参数emojiString必须是string类型");
2140
+ throw new TypeError("参数emojiString必须是string类型");
2147
2141
  }
2148
2142
  this.MenuHandle.$emoji.error = emojiString;
2149
2143
  }
2150
2144
  /**
2151
2145
  * 设置本地存储的菜单外部的键名
2152
- * @param keyName
2146
+ * @param keyName 键名
2153
2147
  */
2154
2148
  setLocalStorageKeyName(keyName) {
2155
2149
  if (typeof keyName !== "string") {
2156
- throw new Error("参数keyName必须是string类型");
2150
+ throw new TypeError("参数keyName必须是string类型");
2157
2151
  }
2158
2152
  this.MenuHandle.$data.key = keyName;
2159
2153
  }
@@ -4936,403 +4930,401 @@
4936
4930
  const setInterval = (...args) => loadOrReturnBroker().setInterval(...args);
4937
4931
  const setTimeout$1 = (...args) => loadOrReturnBroker().setTimeout(...args);
4938
4932
 
4939
- /* eslint-disable */
4940
- // ==UserScript==
4941
- // @name ModuleRaid.js
4942
- // @namespace http://tampermonkey.net/
4943
- // @version 6.2.0
4944
- // @description 检索调用webpackJsonp模块,可指定检索的window
4945
- // @author empyrealtear
4946
- // @license MIT
4947
- // @original-script https://github.com/pixeldesu/moduleRaid
4948
- // ==/UserScript==
4949
-
4950
-
4951
- /**
4952
- * Main moduleRaid class
4953
- * @link https://scriptcat.org/zh-CN/script-show-page/2628
4954
- */
4955
- class ModuleRaid {
4956
- /**
4957
- * moduleRaid constructor
4958
- *
4959
- * @example
4960
- * Constructing an instance without any arguments:
4961
- * ```ts
4962
- * const mR = new ModuleRaid()
4963
- * ```
4964
- *
4965
- * Constructing an instance with the optional `opts` object:
4966
- * ```ts
4967
- * const mR = new ModuleRaid({ entrypoint: 'webpackChunk_custom_name' })
4968
- * ```
4969
- *
4970
- * @param opts a object containing options to initialize moduleRaid with
4971
- * - **opts:**
4972
- * - _target_: the window object being searched for
4973
- * - _entrypoint_: the Webpack entrypoint present on the global window object
4974
- * - _debug_: whether debug mode is enabled or not
4975
- * - _strict_: whether strict mode is enabled or not
4976
- */
4977
- constructor(opts) {
4978
- /**
4979
- * A random generated module ID we use for injecting into Webpack
4980
- */
4981
- this.moduleID = Math.random().toString(36).substring(7);
4982
- /**
4983
- * An array containing different argument injection methods for
4984
- * Webpack (before version 4), and subsequently pulling out methods and modules
4985
- * @internal
4986
- */
4987
- this.functionArguments = [
4988
- [
4989
- [0],
4990
- [
4991
- (_e, _t, i) => {
4992
- this.modules = i.c;
4993
- this.constructors = i.m;
4994
- this.get = i;
4995
- },
4996
- ],
4997
- ],
4998
- [
4999
- [1e3],
5000
- {
5001
- [this.moduleID]: (_e, _t, i) => {
5002
- this.modules = i.c;
5003
- this.constructors = i.m;
5004
- this.get = i;
5005
- },
5006
- },
5007
- [[this.moduleID]],
5008
- ],
5009
- ];
5010
- /**
5011
- * An array containing different argument injection methods for
5012
- * Webpack (after version 4), and subsequently pulling out methods and modules
5013
- * @internal
5014
- */
5015
- this.arrayArguments = [
5016
- [
5017
- [this.moduleID],
5018
- {},
5019
- (e) => {
5020
- const mCac = e.m;
5021
- Object.keys(mCac).forEach((mod) => {
5022
- try {
5023
- this.modules[mod] = e(mod);
5024
- }
5025
- catch (err) {
5026
- this.log(`[arrayArguments/1] Failed to require(${mod}) with error:\n${err}\n${err.stack}`);
5027
- }
5028
- });
5029
- this.get = e;
5030
- },
5031
- ],
5032
- this.functionArguments[1],
5033
- ];
5034
- /**
5035
- * Storage for the modules we extracted from Webpack
5036
- */
5037
- this.modules = {};
5038
- /**
5039
- * Storage for the constructors we extracted from Webpack
5040
- */
5041
- this.constructors = [];
5042
- let options = {
5043
- target: window,
5044
- entrypoint: 'webpackJsonp',
5045
- debug: false,
5046
- strict: false,
5047
- };
5048
- if (typeof opts === 'object') {
5049
- options = Object.assign(Object.assign({}, options), opts);
5050
- }
5051
- this.target = options.target;
5052
- this.entrypoint = options.entrypoint;
5053
- this.debug = options.debug;
5054
- this.strict = options.strict;
5055
- this.detectEntrypoint();
5056
- this.fillModules();
5057
- this.replaceGet();
5058
- this.setupPushEvent();
5059
- }
5060
- /**
5061
- * Debug logging method, outputs to the console when {@link ModuleRaid.debug} is true
5062
- *
5063
- * @param {*} message The message to be logged
5064
- * @internal
5065
- */
5066
- log(message) {
5067
- if (this.debug) {
5068
- console.warn(`[moduleRaid] ${message}`);
5069
- }
5070
- }
5071
- /**
5072
- * Method to set an alternative getter if we weren't able to extract __webpack_require__
5073
- * from Webpack
5074
- * @internal
5075
- */
5076
- replaceGet() {
5077
- if (this.get === null) {
5078
- this.get = (key) => this.modules[key];
5079
- }
5080
- }
5081
- /**
5082
- * Method that will try to inject a module into Webpack or get modules
5083
- * depending on it's success it might be more or less brute about it
5084
- * @internal
5085
- */
5086
- fillModules() {
5087
- if (typeof this.target[this.entrypoint] === 'function') {
5088
- this.functionArguments.forEach((argument, index) => {
5089
- try {
5090
- if (this.modules && Object.keys(this.modules).length > 0)
5091
- return;
5092
- this.target[this.entrypoint](...argument);
5093
- }
5094
- catch (err) {
5095
- this.log(`moduleRaid.functionArguments[${index}] failed:\n${err}\n${err.stack}`);
5096
- }
5097
- });
5098
- }
5099
- else {
5100
- this.arrayArguments.forEach((argument, index) => {
5101
- try {
5102
- if (this.modules && Object.keys(this.modules).length > 0)
5103
- return;
5104
- this.target[this.entrypoint].push(argument);
5105
- }
5106
- catch (err) {
5107
- this.log(`Pushing moduleRaid.arrayArguments[${index}] into ${this.entrypoint} failed:\n${err}\n${err.stack}`);
5108
- }
5109
- });
5110
- }
5111
- if (this.modules && Object.keys(this.modules).length == 0) {
5112
- let moduleEnd = false;
5113
- let moduleIterator = 0;
5114
- if (typeof this.target[this.entrypoint] != 'function' || !this.target[this.entrypoint]([], [], [moduleIterator])) {
5115
- throw Error('Unknown Webpack structure');
5116
- }
5117
- while (!moduleEnd) {
5118
- try {
5119
- this.modules[moduleIterator] = this.target[this.entrypoint]([], [], [moduleIterator]);
5120
- moduleIterator++;
5121
- }
5122
- catch (err) {
5123
- moduleEnd = true;
5124
- }
5125
- }
5126
- }
5127
- }
5128
- /**
5129
- * Method to hook into `window[this.entrypoint].push` adding a listener for new
5130
- * chunks being pushed into Webpack
5131
- *
5132
- * @example
5133
- * You can listen for newly pushed packages using the `moduleraid:webpack-push` event
5134
- * on `document`
5135
- *
5136
- * ```ts
5137
- * document.addEventListener('moduleraid:webpack-push', (e) => {
5138
- * // e.detail contains the arguments push() was called with
5139
- * console.log(e.detail)
5140
- * })
5141
- * ```
5142
- * @internal
5143
- */
5144
- setupPushEvent() {
5145
- const originalPush = this.target[this.entrypoint].push;
5146
- this.target[this.entrypoint].push = (...args) => {
5147
- const result = Reflect.apply(originalPush, this.target[this.entrypoint], args);
5148
- document.dispatchEvent(new CustomEvent('moduleraid:webpack-push', { detail: args }));
5149
- return result;
5150
- };
5151
- }
5152
- /**
5153
- * Method to try autodetecting a Webpack JSONP entrypoint based on common naming
5154
- *
5155
- * If the default entrypoint, or the entrypoint that's passed to the moduleRaid constructor
5156
- * already matches, the method exits early
5157
- *
5158
- * If `options.strict` has been set in the constructor and the initial entrypoint cannot
5159
- * be found, this method will error, demanding a strictly set entrypoint
5160
- * @internal
5161
- */
5162
- detectEntrypoint() {
5163
- if (this.target[this.entrypoint] != undefined) {
5164
- return;
5165
- }
5166
- if (this.strict) {
5167
- throw Error(`Strict mode is enabled and entrypoint at window.${this.entrypoint} couldn't be found. Please specify the correct one!`);
5168
- }
5169
- let windowObjects = Object.keys(this.target);
5170
- windowObjects = windowObjects
5171
- .filter((object) => object.toLowerCase().includes('chunk') || object.toLowerCase().includes('webpack'))
5172
- .filter((object) => typeof this.target[object] === 'function' || Array.isArray(this.target[object]));
5173
- if (windowObjects.length > 1) {
5174
- throw Error(`Multiple possible endpoints have been detected, please create a new moduleRaid instance with a specific one:\n${windowObjects.join(', ')}`);
5175
- }
5176
- if (windowObjects.length === 0) {
5177
- throw Error('No Webpack JSONP entrypoints could be detected');
5178
- }
5179
- this.log(`Entrypoint has been detected at window.${windowObjects[0]} and set for injection`);
5180
- this.entrypoint = windowObjects[0];
5181
- }
5182
- /**
5183
- * Recursive object-search function for modules
5184
- *
5185
- * @param object the object to search through
5186
- * @param query the query the object keys/values are searched for
5187
- * @returns boolean state of `object` containing `query` somewhere in it
5188
- * @internal
5189
- */
5190
- searchObject(object, query) {
5191
- for (const key in object) {
5192
- const value = object[key];
5193
- const lowerCaseQuery = query.toLowerCase();
5194
- if (typeof value != 'object') {
5195
- const lowerCaseKey = key.toString().toLowerCase();
5196
- if (lowerCaseKey.includes(lowerCaseQuery))
5197
- return true;
5198
- if (typeof value != 'object') {
5199
- const lowerCaseValue = value.toString().toLowerCase();
5200
- if (lowerCaseValue.includes(lowerCaseQuery))
5201
- return true;
5202
- }
5203
- else {
5204
- if (this.searchObject(value, query))
5205
- return true;
5206
- }
5207
- }
5208
- }
5209
- return false;
5210
- }
5211
- /**
5212
- * Method to search through the module object, searching for the fitting content
5213
- * if a string is supplied
5214
- *
5215
- * If query is supplied as a function, everything that returns true when passed
5216
- * to the query function will be returned
5217
- *
5218
- * @example
5219
- * With a string as query argument:
5220
- * ```ts
5221
- * const results = mR.findModule('feature')
5222
- * // => Array of module results
5223
- * ```
5224
- *
5225
- * With a function as query argument:
5226
- * ```ts
5227
- * const results = mR.findModule((module) => { typeof module === 'function' })
5228
- * // => Array of module results
5229
- * ```
5230
- *
5231
- * @param query query to search the module list for
5232
- * @return a list of modules fitting the query
5233
- */
5234
- findModule(query) {
5235
- const results = [];
5236
- const modules = Object.keys(this.modules);
5237
- if (modules.length === 0) {
5238
- throw new Error('There are no modules to search through!');
5239
- }
5240
- modules.forEach((key) => {
5241
- const module = this.modules[key.toString()];
5242
- if (module === undefined)
5243
- return;
5244
- try {
5245
- if (typeof query === 'string') {
5246
- query = query.toLowerCase();
5247
- switch (typeof module) {
5248
- case 'string':
5249
- if (module.toLowerCase().includes(query))
5250
- results.push(module);
5251
- break;
5252
- case 'function':
5253
- if (module.toString().toLowerCase().includes(query))
5254
- results.push(module);
5255
- break;
5256
- case 'object':
5257
- if (this.searchObject(module, query))
5258
- results.push(module);
5259
- break;
5260
- }
5261
- }
5262
- else if (typeof query === 'function') {
5263
- if (query(module))
5264
- results.push(module);
5265
- }
5266
- else {
5267
- throw new TypeError(`findModule can only find via string and function, ${typeof query} was passed`);
5268
- }
5269
- }
5270
- catch (err) {
5271
- this.log(`There was an error while searching through module '${key}':\n${err}\n${err.stack}`);
5272
- }
5273
- });
5274
- return results;
5275
- }
5276
- /**
5277
- * Method to search through the constructor array, searching for the fitting content
5278
- * if a string is supplied
5279
- *
5280
- * If query is supplied as a function, everything that returns true when passed
5281
- * to the query function will be returned
5282
- *
5283
- * @example
5284
- * With a string as query argument:
5285
- * ```ts
5286
- * const results = mR.findConstructor('feature')
5287
- * // => Array of constructor/module tuples
5288
- * ```
5289
- *
5290
- * With a function as query argument:
5291
- * ```ts
5292
- * const results = mR.findConstructor((constructor) => { constructor.prototype.value !== undefined })
5293
- * // => Array of constructor/module tuples
5294
- * ```
5295
- *
5296
- * Accessing the resulting data:
5297
- * ```ts
5298
- * // With array destructuring (ES6)
5299
- * const [constructor, module] = results[0]
5300
- *
5301
- * // ...or...
5302
- *
5303
- * // regular access
5304
- * const constructor = results[0][0]
5305
- * const module = results[0][1]
5306
- * ```
5307
- *
5308
- * @param query query to search the constructor list for
5309
- * @returns a list of constructor/module tuples fitting the query
5310
- */
5311
- findConstructor(query) {
5312
- const results = [];
5313
- const constructors = Object.keys(this.constructors);
5314
- if (constructors.length === 0) {
5315
- throw new Error('There are no constructors to search through!');
5316
- }
5317
- constructors.forEach((key) => {
5318
- const constructor = this.constructors[key];
5319
- try {
5320
- if (typeof query === 'string') {
5321
- query = query.toLowerCase();
5322
- if (constructor.toString().toLowerCase().includes(query))
5323
- results.push([this.constructors[key], this.modules[key]]);
5324
- }
5325
- else if (typeof query === 'function') {
5326
- if (query(constructor))
5327
- results.push([this.constructors[key], this.modules[key]]);
5328
- }
5329
- }
5330
- catch (err) {
5331
- this.log(`There was an error while searching through constructor '${key}':\n${err}\n${err.stack}`);
5332
- }
5333
- });
5334
- return results;
5335
- }
4933
+ /* eslint-disable */
4934
+ // ==UserScript==
4935
+ // @name ModuleRaid.js
4936
+ // @namespace http://tampermonkey.net/
4937
+ // @version 6.2.0
4938
+ // @description 检索调用webpackJsonp模块,可指定检索的window
4939
+ // @author empyrealtear
4940
+ // @license MIT
4941
+ // @original-script https://github.com/pixeldesu/moduleRaid
4942
+ // ==/UserScript==
4943
+ /**
4944
+ * Main moduleRaid class
4945
+ * @link https://scriptcat.org/zh-CN/script-show-page/2628
4946
+ */
4947
+ class ModuleRaid {
4948
+ /**
4949
+ * moduleRaid constructor
4950
+ *
4951
+ * @example
4952
+ * Constructing an instance without any arguments:
4953
+ * ```ts
4954
+ * const mR = new ModuleRaid()
4955
+ * ```
4956
+ *
4957
+ * Constructing an instance with the optional `opts` object:
4958
+ * ```ts
4959
+ * const mR = new ModuleRaid({ entrypoint: 'webpackChunk_custom_name' })
4960
+ * ```
4961
+ *
4962
+ * @param opts a object containing options to initialize moduleRaid with
4963
+ * - **opts:**
4964
+ * - _target_: the window object being searched for
4965
+ * - _entrypoint_: the Webpack entrypoint present on the global window object
4966
+ * - _debug_: whether debug mode is enabled or not
4967
+ * - _strict_: whether strict mode is enabled or not
4968
+ */
4969
+ constructor(opts) {
4970
+ /**
4971
+ * A random generated module ID we use for injecting into Webpack
4972
+ */
4973
+ this.moduleID = Math.random().toString(36).substring(7);
4974
+ /**
4975
+ * An array containing different argument injection methods for
4976
+ * Webpack (before version 4), and subsequently pulling out methods and modules
4977
+ * @internal
4978
+ */
4979
+ this.functionArguments = [
4980
+ [
4981
+ [0],
4982
+ [
4983
+ (_e, _t, i) => {
4984
+ this.modules = i.c;
4985
+ this.constructors = i.m;
4986
+ this.get = i;
4987
+ },
4988
+ ],
4989
+ ],
4990
+ [
4991
+ [1e3],
4992
+ {
4993
+ [this.moduleID]: (_e, _t, i) => {
4994
+ this.modules = i.c;
4995
+ this.constructors = i.m;
4996
+ this.get = i;
4997
+ },
4998
+ },
4999
+ [[this.moduleID]],
5000
+ ],
5001
+ ];
5002
+ /**
5003
+ * An array containing different argument injection methods for
5004
+ * Webpack (after version 4), and subsequently pulling out methods and modules
5005
+ * @internal
5006
+ */
5007
+ this.arrayArguments = [
5008
+ [
5009
+ [this.moduleID],
5010
+ {},
5011
+ (e) => {
5012
+ const mCac = e.m;
5013
+ Object.keys(mCac).forEach((mod) => {
5014
+ try {
5015
+ this.modules[mod] = e(mod);
5016
+ }
5017
+ catch (err) {
5018
+ this.log(`[arrayArguments/1] Failed to require(${mod}) with error:\n${err}\n${err.stack}`);
5019
+ }
5020
+ });
5021
+ this.get = e;
5022
+ },
5023
+ ],
5024
+ this.functionArguments[1],
5025
+ ];
5026
+ /**
5027
+ * Storage for the modules we extracted from Webpack
5028
+ */
5029
+ this.modules = {};
5030
+ /**
5031
+ * Storage for the constructors we extracted from Webpack
5032
+ */
5033
+ this.constructors = [];
5034
+ let options = {
5035
+ target: window,
5036
+ entrypoint: 'webpackJsonp',
5037
+ debug: false,
5038
+ strict: false,
5039
+ };
5040
+ if (typeof opts === 'object') {
5041
+ options = Object.assign(Object.assign({}, options), opts);
5042
+ }
5043
+ this.target = options.target;
5044
+ this.entrypoint = options.entrypoint;
5045
+ this.debug = options.debug;
5046
+ this.strict = options.strict;
5047
+ this.detectEntrypoint();
5048
+ this.fillModules();
5049
+ this.replaceGet();
5050
+ this.setupPushEvent();
5051
+ }
5052
+ /**
5053
+ * Debug logging method, outputs to the console when {@link ModuleRaid.debug} is true
5054
+ *
5055
+ * @param {*} message The message to be logged
5056
+ * @internal
5057
+ */
5058
+ log(message) {
5059
+ if (this.debug) {
5060
+ console.warn(`[moduleRaid] ${message}`);
5061
+ }
5062
+ }
5063
+ /**
5064
+ * Method to set an alternative getter if we weren't able to extract __webpack_require__
5065
+ * from Webpack
5066
+ * @internal
5067
+ */
5068
+ replaceGet() {
5069
+ if (this.get === null) {
5070
+ this.get = (key) => this.modules[key];
5071
+ }
5072
+ }
5073
+ /**
5074
+ * Method that will try to inject a module into Webpack or get modules
5075
+ * depending on it's success it might be more or less brute about it
5076
+ * @internal
5077
+ */
5078
+ fillModules() {
5079
+ if (typeof this.target[this.entrypoint] === 'function') {
5080
+ this.functionArguments.forEach((argument, index) => {
5081
+ try {
5082
+ if (this.modules && Object.keys(this.modules).length > 0)
5083
+ return;
5084
+ this.target[this.entrypoint](...argument);
5085
+ }
5086
+ catch (err) {
5087
+ this.log(`moduleRaid.functionArguments[${index}] failed:\n${err}\n${err.stack}`);
5088
+ }
5089
+ });
5090
+ }
5091
+ else {
5092
+ this.arrayArguments.forEach((argument, index) => {
5093
+ try {
5094
+ if (this.modules && Object.keys(this.modules).length > 0)
5095
+ return;
5096
+ this.target[this.entrypoint].push(argument);
5097
+ }
5098
+ catch (err) {
5099
+ this.log(`Pushing moduleRaid.arrayArguments[${index}] into ${this.entrypoint} failed:\n${err}\n${err.stack}`);
5100
+ }
5101
+ });
5102
+ }
5103
+ if (this.modules && Object.keys(this.modules).length == 0) {
5104
+ let moduleEnd = false;
5105
+ let moduleIterator = 0;
5106
+ if (typeof this.target[this.entrypoint] != 'function' || !this.target[this.entrypoint]([], [], [moduleIterator])) {
5107
+ throw Error('Unknown Webpack structure');
5108
+ }
5109
+ while (!moduleEnd) {
5110
+ try {
5111
+ this.modules[moduleIterator] = this.target[this.entrypoint]([], [], [moduleIterator]);
5112
+ moduleIterator++;
5113
+ }
5114
+ catch (err) {
5115
+ moduleEnd = true;
5116
+ }
5117
+ }
5118
+ }
5119
+ }
5120
+ /**
5121
+ * Method to hook into `window[this.entrypoint].push` adding a listener for new
5122
+ * chunks being pushed into Webpack
5123
+ *
5124
+ * @example
5125
+ * You can listen for newly pushed packages using the `moduleraid:webpack-push` event
5126
+ * on `document`
5127
+ *
5128
+ * ```ts
5129
+ * document.addEventListener('moduleraid:webpack-push', (e) => {
5130
+ * // e.detail contains the arguments push() was called with
5131
+ * console.log(e.detail)
5132
+ * })
5133
+ * ```
5134
+ * @internal
5135
+ */
5136
+ setupPushEvent() {
5137
+ const originalPush = this.target[this.entrypoint].push;
5138
+ this.target[this.entrypoint].push = (...args) => {
5139
+ const result = Reflect.apply(originalPush, this.target[this.entrypoint], args);
5140
+ document.dispatchEvent(new CustomEvent('moduleraid:webpack-push', { detail: args }));
5141
+ return result;
5142
+ };
5143
+ }
5144
+ /**
5145
+ * Method to try autodetecting a Webpack JSONP entrypoint based on common naming
5146
+ *
5147
+ * If the default entrypoint, or the entrypoint that's passed to the moduleRaid constructor
5148
+ * already matches, the method exits early
5149
+ *
5150
+ * If `options.strict` has been set in the constructor and the initial entrypoint cannot
5151
+ * be found, this method will error, demanding a strictly set entrypoint
5152
+ * @internal
5153
+ */
5154
+ detectEntrypoint() {
5155
+ if (this.target[this.entrypoint] != undefined) {
5156
+ return;
5157
+ }
5158
+ if (this.strict) {
5159
+ throw Error(`Strict mode is enabled and entrypoint at window.${this.entrypoint} couldn't be found. Please specify the correct one!`);
5160
+ }
5161
+ let windowObjects = Object.keys(this.target);
5162
+ windowObjects = windowObjects
5163
+ .filter((object) => object.toLowerCase().includes('chunk') || object.toLowerCase().includes('webpack'))
5164
+ .filter((object) => typeof this.target[object] === 'function' || Array.isArray(this.target[object]));
5165
+ if (windowObjects.length > 1) {
5166
+ throw Error(`Multiple possible endpoints have been detected, please create a new moduleRaid instance with a specific one:\n${windowObjects.join(', ')}`);
5167
+ }
5168
+ if (windowObjects.length === 0) {
5169
+ throw Error('No Webpack JSONP entrypoints could be detected');
5170
+ }
5171
+ this.log(`Entrypoint has been detected at window.${windowObjects[0]} and set for injection`);
5172
+ this.entrypoint = windowObjects[0];
5173
+ }
5174
+ /**
5175
+ * Recursive object-search function for modules
5176
+ *
5177
+ * @param object the object to search through
5178
+ * @param query the query the object keys/values are searched for
5179
+ * @returns boolean state of `object` containing `query` somewhere in it
5180
+ * @internal
5181
+ */
5182
+ searchObject(object, query) {
5183
+ for (const key in object) {
5184
+ const value = object[key];
5185
+ const lowerCaseQuery = query.toLowerCase();
5186
+ if (typeof value != 'object') {
5187
+ const lowerCaseKey = key.toString().toLowerCase();
5188
+ if (lowerCaseKey.includes(lowerCaseQuery))
5189
+ return true;
5190
+ if (typeof value != 'object') {
5191
+ const lowerCaseValue = value.toString().toLowerCase();
5192
+ if (lowerCaseValue.includes(lowerCaseQuery))
5193
+ return true;
5194
+ }
5195
+ else {
5196
+ if (this.searchObject(value, query))
5197
+ return true;
5198
+ }
5199
+ }
5200
+ }
5201
+ return false;
5202
+ }
5203
+ /**
5204
+ * Method to search through the module object, searching for the fitting content
5205
+ * if a string is supplied
5206
+ *
5207
+ * If query is supplied as a function, everything that returns true when passed
5208
+ * to the query function will be returned
5209
+ *
5210
+ * @example
5211
+ * With a string as query argument:
5212
+ * ```ts
5213
+ * const results = mR.findModule('feature')
5214
+ * // => Array of module results
5215
+ * ```
5216
+ *
5217
+ * With a function as query argument:
5218
+ * ```ts
5219
+ * const results = mR.findModule((module) => { typeof module === 'function' })
5220
+ * // => Array of module results
5221
+ * ```
5222
+ *
5223
+ * @param query query to search the module list for
5224
+ * @return a list of modules fitting the query
5225
+ */
5226
+ findModule(query) {
5227
+ const results = [];
5228
+ const modules = Object.keys(this.modules);
5229
+ if (modules.length === 0) {
5230
+ throw new Error('There are no modules to search through!');
5231
+ }
5232
+ modules.forEach((key) => {
5233
+ const module = this.modules[key.toString()];
5234
+ if (module === undefined)
5235
+ return;
5236
+ try {
5237
+ if (typeof query === 'string') {
5238
+ query = query.toLowerCase();
5239
+ switch (typeof module) {
5240
+ case 'string':
5241
+ if (module.toLowerCase().includes(query))
5242
+ results.push(module);
5243
+ break;
5244
+ case 'function':
5245
+ if (module.toString().toLowerCase().includes(query))
5246
+ results.push(module);
5247
+ break;
5248
+ case 'object':
5249
+ if (this.searchObject(module, query))
5250
+ results.push(module);
5251
+ break;
5252
+ }
5253
+ }
5254
+ else if (typeof query === 'function') {
5255
+ if (query(module))
5256
+ results.push(module);
5257
+ }
5258
+ else {
5259
+ throw new TypeError(`findModule can only find via string and function, ${typeof query} was passed`);
5260
+ }
5261
+ }
5262
+ catch (err) {
5263
+ this.log(`There was an error while searching through module '${key}':\n${err}\n${err.stack}`);
5264
+ }
5265
+ });
5266
+ return results;
5267
+ }
5268
+ /**
5269
+ * Method to search through the constructor array, searching for the fitting content
5270
+ * if a string is supplied
5271
+ *
5272
+ * If query is supplied as a function, everything that returns true when passed
5273
+ * to the query function will be returned
5274
+ *
5275
+ * @example
5276
+ * With a string as query argument:
5277
+ * ```ts
5278
+ * const results = mR.findConstructor('feature')
5279
+ * // => Array of constructor/module tuples
5280
+ * ```
5281
+ *
5282
+ * With a function as query argument:
5283
+ * ```ts
5284
+ * const results = mR.findConstructor((constructor) => { constructor.prototype.value !== undefined })
5285
+ * // => Array of constructor/module tuples
5286
+ * ```
5287
+ *
5288
+ * Accessing the resulting data:
5289
+ * ```ts
5290
+ * // With array destructuring (ES6)
5291
+ * const [constructor, module] = results[0]
5292
+ *
5293
+ * // ...or...
5294
+ *
5295
+ * // regular access
5296
+ * const constructor = results[0][0]
5297
+ * const module = results[0][1]
5298
+ * ```
5299
+ *
5300
+ * @param query query to search the constructor list for
5301
+ * @returns a list of constructor/module tuples fitting the query
5302
+ */
5303
+ findConstructor(query) {
5304
+ const results = [];
5305
+ const constructors = Object.keys(this.constructors);
5306
+ if (constructors.length === 0) {
5307
+ throw new Error('There are no constructors to search through!');
5308
+ }
5309
+ constructors.forEach((key) => {
5310
+ const constructor = this.constructors[key];
5311
+ try {
5312
+ if (typeof query === 'string') {
5313
+ query = query.toLowerCase();
5314
+ if (constructor.toString().toLowerCase().includes(query))
5315
+ results.push([this.constructors[key], this.modules[key]]);
5316
+ }
5317
+ else if (typeof query === 'function') {
5318
+ if (query(constructor))
5319
+ results.push([this.constructors[key], this.modules[key]]);
5320
+ }
5321
+ }
5322
+ catch (err) {
5323
+ this.log(`There was an error while searching through constructor '${key}':\n${err}\n${err.stack}`);
5324
+ }
5325
+ });
5326
+ return results;
5327
+ }
5336
5328
  }
5337
5329
 
5338
5330
  class DOMUtils {
@@ -5504,7 +5496,7 @@
5504
5496
  }
5505
5497
  const domUtils = new DOMUtils();
5506
5498
 
5507
- const version = "2.9.4";
5499
+ const version = "2.9.6";
5508
5500
 
5509
5501
  class Utils {
5510
5502
  windowApi;