Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
shaonianzhentan committed Oct 24, 2023
1 parent b5d4383 commit 976367d
Show file tree
Hide file tree
Showing 8 changed files with 1,748 additions and 0 deletions.
22 changes: 22 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,24 @@
# node-red-contrib-puppeteer-js
运行JavaScript代码并返回结果


input
```js
{
url: 'https://www.npmjs.com',
payload: `
const sleep = (s) => new Promise((resolve) => setTimeout(resolve, s * 1000));
await sleep(2)
return 123
`
}

```

```js
'data:image/png;base64,' + msg.screenshot
```

## 相关文档

- https://pptr.dev
58 changes: 58 additions & 0 deletions browser.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const puppeteer = require('puppeteer-core');

module.exports = class {

constructor(config) {
this.timer = null
this.config = {
headless: 'new',
defaultViewport: { width: 1920, height: 1080 },
args: ['--start-maximized', '--no-sandbox'],
...config
}
}

async launch() {
this.browser = await puppeteer.launch(this.config);
}

async evaluate(url, js, isScreenshot = false) {
if (!this.browser) {
await this.launch()
}
if (this.timer) clearTimeout(this.timer)

this.timer = setTimeout(async () => {
await this.browser.close()
this.browser = null
}, 60000)

const page = await this.browser.newPage();
// 返回结果
const response = {
url,
payload: null,
error: null,
screenshot: null
}
try {
await page.goto(url);
response.payload = await page.evaluate(async (js) => {
eval(`window.PUPPETEER_ASYNC_FUNCTION = async () => { ${js} }`)
return await window.PUPPETEER_ASYNC_FUNCTION()
}, js);
// 截图
if (isScreenshot) {
response.screenshot = await page.screenshot({
fullPage: true,
encoding: 'base64'
});
}
} catch (ex) {
response.error = ex
} finally {
await page.close()
}
return response
}
}
48 changes: 48 additions & 0 deletions eval-js/eval-js.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<script type="text/html" data-template-name="eval-js">
<div class="form-row">
<label for="node-input-name">名称</label>
<input type="text" id="node-input-name" placeholder="请输入名称">
</div>
<div class="form-row">
<label for="node-input-puppeteer">Puppeteer</label>
<select type="text" id="node-input-puppeteer" placeholder="请选择Puppeteer"></select>
</div>
<div class="form-row">
<label for="node-input-url">URL</label>
<input type="url" id="node-input-url" placeholder="请输入要访问的URL地址">
</div>
<div class="form-row">
<label for="node-input-js">JavaScript</label>
<textarea id="node-input-js" style="width:100%;" rows="15" placeholder="请输入要执行的JavaScript代码"></textarea>
</div>
<div class="form-row">
<label for="node-input-screenshot">
screenshot
</label>
<label style="width: 70%;">
<input type="checkbox" id="node-input-screenshot" style="display:inline-block; width:22px; vertical-align:top;" autocomplete="off"/>
<span style="user-select: none;">截图输出base64字符串</span>
</label>
</div>
</script>
<script type="text/javascript">
RED.nodes.registerType('eval-js', {
category: "puppeteer",
color: '#dfdff0',
icon: "font-awesome/fa-code",
paletteLabel: 'Eval JS',
defaults: {
name: { value: "" },
puppeteer: { value: "", type: "puppeteer-js", required: true },
url: { value: "", required: true },
js: { value: "", required: true },
screenshot: { value: false }
},
inputs: 1,
outputs: 1,
inputLabels: "执行js代码",
label: function () {
return this.name || "Eval JS";
}
});
</script>
31 changes: 31 additions & 0 deletions eval-js/eval-js.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module.exports = function (RED) {

RED.nodes.registerType("eval-js", function (config) {
RED.nodes.createNode(this, config);

const puppeteer = RED.nodes.getNode(config.puppeteer);

const node = this;
node.on('input', async function (msg) {
let url = config.url
let js = config.js
// 使用配置项
if (!url) url = msg.url
if (!js) js = msg.payload

if (url && js) {
this.status({ fill: "blue", shape: "ring", text: "正在执行代码" });
const response = await puppeteer.browser.evaluate(url, js, config.screenshot)
// 出现异常
if (response.error) {
this.status({ fill: "red", shape: "ring", text: response.error });
} else {
this.status({ fill: "green", shape: "ring", text: "执行成功返回结果" });
}
node.send(response);
} else {
this.status({ fill: "red", shape: "ring", text: "缺少必要参数" });
}
});
});
}

0 comments on commit 976367d

Please sign in to comment.