File size: 4,349 Bytes
4598406
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
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: '群里发送网页地址,截图预览网页内容',
            /** https://oicqjs.github.io/oicq/#events */
            event: 'message',
            /** 优先级,数字越小等级越高 */
            priority: 1006,
            rule: [
                {
                    /** 命令正则匹配 */
                    reg: '^(?:(http|https):\/\/)?((?:[\\w-]+\\.)+[a-z0-9]+)(?::\\d+)?((?:\/[^\/?#]*)+)?(\\?[^#]+)?(#.+)?$'
,
                    /** 执行方法 */
                    fnc: 'webPreview'
                },
                {
                    /** 命令正则匹配 */
                    reg: '^#*必应(.*)$',
                    /** 执行方法 */
                    fnc: 'bingWeb'
                },
            ]
     
        })
    }

    /**
     * 
     * @param e oicq传递的事件参数e
     */
    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();
      }
      

    /**
     * 
     * @param e oicq传递的事件参数e
     */
    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){
                // console.log("找到");
                link = link.toString();
            }else{
                // console.log("没找到");
                link = weburl;
            }
            console.log(link);
    
            await page.goto(link);

        }
        

        
    
        await this.reply(segment.image(await page.screenshot({
            fullPage: true
        })))
    
        await browser.close();
    }
}