-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice_worker.js
119 lines (112 loc) · 3.92 KB
/
service_worker.js
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
chrome.storage.sync
.get({
match: "https://my-source.com",
replace: "http://localhost:8080",
wds: true,
webServerPort: 8080,
compressed: false
})
.then(async ({ match, replace, wds, webServerPort, compressed }) => {
const replaceURL = new URL(replace);
const likelyWebServerURL = `${replaceURL.protocol}//${replaceURL.hostname}:${webServerPort}`;
const isWDS = wds === "true";
const isCompressed = compressed === "true";
const matchForRegex = match.replaceAll(/\./g, '\\.');
const likelyWebServerURLForRegex = likelyWebServerURL.replaceAll(/\./g, '\\.');
const genRegex = `^${matchForRegex}.*\/static\/([a-z]+)\/([^/]*)\\.[a-fA-F0-9]+\\..*$`;
const appAssetRegex = `^${matchForRegex}.*\/static\/([a-z]+)\/([a-z]+)\/(.*)\\.[a-fA-F0-9]+\\..*$`
const appAssetNoHashRegex = `^${matchForRegex}.*\/static\/([a-z]+)\/([a-z]+)\/(.*)\\..*$`
const oldRules = await chrome.declarativeNetRequest.getDynamicRules();
const oldRuleIds = oldRules.map((rule) => rule.id);
// Uncomment for seeing matches (not a lot of info).
// chrome.declarativeNetRequest.onRuleMatchedDebug.addListener((info) => {
// console.log(JSON.stringify(info))
// })
const addMin = (type) => {
return isCompressed ? '.min' : '';
}
const selectURLForRegexSub = (type) => {
return (isWDS && type === 'css') ? likelyWebServerURL : replace;
}
const makeGenAssetRegexSub = (type) => {
return `${selectURLForRegexSub(type)}/static/\\1/\\2${addMin(type)}.${type}`
}
const makeAppAssetRegexSub = (type) => {
return `${selectURLForRegexSub(type)}/static/\\1/\\2/\\3${addMin(type)}.${type}`
}
const makeAppAssetNoHashRegexSub = (type) => {
// min will show up in group 3, if needed.
return `${selectURLForRegexSub(type)}/static/\\1/\\2/\\3.${type}`
}
const cssAssetRegexSub = `${replace}/static/assets/css/\\1${addMin('css')}.css`
chrome.declarativeNetRequest.updateDynamicRules({
removeRuleIds: oldRuleIds,
addRules: [
// JS
{
id: 1,
action: {
type: "redirect",
"redirect": { regexSubstitution: makeGenAssetRegexSub('js'), }
},
"condition": {
regexFilter: genRegex,
"resourceTypes": ["script"]
}
},
// JS chunk redirected-to hashes
{
id: 2,
action: {
type: "redirect",
"redirect": { regexSubstitution: makeAppAssetNoHashRegexSub('js'), }
},
"condition": {
regexFilter: appAssetNoHashRegex,
"resourceTypes": ["script"]
}
},
// CSS
{
id: 3,
action: {
type: "redirect",
redirect: {
regexSubstitution: makeGenAssetRegexSub('css'),
},
},
condition: {
regexFilter: genRegex,
resourceTypes: ["stylesheet"],
},
},
{
id: 4,
action: {
type: "redirect",
redirect: {
regexSubstitution: makeAppAssetRegexSub('css'),
},
},
condition: {
regexFilter: appAssetRegex,
resourceTypes: ["stylesheet"],
},
},
{
id: 5,
action: {
type: "modifyHeaders",
responseHeaders: [
{ header: "Access-Control-Allow-Origin", operation: "set", value: "*" },
{ header: "Access-Control-Allow-Headers", operation: "set", value: "*" },
],
},
condition: {
urlFilter: isWDS ? likelyWebServerURL : replace,
resourceTypes: ["main_frame", "sub_frame", "stylesheet", "script", "image", "font", "object", "xmlhttprequest", "ping", "csp_report", "media", "websocket", "webtransport", "webbundle", "other"],
},
},
],
});
});