Skip to content

Commit 4cf6450

Browse files
committed
🐛 优化脚本运行框架注入速度 #232
1 parent 9d4ebcc commit 4cf6450

File tree

7 files changed

+68
-33
lines changed

7 files changed

+68
-33
lines changed

package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
"scripts": {
88
"test": "jest --coverage",
99
"lint": "eslint --ext .js,.ts,.tsx src/",
10-
"dev": "webpack --mode development --config ./webpack/webpack.dev.ts",
11-
"build": "webpack --mode production --config ./webpack/webpack.prod.ts && concurrently \"npm run build:linter\" \"npm run build:no-split\"",
10+
"dev": "concurrently \"webpack --mode development --config ./webpack/webpack.dev.ts\" \"webpack --mode development --config ./webpack/webpack.inject.ts\"",
11+
"build": "webpack --mode production --config ./webpack/webpack.prod.ts && concurrently \"npm run build:linter\" \"npm run build:inject && npm run build:no-split\"",
1212
"build:linter": "webpack --mode production --config ./webpack/webpack.linter.ts",
13+
"build:inject": "webpack --mode production --config ./webpack/webpack.inject.ts",
1314
"build:no-split": "webpack --mode production --config ./webpack/webpack.no.split.ts",
1415
"dev:linter": "webpack --mode development --config ./webpack/webpack.linter.dev.ts",
1516
"pack": "node ./build/pack.js",

src/content.ts

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@ import MessageWriter from "./app/logger/message_writer";
33
import MessageContent from "./app/message/content";
44
import MessageInternal from "./app/message/internal";
55
import ContentRuntime from "./runtime/content/content";
6+
// @ts-ignore
7+
import injectJs from "../dist/inject.js";
8+
import { randomString } from "./pkg/utils/utils";
69

710
const internalMessage = new MessageInternal("content");
811

@@ -12,9 +15,19 @@ const logger = new LoggerCore({
1215
labels: { env: "content", href: window.location.href },
1316
});
1417

18+
const scriptFlag = randomString(8);
19+
20+
// 注入运行框架
21+
const temp = document.createElementNS("http://www.w3.org/1999/xhtml", "script");
22+
temp.setAttribute("type", "text/javascript");
23+
temp.innerHTML = `(function (ScriptFlag) {\n${injectJs}\n})('${scriptFlag}')`;
24+
temp.className = "injected-js";
25+
document.documentElement.appendChild(temp);
26+
temp.remove();
27+
1528
internalMessage.syncSend("pageLoad", null).then((resp) => {
1629
logger.logger().debug("content start");
1730
// 通过flag与inject建立通讯
18-
const contentMessage = new MessageContent(resp.flag, true);
31+
const contentMessage = new MessageContent(scriptFlag, true);
1932
new ContentRuntime(contentMessage, internalMessage).start(resp);
2033
});

src/runtime/background/runtime.ts

Lines changed: 2 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ export default class Runtime extends Manager {
6767

6868
logger: Logger;
6969

70-
scriptFlag: string;
71-
7270
match: UrlMatch<ScriptRunResouce> = new UrlMatch();
7371

7472
include: UrlInclude<ScriptRunResouce> = new UrlInclude();
@@ -90,7 +88,6 @@ export default class Runtime extends Manager {
9088
this.scriptDAO = new ScriptDAO();
9189
this.resourceManager = resourceManager;
9290
this.valueManager = valueManager;
93-
this.scriptFlag = randomString(8);
9491
this.logger = LoggerCore.getInstance().logger({ component: "runtime" });
9592
ScriptManager.hook.addListener("upsert", this.scriptUpdate.bind(this));
9693
ScriptManager.hook.addListener("delete", this.scriptDelete.bind(this));
@@ -158,15 +155,6 @@ export default class Runtime extends Manager {
158155

159156
// 接受消息,注入脚本
160157
// 获取注入源码
161-
const { scriptFlag } = this;
162-
let injectedSource = "";
163-
fetch(chrome.runtime.getURL("src/inject.js"))
164-
.then((resp) => resp.text())
165-
.then((source: string) => {
166-
injectedSource = dealScript(
167-
`(function (ScriptFlag) {\n${source}\n})('${scriptFlag}')`
168-
);
169-
});
170158

171159
// 监听菜单创建
172160
const scriptMenu: Map<
@@ -433,26 +421,12 @@ export default class Runtime extends Manager {
433421
}
434422
);
435423

436-
// 注入运行框架
437-
chrome.tabs.executeScript(sender.tabId, {
438-
frameId: sender.frameId,
439-
code: `(function(){
440-
let temp = document.createElementNS("http://www.w3.org/1999/xhtml", "script");
441-
temp.setAttribute('type', 'text/javascript');
442-
temp.innerHTML = "${injectedSource}";
443-
temp.className = "injected-js";
444-
document.documentElement.appendChild(temp);
445-
temp.remove();
446-
}())`,
447-
runAt: "document_start",
448-
});
449-
450424
if (!filter.length) {
451-
resolve({ flag: scriptFlag, scripts: [] });
425+
resolve({ scripts: [] });
452426
return;
453427
}
454428

455-
resolve({ flag: scriptFlag, scripts: filter });
429+
resolve({ scripts: filter });
456430

457431
// 注入脚本
458432
filter.forEach((script) => {

webpack.config.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,11 @@ const config: Configuration = {
147147
use: ["raw-loader"],
148148
exclude: /node_modules/,
149149
},
150+
{
151+
test: /inject\.js$/,
152+
use: ["raw-loader"],
153+
include: /dist/,
154+
},
150155
{
151156
test: /\.tpl$/,
152157
use: ["raw-loader"],

webpack/webpack.dev.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ common.entry = {
1515
// @ts-ignore
1616
...common.entry,
1717
content: `${src}/content.ts`,
18-
inject: `${src}/inject.ts`,
1918
"editor.worker": "monaco-editor/esm/vs/editor/editor.worker.js",
2019
"ts.worker": "monaco-editor/esm/vs/language/typescript/ts.worker.js",
2120
"linter.worker": `${src}/linter.worker.ts`,

webpack/webpack.inject.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* eslint-disable import/no-extraneous-dependencies */
2+
import merge from "webpack-merge";
3+
import HtmlWebpackPlugin from "html-webpack-plugin";
4+
import CopyPlugin from "copy-webpack-plugin";
5+
import CompressionPlugin from "compression-webpack-plugin";
6+
import { CleanWebpackPlugin } from "clean-webpack-plugin";
7+
import common from "../webpack.config";
8+
9+
const src = `${__dirname}/../src`;
10+
const dist = `${__dirname}/../dist`;
11+
12+
// 不要分割的文件
13+
common.entry = {
14+
inject: `${src}/inject.ts`,
15+
};
16+
17+
common.output = {
18+
path: `${dist}`,
19+
filename: "[name].js",
20+
clean: false,
21+
};
22+
23+
// 取消splitChunks
24+
common.optimization = {};
25+
26+
// 移除插件
27+
common.plugins = common.plugins!.filter(
28+
(plugin) =>
29+
!(
30+
plugin instanceof HtmlWebpackPlugin ||
31+
plugin instanceof CopyPlugin ||
32+
plugin instanceof CleanWebpackPlugin
33+
)
34+
);
35+
36+
export default merge(common, {
37+
plugins: [
38+
// firefox商店文件不能大于4M, 所以需要压缩
39+
new CompressionPlugin({
40+
test: /ts.worker.js/,
41+
deleteOriginalAssets: true,
42+
}),
43+
],
44+
});

webpack/webpack.no.split.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ const dist = `${__dirname}/../dist`;
1212
// 不要分割的文件
1313
common.entry = {
1414
content: `${src}/content.ts`,
15-
inject: `${src}/inject.ts`,
1615
"editor.worker": "monaco-editor/esm/vs/editor/editor.worker.js",
1716
"ts.worker": "monaco-editor/esm/vs/language/typescript/ts.worker.js",
1817
};

0 commit comments

Comments
 (0)