lintonxue00 commited on
Commit
4598406
1 Parent(s): 7784286

Upload webPreview.js

Browse files
Files changed (1) hide show
  1. 不知道/回收站/1/webPreview.js +156 -0
不知道/回收站/1/webPreview.js ADDED
@@ -0,0 +1,156 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import plugin from '../../../lib/plugins/plugin.js'
2
+ import { segment } from 'oicq'
3
+ import lodash from "lodash";
4
+
5
+ import { createRequire } from 'module'
6
+ const require = createRequire(import.meta.url)
7
+
8
+ export class webPreview extends plugin {
9
+ constructor() {
10
+ super({
11
+ /** 功能名称 */
12
+ name: '截图预览网页内容',
13
+ /** 功能描述 */
14
+ dsc: '群里发送网页地址,截图预览网页内容',
15
+ /** https://oicqjs.github.io/oicq/#events */
16
+ event: 'message',
17
+ /** 优先级,数字越小等级越高 */
18
+ priority: 1006,
19
+ rule: [
20
+ {
21
+ /** 命令正则匹配 */
22
+ reg: '^(?:(http|https):\/\/)?((?:[\\w-]+\\.)+[a-z0-9]+)(?::\\d+)?((?:\/[^\/?#]*)+)?(\\?[^#]+)?(#.+)?$'
23
+ ,
24
+ /** 执行方法 */
25
+ fnc: 'webPreview'
26
+ },
27
+ {
28
+ /** 命令正则匹配 */
29
+ reg: '^#*必应(.*)$',
30
+ /** 执行方法 */
31
+ fnc: 'bingWeb'
32
+ },
33
+ ]
34
+
35
+ })
36
+ }
37
+
38
+ /**
39
+ *
40
+ * @param e oicq传递的事件参数e
41
+ */
42
+ async webPreview(e) {
43
+ const puppeteer = require('puppeteer');
44
+ const browser = await puppeteer.launch({
45
+ headless: true,
46
+ args: [
47
+ '--disable-gpu',
48
+ '--disable-dev-shm-usage',
49
+ '--disable-setuid-sandbox',
50
+ '--no-first-run',
51
+ '--no-sandbox',
52
+ '--no-zygote',
53
+ '--single-process'
54
+ ]
55
+ });
56
+ const page = await browser.newPage();
57
+ try {
58
+ await page.goto(e.msg, { timeout: 30000, waitUntil: 'networkidle0' });
59
+ } catch (err) {
60
+ console.error(`Failed to navigate to ${e.msg}: ${err.message}`);
61
+ await browser.close();
62
+ return;
63
+ }
64
+ await page.setViewport({ width: 1920, height: 1080 });
65
+ let screenshot;
66
+ try {
67
+ screenshot = await page.screenshot({ fullPage: true });
68
+ } catch (err) {
69
+ console.error(`Failed to take screenshot: ${err.message}`);
70
+ await browser.close();
71
+ return;
72
+ }
73
+ await this.reply(segment.image(screenshot));
74
+ await browser.close();
75
+ }
76
+
77
+
78
+ /**
79
+ *
80
+ * @param e oicq传递的事件参数e
81
+ */
82
+ async bingWeb(e) {
83
+
84
+ let webkeywd = e.msg.replace(/#|必应/gm, '');
85
+
86
+ webkeywd = webkeywd.replace(/,| |,/g, ",");
87
+
88
+ let wdKey = webkeywd.split(",");
89
+
90
+ wdKey = lodash.compact(wdKey);
91
+
92
+ let keyWd = '';
93
+ let searchKey = '';
94
+
95
+ if(wdKey.length > 1){
96
+ searchKey = wdKey[0]
97
+ keyWd = wdKey[1];
98
+ }
99
+
100
+ console.log("keyWd",keyWd);
101
+
102
+ let weburl = `https://www.bing.com/search?q=${searchKey?searchKey:webkeywd}`;
103
+
104
+ const puppeteer = require('puppeteer');
105
+
106
+ const browser = await puppeteer.launch({
107
+ headless: true,
108
+ args: [
109
+ '--disable-gpu',
110
+ '--disable-dev-shm-usage',
111
+ '--disable-setuid-sandbox',
112
+ '--no-first-run',
113
+ '--no-sandbox',
114
+ '--no-zygote',
115
+ '--single-process'
116
+ ]
117
+ });
118
+ const page = await browser.newPage();
119
+ await page.goto(weburl);
120
+ await page.setViewport({
121
+ width: 1920,
122
+ height: 1080
123
+ });
124
+
125
+ if(keyWd != '列表'){
126
+
127
+ let link = await page.evaluate(async keyWd => {
128
+
129
+ return [...document.querySelectorAll('.result a')].filter(item => {
130
+ return item.innerText && item.innerText.includes(keyWd)
131
+ })[0].toString();
132
+ },keyWd);
133
+
134
+ if(link){
135
+ // console.log("找到");
136
+ link = link.toString();
137
+ }else{
138
+ // console.log("没找到");
139
+ link = weburl;
140
+ }
141
+ console.log(link);
142
+
143
+ await page.goto(link);
144
+
145
+ }
146
+
147
+
148
+
149
+
150
+ await this.reply(segment.image(await page.screenshot({
151
+ fullPage: true
152
+ })))
153
+
154
+ await browser.close();
155
+ }
156
+ }