-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
144 lines (139 loc) · 6.38 KB
/
index.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
const fs = require("fs");
const fsExtra = require("fs-extra");
class AutoRunChromeExtPlugin {
constructor(options) {
const {
targetDir,
autoClickSelectLocation,
chromedriverLocation = "C:\\Program Files\\Google\\Chrome\\Application\\chromedriver.exe",
} = options;
this.targetDir = targetDir;
this.autoClickSelectLocation = autoClickSelectLocation;
this.chromedriverLocation = chromedriverLocation;
}
apply(compiler) {
// 监听webpack 打包时候的钩子,选择在编译完成的时候
compiler.hooks.done.tap("autoLoadChromeExt", async (stats) => {
// 判断targetDir文件夹是否存在
if (this.targetDir && typeof this.targetDir === "string") {
fs.access(this.targetDir, fs.constants.F_OK, async (err) => {
if (!err) {
// 判断chromedriver.exe文件是否存在
fs.access(
this.chromedriverLocation,
fs.constants.F_OK,
async (err2) => {
if (!err2) {
// 清空目标文件夹里的内容
fsExtra.emptyDirSync(this.targetDir);
// 将打包后文件夹里的内容复制到目标文件夹
const { outputPath } = stats.compilation.compiler;
fsExtra.copySync(outputPath, this.targetDir, {
dereference: true,
});
// 构建driver
const webdriver = require("selenium-webdriver");
const remote = require("selenium-webdriver/remote");
const {
ServiceBuilder,
} = require("selenium-webdriver/chrome");
const { Builder, By } = webdriver;
const driver = new Builder()
.forBrowser("chrome")
.setChromeService(
new ServiceBuilder(this.chromedriverLocation)
)
.build();
driver.setFileDetector(new remote.FileDetector());
driver.manage().setTimeouts({ implicit: 2000 });
// 打开Chrome浏览器的扩展管理页面
await driver.get("chrome://extensions/");
const window = await driver.manage().window();
await window.maximize();
const rect = await window.getRect();
// 找到页面中开发者模式的按钮
const a = await driver
.findElement(By.css("extensions-manager"))
.getShadowRoot();
const b = await a.findElement(By.css("extensions-toolbar"));
const c = await b.getShadowRoot();
const switchBtn = await c.findElement(By.id("devMode"));
if (switchBtn) {
const isSelected = await switchBtn.isSelected();
if (!isSelected) {
// 点击打开开发者模式
await switchBtn.click();
}
const devDrawer = await c.findElement(By.id("devDrawer"));
const buttonStrip = await devDrawer.findElement(
By.id("buttonStrip")
);
// 找到页面中加载已解压的扩展程序按钮
const loadUnpackedBtn = await buttonStrip.findElement(
By.id("loadUnpacked")
);
if (loadUnpackedBtn) {
// 判断autoClickSelect.exe文件是否存在
if (
this.autoClickSelectLocation &&
typeof this.autoClickSelectLocation === "string"
) {
fs.access(
this.autoClickSelectLocation,
fs.constants.F_OK,
async (err) => {
if (!err) {
// 执行autoClickSelect.exe文件,等待自动点击选择文件按钮
const exec = require("child_process").execFile;
const child = exec(
this.autoClickSelectLocation,
[
this.targetDir,
rect.x,
rect.y,
rect.width,
rect.height,
],
{},
(error, stdout, stderr) => {
if (error) return console.error(error);
}
);
} else {
console.log(
`auto-load-chrome-ext-webpack-plugin所需的autoClickSelect.exe文件不存在,配置请参考文档: https://www.jianshu.com/p/4c854f6c6f86`
);
}
// 点击加载已解压的扩展程序按钮
await loadUnpackedBtn.sendKeys(this.targetDir);
}
);
} else {
console.log(
"请正确传入auto-load-chrome-ext-webpack-plugin所需的autoClickSelectLocation参数,配置请参考文档: https://www.jianshu.com/p/4c854f6c6f86"
);
}
}
}
} else {
console.log(
"auto-load-chrome-ext-webpack-plugin必需的chromedriver.exe文件不存在,配置请参考文档: https://www.jianshu.com/p/4c854f6c6f86"
);
}
}
);
} else {
console.log(
"auto-load-chrome-ext-webpack-plugin必需的targetDir目标文件夹不存在,配置请参考文档: https://www.jianshu.com/p/4c854f6c6f86"
);
}
});
} else {
console.log(
"请正确传入auto-load-chrome-ext-webpack-plugin所必需的targetDir参数,配置请参考文档: https://www.jianshu.com/p/4c854f6c6f86"
);
}
});
}
}
module.exports = AutoRunChromeExtPlugin;