-
Notifications
You must be signed in to change notification settings - Fork 388
Expand file tree
/
Copy pathmv3_utils.ts
More file actions
209 lines (195 loc) · 7.56 KB
/
Copy pathmv3_utils.ts
File metadata and controls
209 lines (195 loc) · 7.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import { stackAsyncTask } from "@App/pkg/utils/async_queue";
import { isFirefox } from "@App/pkg/utils/utils";
import { type FetchXHR } from "@App/pkg/utils/xhr/fetch_xhr";
import { scXhrRequests } from "./gm_xhr";
import { INTERNAL_DNR_PRIORITY } from "../dnr_rule_ids";
const bFirefox = isFirefox();
type TbgMarkerMapEntry = {
markerID: string;
reqId?: string;
r?: any;
url: string;
resolvePromise: (r: any) => void;
};
const bgMarkerMap = new Map<string, TbgMarkerMapEntry>();
export const normalizeBackgroundRequestUrl = (url: string) => {
const u = new URL(url);
// input "https://user:passwd@httpbingo.org/basic-auth/user/passwd?q=1&r=2"
// output "https://httpbingo.org/basic-auth/user/passwd?q=1&r=2"
return `${u.origin}${u.pathname}${u.search}`;
};
export type IWebRequestDetails = {
/** The value 0 indicates that the request happens in the main frame; a positive value indicates the ID of a subframe in which the request happens. If the document of a (sub-)frame is loaded (`type` is `main_frame` or `sub_frame`), `frameId` indicates the ID of this frame, not the ID of the outer frame. Frame IDs are unique within a tab. */
frameId: number;
/** Standard HTTP method. */
method: string;
/** ID of frame that wraps the frame which sent the request. Set to -1 if no parent frame exists. */
parentFrameId: number;
/** The ID of the request. Request IDs are unique within a browser session. As a result, they could be used to relate different events of the same request. */
requestId: string;
/** The ID of the tab in which the request takes place. Set to -1 if the request isn't related to a tab. */
tabId: number;
/** The time when this signal is triggered, in milliseconds since the epoch. */
timeStamp: number;
/** How the requested resource will be used. */
type: `${chrome.declarativeNetRequest.ResourceType}`;
url: string;
/** [Chrome 63+] The origin where the request was initiated. This does not change through redirects. If this is an opaque origin, the string 'null' will be used. */
initiator?: string; // chrome MV3
/** [Firefox 54+] URL of the document in which the resource will be loaded. */
documentUrl?: string; // firefox MV3
/** [Firefox 48+] URL of the resource which triggered the request. */
originUrl?: string; // firefox MV3
};
/** Minimum Requirements: Chrome 63+ or Firefox 54+ */
export const isRequestInitiatorOriginMatched = (request: IWebRequestDetails, targetOrigin: string) => {
let url: string | undefined;
try {
if (typeof request.initiator === "string") {
url = request.initiator;
} else if (typeof request.documentUrl === "string" && typeof request.originUrl === "string") {
url = request.originUrl;
}
if (url && url.length > 8 && targetOrigin && targetOrigin.length > 8) {
// avoid "null"; requires something like "abc://def"
return url.startsWith(targetOrigin);
}
} catch (e) {
console.error(e);
}
return false;
};
type SetupParams = {
cleanupOnAPIError: (requestId: string) => void;
};
type SendContext = {
markerID: string;
url: string;
};
export interface GmXhrRequestLinker {
setup(params: SetupParams): void;
prepareRequest(details: GMSend.XHRDetails, headers: { [key: string]: string }, markerID: string): void;
send(
baseXHR: FetchXHR | XMLHttpRequest,
data: XMLHttpRequestBodyInit | null | undefined,
context: SendContext
): Promise<any> | any;
}
const resolver = (o: TbgMarkerMapEntry) => {
const resolvePromise = o.resolvePromise;
const result = o.r;
bgMarkerMap.delete(o.url);
bgMarkerMap.delete(o.markerID);
scXhrRequests.set(o.reqId!, o.markerID);
scXhrRequests.set(o.markerID, o.reqId!);
o.r = null;
resolvePromise(result);
};
export class FirefoxWebRequestLinker implements GmXhrRequestLinker {
private readonly currentOrigin: string = new URL(chrome.runtime.getURL("/")).origin;
setup({ cleanupOnAPIError }: SetupParams) {
chrome.webRequest.onBeforeRequest.addListener(
(details) => {
const lastError = chrome.runtime.lastError;
if (lastError) {
console.error("chrome.runtime.lastError in chrome.webRequest.onBeforeRequest:", lastError);
// webRequest API 出错不进行后续处理
cleanupOnAPIError(details?.requestId);
return undefined;
}
if (details.tabId === -1 && isRequestInitiatorOriginMatched(details, this.currentOrigin)) {
const wURL = normalizeBackgroundRequestUrl(details.url);
const o = bgMarkerMap.get(wURL);
if (o) {
bgMarkerMap.delete(wURL);
o.reqId = details.requestId;
resolver(o);
} else if (scXhrRequests.has(details.requestId)) {
// redirection to new url
} else {
console.error(`onBeforeRequest: No marker ID record for ${wURL}`);
}
}
},
{
urls: ["<all_urls>"],
types: ["xmlhttprequest"],
tabId: chrome.tabs.TAB_ID_NONE, // 只限于后台 service_worker / offscreen
}
);
}
prepareRequest(_details: GMSend.XHRDetails, _headers: { [key: string]: string }, _markerID: string) {
// Firefox links background requests through webRequest requestId capture.
}
async send(
baseXHR: FetchXHR | XMLHttpRequest,
data: XMLHttpRequestBodyInit | null | undefined,
{ markerID, url }: SendContext
) {
// Send data (if any)
if (!markerID) return baseXHR.send(data);
const fn = (resolve: any, _reject: any) => {
const wURL = normalizeBackgroundRequestUrl(url);
const o: TbgMarkerMapEntry = {
url: wURL,
markerID,
resolvePromise: resolve,
};
bgMarkerMap.delete(markerID);
bgMarkerMap.delete(wURL);
bgMarkerMap.set(markerID, o);
bgMarkerMap.set(wURL, o);
// Send data (if any)
o.r = baseXHR.send(data);
};
return await stackAsyncTask("bg_gm_xhr_queue", () => new Promise(fn));
}
}
export class ChromiumHeaderMarkerLinker implements GmXhrRequestLinker {
setup(_params: SetupParams) {
const ruleId = 999;
const rule = {
id: ruleId,
action: {
type: "modifyHeaders",
requestHeaders: [
{
header: "x-sc-request-marker",
operation: "remove",
},
] satisfies chrome.declarativeNetRequest.ModifyHeaderInfo[],
},
priority: INTERNAL_DNR_PRIORITY,
condition: {
resourceTypes: ["xmlhttprequest"],
tabIds: [chrome.tabs.TAB_ID_NONE], // 只限于后台 service_worker / offscreen
},
} as chrome.declarativeNetRequest.Rule;
chrome.declarativeNetRequest.updateSessionRules(
{
removeRuleIds: [ruleId],
addRules: [rule],
},
() => {
const lastError = chrome.runtime.lastError;
if (lastError) {
console.error("chrome.declarativeNetRequest.updateSessionRules:", lastError);
}
}
);
}
prepareRequest(_details: GMSend.XHRDetails, headers: { [key: string]: string }, markerID: string) {
// HTTP/1.1 and HTTP/2
// https://www.rfc-editor.org/rfc/rfc7540#section-8.1.2
// https://datatracker.ietf.org/doc/html/rfc6648
// All header names in HTTP/2 are lower case, and CF will convert if needed.
// All headers comparisons in HTTP/1.1 should be case insensitive.
headers["x-sc-request-marker"] = `${markerID}`;
}
send(baseXHR: FetchXHR | XMLHttpRequest, data: XMLHttpRequestBodyInit | null | undefined, _context?: SendContext) {
return baseXHR.send(data);
}
}
export const gmXhrRequestLinker: GmXhrRequestLinker = bFirefox
? new FirefoxWebRequestLinker()
: new ChromiumHeaderMarkerLinker();