Skip to content

Commit abdbe40

Browse files
committed
v0.2
插件主函数新增参数:插件文件夹路径
1 parent 46f7d52 commit abdbe40

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

README.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,15 @@ npm install
6464
`index.js`必须提供如下格式的入口函数:
6565

6666
```js
67-
export default async function main(page, context, setting) {
68-
// page是创建好的新页面,context是浏览器上下文对象
69-
// 如果有设置文件,setting是一个保存了插件设置信息的对象
70-
// 如果没有设置文件,setting为null
67+
/**
68+
* 主函数,用于执行Playwright自动化脚本
69+
* @param {import('playwright').Page} page - 创建好的新页面
70+
* @param {import('playwright').BrowserContext} context - 浏览器上下文对象
71+
* @param {Object|null} setting - 保存了插件设置信息的对象,如果没有设置文件则为null
72+
* @param {string} pluginsFolderPath - 插件目录的绝对路径,例如:R:\Playwright自动化脚本加载器\plugins\百度登录插件
73+
* 注意:插件内代码必须用pluginsFolderPath拼接绝对路径,插件内使用相对路径会错误!
74+
*/
75+
export default async function main(page, context, setting, pluginsFolderPath) {
7176
// 下面可自由编写业务逻辑
7277
await page.goto('https://xxxx'); // 跳转网址示例
7378
}

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { startBrowser } from './src/starter.js';
22
import { scanPlugins } from './src/pluginsScanner.js';
33
import fs from 'fs/promises';
4+
import path from 'path';
45

56
// 发送消息给页面
67
async function sendMessage2Page(page, type, data) {
@@ -102,7 +103,7 @@ async function loadPlugin(pluginName, pluginFolderName, pluginSettingHtmlFileURL
102103
const plugin = await import(`./plugins/${pluginFolderName}/index.js`);
103104

104105
// 调用插件中的 main 函数
105-
await plugin.default(page, context, settingData);
106+
await plugin.default(page, context, settingData, path.resolve(`./plugins/${pluginFolderName}`));
106107

107108
// 插件结束运行
108109
console.log(`插件结束运行 ${pluginName}`);

plugins/百度登录插件/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
export default async function main(page, context, setting) {
1+
export default async function main(page, context, setting, pluginsFolderPath) {
22
await page.goto('https://www.baidu.com/');
33
await page.getByRole('link', { name: '登录' }).click();
44
await page.getByPlaceholder('手机号/用户名/邮箱').click();

0 commit comments

Comments
 (0)