Skip to content

Commit 4b14852

Browse files
committed
Migrate to manifest v3 in Chrome
1 parent fcd1985 commit 4b14852

File tree

6 files changed

+82
-6
lines changed

6 files changed

+82
-6
lines changed

apps/browser/rules/csp.json

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
[
2+
{
3+
"id": 1,
4+
"priority": 1,
5+
"condition": {
6+
"urlFilter": "abc*d"
7+
},
8+
"action": {
9+
"type": "modifyHeaders",
10+
"responseHeaders": [
11+
{
12+
"header": "Content-Security-Policy",
13+
"operation": "remove"
14+
},
15+
{
16+
"header": "X-Webkit-CSP",
17+
"operation": "remove"
18+
},
19+
{
20+
"header": "X-Frame-Options",
21+
"operation": "remove"
22+
}
23+
]
24+
}
25+
}
26+
]

apps/browser/src/components/Frame.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function Frame({ withMotion, children, dark, dispatch, onLoaded, ...props }) {
4747
const css = document.createElement("link");
4848
css.rel = "stylesheet";
4949
css.type = "text/css";
50-
css.href = chrome.extension.getURL("css/app.css");
50+
css.href = chrome.runtime.getURL("css/app.css");
5151
css.onload = () => {
5252
setCssLoading(false);
5353
};

apps/browser/src/entry/background.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@ if (App.isDev()) require("crx-hotreload");
2222
});
2323
}
2424

25-
if (chrome.webRequest) {
25+
if (chrome.action) {
26+
chrome.action.onClicked.addListener(() => {
27+
Tab.send("action:click");
28+
});
29+
}
30+
31+
if (chrome.webRequest && !(!App.isDev() && App.isChrome())) {
2632
chrome.webRequest.onHeadersReceived.addListener(
2733
Events.onHttpRequest,
2834
{

apps/browser/src/util/policy.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ export default class Policy {
8181
return (
8282
name == "CONTENT-SECURITY-POLICY" ||
8383
name == "X-WEBKIT-CSP" ||
84-
name == "X-FRAME-OPTIONS" ||
85-
name == "CONTENT_SECURITY_REPORT_ONLY"
84+
name == "X-FRAME-OPTIONS"
8685
);
8786
}
8887

apps/browser/webpack.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,9 +80,9 @@ module.exports = function (_, env) {
8080
to: "manifest.json",
8181
},
8282
{ from: path.join(__dirname, "assets"), to: "assets" },
83+
{ from: path.join(__dirname, "rules"), to: "rules" },
8384
],
8485
}),
85-
// env.mode == "production" && new BundleAnalyzerPlugin(),
8686
].filter((p) => p !== false),
8787
};
8888
};

gulpfile.js

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function distChrome() {
4646
return dist("chrome", (manifest) => {
4747
manifest.version = package.version;
4848
manifest.externally_connectable.matches.pop();
49-
return manifest;
49+
return toV3(manifest);
5050
});
5151
}
5252

@@ -58,6 +58,51 @@ function distEdge() {
5858
});
5959
}
6060

61+
function toV3(manifest) {
62+
manifest.manifest_version = 3;
63+
manifest.background.service_worker = manifest.background.scripts[0];
64+
manifest.action = manifest.browser_action;
65+
manifest.host_permissions = ["*://*/*"];
66+
67+
manifest.web_accessible_resources = [
68+
{
69+
resources: manifest.web_accessible_resources,
70+
matches: ["http://*/*", "https://*/*"],
71+
},
72+
];
73+
74+
manifest.declarative_net_request = {
75+
rule_resources: [
76+
{
77+
id: "CSP Change",
78+
path: "rules/csp.json",
79+
enabled: true,
80+
},
81+
],
82+
};
83+
84+
manifest.permissions = manifest.permissions.filter(
85+
(p) =>
86+
![
87+
"http://*/*",
88+
"https://*/*",
89+
"webRequest",
90+
"webRequestBlocking",
91+
].includes(p)
92+
);
93+
94+
manifest.permissions.push(
95+
"declarativeNetRequest",
96+
"declarativeNetRequestFeedback"
97+
);
98+
99+
delete manifest.background.scripts;
100+
delete manifest.background.persistent;
101+
delete manifest.browser_action;
102+
103+
return manifest;
104+
}
105+
61106
exports["dist:firefox"] = distFirefox;
62107
exports["dist:chrome"] = distChrome;
63108
exports["dist:opera"] = distOpera;

0 commit comments

Comments
 (0)