Skip to content

Commit 490a969

Browse files
authored
feat(plugins/community/screenshot): add plugin_screenshot_mode (#1324)
1 parent 3b30d78 commit 490a969

File tree

4 files changed

+48
-12
lines changed

4 files changed

+48
-12
lines changed

source/plugins/community/screenshot/index.mjs

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export default async function({login, q, imports, data, account}, {enabled = fal
77
return null
88

99
//Load inputs
10-
let {url, selector, title, background, viewport, wait} = imports.metadata.plugins.screenshot.inputs({data, account, q})
10+
let {url, selector, title, background, viewport, wait, mode} = imports.metadata.plugins.screenshot.inputs({data, account, q})
1111
if (!url)
1212
throw {error: {message: "URL is not set"}}
1313

@@ -23,19 +23,35 @@ export default async function({login, q, imports, data, account}, {enabled = fal
2323
await new Promise(solve => setTimeout(solve, wait))
2424

2525
//Screenshot
26+
let content = null
27+
let image = null
28+
const metadata = {height:null, width:null}
2629
await page.waitForSelector(selector)
27-
const clip = await page.evaluate(selector => {
28-
const {x, y, width, height} = document.querySelector(selector).getBoundingClientRect()
29-
return {x, y, width, height}
30-
}, selector)
31-
console.debug(`metrics/compute/${login}/plugins > screenshot > coordinates ${JSON.stringify(clip)}`)
32-
const [buffer] = await imports.record({page, ...clip, frames: 1, background})
33-
const screenshot = await imports.sharp(Buffer.from(buffer.split(",").pop(), "base64")).resize({width: Math.min(454 * (1 + data.large), clip.width)})
34-
const metadata = await screenshot.metadata()
30+
switch (mode) {
31+
case "image":{
32+
const clip = await page.evaluate(selector => {
33+
const {x, y, width, height} = document.querySelector(selector).getBoundingClientRect()
34+
return {x, y, width, height}
35+
}, selector)
36+
console.debug(`metrics/compute/${login}/plugins > screenshot > coordinates ${JSON.stringify(clip)}`)
37+
const [buffer] = await imports.record({page, ...clip, frames: 1, background})
38+
const screenshot = await imports.sharp(Buffer.from(buffer.split(",").pop(), "base64")).resize({width: Math.min(454 * (1 + data.large), clip.width)})
39+
image = `data:image/png;base64,${(await screenshot.toBuffer()).toString("base64")}`
40+
Object.assign(metadata, await screenshot.metadata())
41+
break
42+
}
43+
case "text":{
44+
content = await page.evaluate(selector => document.querySelector(selector)?.innerText ?? "", selector)
45+
break
46+
}
47+
default:
48+
throw {error: {message: `Unsupported mode "${mode}"`}}
49+
}
50+
3551
await browser.close()
3652

3753
//Results
38-
return {image: `data:image/png;base64,${(await screenshot.toBuffer()).toString("base64")}`, title, height: metadata.height, width: metadata.width, url}
54+
return {mode, image, content, title, height: metadata.height, width: metadata.width, url}
3955
}
4056
//Handle errors
4157
catch (error) {

source/plugins/community/screenshot/metadata.yml

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ inputs:
4242
type: string
4343
default: body
4444

45+
plugin_screenshot_mode:
46+
description: |
47+
Output mode
48+
49+
- `image`: screenshot of selected element
50+
- `text`: keep [`innerText`](https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/innerText) of selected element
51+
- *⚠️ Any CSS style applied to text such as font size, weight or color will be removed*
52+
type: string
53+
default: image
54+
values:
55+
- image
56+
- text
57+
4558
plugin_screenshot_viewport:
4659
description: |
4760
Viewport options
@@ -63,4 +76,4 @@ inputs:
6376
description: |
6477
Background
6578
type: boolean
66-
default: yes
79+
default: yes

source/templates/classic/partials/screenshot.ejs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@
1111
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 16 16" width="16" height="16"><path fill-rule="evenodd" d="M2.343 13.657A8 8 0 1113.657 2.343 8 8 0 012.343 13.657zM6.03 4.97a.75.75 0 00-1.06 1.06L6.94 8 4.97 9.97a.75.75 0 101.06 1.06L8 9.06l1.97 1.97a.75.75 0 101.06-1.06L9.06 8l1.97-1.97a.75.75 0 10-1.06-1.06L8 6.94 6.03 4.97z"></path></svg>
1212
<%= plugins.screenshot.error.message %>
1313
</div>
14-
<% } else { %>
14+
<% } else if (plugins.screenshot.image) { %>
1515
<img class="screenshot autosize" src="<%= plugins.screenshot.image %>" height="<%= plugins.screenshot.height %>" width="<%= plugins.screenshot.width %>" alt=""/>
16+
<% } else if (plugins.screenshot.content) { %>
17+
<div class="screenshot-content">
18+
<%= plugins.screenshot.content %>
19+
</div>
1620
<% } %>
1721
</section>
1822
</div>

source/templates/classic/style.css

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,9 @@
469469
margin: 8px 14px 4px;
470470
border-radius: 5px;
471471
}
472+
.screenshot-content {
473+
margin-left: 12px;
474+
}
472475

473476
svg.large .audits {
474477
display: inline-flex;

0 commit comments

Comments
 (0)