forked from darkreader/darkreader
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfetch.ts
More file actions
28 lines (24 loc) · 803 Bytes
/
Copy pathfetch.ts
File metadata and controls
28 lines (24 loc) · 803 Bytes
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
const throwCORSError = async (url: string) => {
return Promise.reject(new Error(
[
'Embedded Dark Reader cannot access a cross-origin resource',
url,
'Overview your URLs and CORS policies or use',
'`DarkReader.setFetchMethod(fetch: (url) => Promise<Response>))`.',
'See if using `DarkReader.setFetchMethod(window.fetch)`',
'before `DarkReader.enable()` works.'
].join(' '),
));
};
type Fetcher = (url: string) => Promise<Response>;
let fetcher: Fetcher = throwCORSError;
export function setFetchMethod(fetch: Fetcher) {
if (fetch) {
fetcher = fetch;
} else {
fetcher = throwCORSError;
}
}
export async function callFetchMethod(url: string) {
return await fetcher(url);
}