|
import plugin from '../../../lib/plugins/plugin.js' |
|
import { segment } from 'oicq' |
|
import lodash from "lodash"; |
|
|
|
import { createRequire } from 'module' |
|
const require = createRequire(import.meta.url) |
|
|
|
export class webPreview extends plugin { |
|
constructor() { |
|
super({ |
|
|
|
name: '截图预览网页内容', |
|
|
|
dsc: '群里发送网页地址,截图预览网页内容', |
|
|
|
event: 'message', |
|
|
|
priority: 1006, |
|
rule: [ |
|
{ |
|
|
|
reg: '^(?:(http|https):\/\/)?((?:[\\w-]+\\.)+[a-z0-9]+)(?::\\d+)?((?:\/[^\/?#]*)+)?(\\?[^#]+)?(#.+)?$' |
|
, |
|
|
|
fnc: 'webPreview' |
|
}, |
|
{ |
|
|
|
reg: '^#*必应(.*)$', |
|
|
|
fnc: 'bingWeb' |
|
}, |
|
] |
|
|
|
}) |
|
} |
|
|
|
|
|
|
|
|
|
|
|
async webPreview(e) { |
|
const puppeteer = require('puppeteer'); |
|
const browser = await puppeteer.launch({ |
|
headless: true, |
|
args: [ |
|
'--disable-gpu', |
|
'--disable-dev-shm-usage', |
|
'--disable-setuid-sandbox', |
|
'--no-first-run', |
|
'--no-sandbox', |
|
'--no-zygote', |
|
'--single-process' |
|
] |
|
}); |
|
const page = await browser.newPage(); |
|
try { |
|
await page.goto(e.msg, { timeout: 30000, waitUntil: 'networkidle0' }); |
|
} catch (err) { |
|
console.error(`Failed to navigate to ${e.msg}: ${err.message}`); |
|
await browser.close(); |
|
return; |
|
} |
|
await page.setViewport({ width: 1920, height: 1080 }); |
|
let screenshot; |
|
try { |
|
screenshot = await page.screenshot({ fullPage: true }); |
|
} catch (err) { |
|
console.error(`Failed to take screenshot: ${err.message}`); |
|
await browser.close(); |
|
return; |
|
} |
|
await this.reply(segment.image(screenshot)); |
|
await browser.close(); |
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
async bingWeb(e) { |
|
|
|
let webkeywd = e.msg.replace(/#|必应/gm, ''); |
|
|
|
webkeywd = webkeywd.replace(/,| |,/g, ","); |
|
|
|
let wdKey = webkeywd.split(","); |
|
|
|
wdKey = lodash.compact(wdKey); |
|
|
|
let keyWd = ''; |
|
let searchKey = ''; |
|
|
|
if(wdKey.length > 1){ |
|
searchKey = wdKey[0] |
|
keyWd = wdKey[1]; |
|
} |
|
|
|
console.log("keyWd",keyWd); |
|
|
|
let weburl = `https://www.bing.com/search?q=${searchKey?searchKey:webkeywd}`; |
|
|
|
const puppeteer = require('puppeteer'); |
|
|
|
const browser = await puppeteer.launch({ |
|
headless: true, |
|
args: [ |
|
'--disable-gpu', |
|
'--disable-dev-shm-usage', |
|
'--disable-setuid-sandbox', |
|
'--no-first-run', |
|
'--no-sandbox', |
|
'--no-zygote', |
|
'--single-process' |
|
] |
|
}); |
|
const page = await browser.newPage(); |
|
await page.goto(weburl); |
|
await page.setViewport({ |
|
width: 1920, |
|
height: 1080 |
|
}); |
|
|
|
if(keyWd != '列表'){ |
|
|
|
let link = await page.evaluate(async keyWd => { |
|
|
|
return [...document.querySelectorAll('.result a')].filter(item => { |
|
return item.innerText && item.innerText.includes(keyWd) |
|
})[0].toString(); |
|
},keyWd); |
|
|
|
if(link){ |
|
|
|
link = link.toString(); |
|
}else{ |
|
|
|
link = weburl; |
|
} |
|
console.log(link); |
|
|
|
await page.goto(link); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
await this.reply(segment.image(await page.screenshot({ |
|
fullPage: true |
|
}))) |
|
|
|
await browser.close(); |
|
} |
|
} |
|
|