Skip to content

Commit ab20c09

Browse files
committed
fix: firefox csp
1 parent ad9a388 commit ab20c09

File tree

11 files changed

+99
-106
lines changed

11 files changed

+99
-106
lines changed

.eslintrc.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,13 @@ module.exports = {
2727
],
2828
ignorePatterns: ["node_modules", "build", "dist", "coverage", "public"],
2929
rules: {
30-
// 分号
3130
"semi": "error",
32-
// 对象键值引号样式保持一致
3331
"quote-props": ["error", "consistent-as-needed"],
34-
// 箭头函数允许单参数不带括号
3532
"arrow-parens": ["error", "as-needed"],
36-
// no var
3733
"no-var": "error",
38-
// const
3934
"prefer-const": "error",
40-
// 允许console
4135
"no-console": "off",
42-
// 关闭每个函数都要显式声明返回值
4336
"@typescript-eslint/explicit-module-boundary-types": "off",
44-
// 关闭@ts-ignore检查
4537
"@typescript-eslint/ban-ts-ignore": "off",
4638
"@typescript-eslint/ban-ts-comment": "off",
4739
"@typescript-eslint/consistent-type-imports": "error",

.prettierrc.js

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
module.exports = {
2-
"printWidth": 100, // 指定代码长度,超出换行
3-
"tabWidth": 2, // tab 键的宽度
4-
"useTabs": false, // 不使用tab
5-
"semi": true, // 结尾加上分号
6-
"singleQuote": false, // 使用单引号
7-
"quoteProps": "preserve", // 不要求对象字面量属性是否使用引号包裹
8-
"jsxSingleQuote": false, // jsx 语法中使用单引号
9-
"trailingComma": "es5", // 确保对象的最后一个属性后有逗号
10-
"bracketSpacing": true, // 大括号有空格 { name: 'rose' }
11-
"jsxBracketSameLine": false, // 在多行JSX元素的最后一行追加 >
12-
"arrowParens": "avoid", // 箭头函数,单个参数不强制添加括号
13-
"requirePragma": false, // 是否严格按照文件顶部的特殊注释格式化代码
14-
"insertPragma": false, // 是否在格式化的文件顶部插入Pragma标记,以表明该文件被prettier格式化过了
15-
"proseWrap": "preserve", // 按照文件原样折行
16-
"htmlWhitespaceSensitivity": "ignore", // html文件的空格敏感度,控制空格是否影响布局
17-
"endOfLine": "lf" // 结尾是 \n \r \n\r auto
18-
}
2+
"printWidth": 100,
3+
"tabWidth": 2,
4+
"useTabs": false,
5+
"semi": true,
6+
"singleQuote": false,
7+
"quoteProps": "preserve",
8+
"jsxSingleQuote": false,
9+
"trailingComma": "es5",
10+
"bracketSpacing": true,
11+
"jsxBracketSameLine": false,
12+
"arrowParens": "avoid",
13+
"requirePragma": false,
14+
"insertPragma": false,
15+
"proseWrap": "preserve",
16+
"htmlWhitespaceSensitivity": "ignore",
17+
"endOfLine": "lf",
18+
};

packages/force-copy/rspack.config.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,18 @@ const { ReloadPlugin } = require("./script/reload");
44
const { ManifestPlugin } = require("./script/manifest");
55
const { WrapperCodePlugin } = require("./script/wrapper");
66
const { default: HtmlPlugin } = require("@rspack/plugin-html");
7-
const { getUniqueId, isDev, isGecko } = require("./script/utils/node");
7+
const { getUniqueId, IS_DEV, IS_GECKO } = require("./script/utils/node");
88

9-
const folder = isGecko ? "build-gecko" : "build";
10-
const EVENT_TYPE = isDev ? "EVENT_TYPE" : getUniqueId();
11-
const INJECT_FILE = isDev ? "INJECT_FILE" : getUniqueId();
9+
const folder = IS_GECKO ? "build-gecko" : "build";
10+
const EVENT_TYPE = IS_DEV ? "EVENT_TYPE" : getUniqueId();
11+
const INJECT_FILE = IS_DEV ? "INJECT_FILE" : getUniqueId();
1212

1313
process.env.EVENT_TYPE = EVENT_TYPE;
1414
process.env.INJECT_FILE = INJECT_FILE;
1515
process.env.PLATFORM = process.env.PLATFORM || "chromium";
1616

1717
/**
18-
* @type {import('@rspack/cli').Configuration}
18+
* @type {import("@rspack/cli").Configuration}
1919
*/
2020
module.exports = {
2121
context: __dirname,
@@ -43,7 +43,7 @@ module.exports = {
4343
},
4444
builtins: {
4545
define: {
46-
"__DEV__": JSON.stringify(isDev),
46+
"__DEV__": JSON.stringify(IS_DEV),
4747
"process.env.NODE_ENV": JSON.stringify(process.env.NODE_ENV),
4848
"process.env.PLATFORM": JSON.stringify(process.env.PLATFORM),
4949
"process.env.EVENT_TYPE": JSON.stringify(process.env.EVENT_TYPE),

packages/force-copy/script/files/index.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const thread = require("child_process");
22
const path = require("path");
33
const fs = require("fs/promises");
4-
const { isGecko } = require("../utils/node");
4+
const { IS_GECKO } = require("../utils/node");
55

66
const exec = command => {
77
return new Promise((resolve, reject) => {
@@ -14,7 +14,7 @@ const exec = command => {
1414

1515
exports.FilesPlugin = class FilesPlugin {
1616
constructor() {
17-
const folder = isGecko ? "build-gecko" : "build";
17+
const folder = IS_GECKO ? "build-gecko" : "build";
1818
fs.mkdir(`${folder}/static`, { recursive: true });
1919
fs.mkdir(`${folder}/_locales`, { recursive: true });
2020
}
@@ -30,7 +30,7 @@ exports.FilesPlugin = class FilesPlugin {
3030
const locales = path.resolve("public/locales");
3131
const resources = path.resolve("public/static");
3232

33-
const folder = isGecko ? "build-gecko" : "build";
33+
const folder = IS_GECKO ? "build-gecko" : "build";
3434
const localesTarget = path.resolve(`${folder}/_locales`);
3535
const resourcesTarget = path.resolve(`${folder}/static`);
3636

packages/force-copy/script/manifest/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const path = require("path");
22
const tsNode = require("ts-node");
33
const fs = require("fs");
4-
const { promisify, isGecko } = require("../utils/node");
4+
const { promisify, IS_GECKO } = require("../utils/node");
55

66
const writeFile = promisify(fs.writeFile);
77

@@ -22,7 +22,7 @@ exports.ManifestPlugin = class ManifestPlugin {
2222
const manifest = require(this.manifest);
2323
const version = require(path.resolve("package.json")).version;
2424
manifest.version = version;
25-
const folder = isGecko ? "build-gecko" : "build";
25+
const folder = IS_GECKO ? "build-gecko" : "build";
2626
return writeFile(path.resolve(`${folder}/manifest.json`), JSON.stringify(manifest, null, 2));
2727
});
2828
}

packages/force-copy/script/reload/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const { isDev } = require("../utils/node");
1+
const { IS_DEV } = require("../utils/node");
22
const WebSocketServer = require("ws").Server;
33

44
/**
@@ -8,7 +8,7 @@ let wsClient = null;
88

99
exports.ReloadPlugin = class ReloadPlugin {
1010
constructor() {
11-
if (isDev) {
11+
if (IS_DEV) {
1212
try {
1313
const server = new WebSocketServer({ port: 3333 });
1414
server.on("connection", client => {

packages/force-copy/script/utils/node.js

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
const isDev = process.env.NODE_ENV === "development";
2-
const isProd = process.env.NODE_ENV === "production";
3-
const isGecko = process.env.PLATFORM === "gecko";
4-
const isChromium = process.env.PLATFORM ? process.env.PLATFORM === "chromium" : true;
5-
1+
const IS_DEV = process.env.NODE_ENV === "development";
2+
const IS_PROD = process.env.NODE_ENV === "production";
3+
const IS_GECKO = process.env.PLATFORM === "gecko";
4+
const IS_CHROMIUM = process.env.PLATFORM ? process.env.PLATFORM === "chromium" : true;
65
const CHARTS = "QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnm0123456789";
6+
7+
exports.IS_DEV = IS_DEV;
8+
exports.IS_PROD = IS_PROD;
9+
exports.IS_GECKO = IS_GECKO;
10+
exports.IS_CHROMIUM = IS_CHROMIUM;
11+
712
const getUniqueId = (len = 10) => {
813
const chars = new Array(len - 1).fill("");
914
return (
@@ -22,9 +27,5 @@ const promisify = fn => {
2227
});
2328
};
2429

25-
exports.isDev = isDev;
26-
exports.isProd = isProd;
27-
exports.isGecko = isGecko;
2830
exports.promisify = promisify;
29-
exports.isChromium = isChromium;
3031
exports.getUniqueId = getUniqueId;

packages/force-copy/script/wrapper/index.js

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,48 @@
1-
const { isChromium } = require("../utils/node");
1+
const { IS_CHROMIUM } = require("../utils/node");
2+
const crypto = require("crypto");
23

34
exports.WrapperCodePlugin = class WrapperCodePlugin {
45
constructor(options) {
56
this.options = options || {};
67
}
78
apply(compiler) {
8-
if (isChromium) return void 0;
9+
if (IS_CHROMIUM) return void 0;
910
compiler.hooks.emit.tapAsync("WrapperCodePlugin", (compilation, done) => {
10-
Object.keys(compilation.assets).forEach(key => {
11-
if (!isChromium && key === process.env.INJECT_FILE + ".js") {
12-
try {
13-
const buffer = compilation.assets[key].source();
14-
let code = buffer.toString("utf-8");
15-
code = `window.${process.env.INJECT_FILE}=function(){${code}}`;
16-
compilation.assets[key] = {
17-
source() {
18-
return code;
19-
},
20-
size() {
21-
return this.source().length;
22-
},
23-
};
24-
} catch (error) {
25-
console.log("Parse Inject File Error", error);
26-
}
11+
const injectKey = process.env.INJECT_FILE + ".js";
12+
const injectFile = compilation.assets[injectKey];
13+
const workerKey = "worker.js";
14+
const workerFile = compilation.assets[workerKey];
15+
if (injectFile) {
16+
// 处理 Inject Script
17+
const buffer = injectFile.source();
18+
const code = buffer.toString("utf-8");
19+
const source = `window.${process.env.INJECT_FILE}=function(){${code}}`;
20+
compilation.assets[injectKey] = {
21+
source() {
22+
return source;
23+
},
24+
size() {
25+
return this.source().length;
26+
},
27+
};
28+
// 处理 Worker Script
29+
if (workerFile) {
30+
const workerBuffer = workerFile.source();
31+
const workerCode = workerBuffer.toString("utf-8");
32+
const hash = crypto.createHash("sha256");
33+
hash.update(`;(function(){${code}})();`);
34+
const hashed = hash.digest("base64");
35+
const nonCSP = workerCode.replace("${CSP-HASH}", hashed);
36+
compilation.assets[workerKey] = {
37+
source() {
38+
return nonCSP;
39+
},
40+
size() {
41+
return this.source().length;
42+
},
43+
};
2744
}
28-
});
45+
}
2946
done();
3047
});
3148
}

packages/force-copy/src/content/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { implantScript } from "./runtime/script";
1212
logger.setLevel(LOG_LEVEL.INFO);
1313
}
1414
logger.info("Content Script Loaded");
15-
// implantScript();
15+
implantScript();
1616
!isInIframe && initializeWorker();
1717
PCBridge.onPopupMessage(onPopupMessage);
1818
})();

packages/force-copy/src/content/runtime/script.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ export const implantScript = () => {
66
if (document instanceof XMLDocument) return void 0;
77
const script = document.createElementNS("http://www.w3.org/1999/xhtml", "script");
88
script.setAttribute("type", "text/javascript");
9+
// 这里的内容需要跟 WrapperCodePlugin 的 HASH 计算保持一致
910
script.innerText = `;(${fn.toString()})();`;
1011
document.documentElement.appendChild(script);
1112
script.onload = () => script.remove();

0 commit comments

Comments
 (0)