简体   繁体   English

Javascript / Node.js中的嵌套类实例

[英]Nested classes instances in Javascript/Node.js

I'm using puppeteer which is a NodeJS module that controls chrome. 我正在使用puppeteer,它是控制chrome的NodeJS模块。

It has 2 functions to initiate a new browser and a new page. 它具有2个功能来启动新的浏览器和新的页面。

const browser = await puppeteer.launch() and browser.newPage() const browser =等待puppeteer.launch()和browser.newPage()

I want to create a class for creating a new page and new browser. 我想创建一个用于创建新页面和新浏览器的类。

This is the old way I was doing it, without classes, it works but it doesn't allow me to create new pages. 这是我做旧的方法,没有类,虽然可以,但是不允许我创建新页面。 This is why I want to move to using classes. 这就是为什么我想转向使用类。

let chrome = {}

chrome.init = async (options) => {
    chrome.browser = await puppeteer.launch(options)
    chrome.page = await chrome.browser.newPage()   
}

chrome.pageContains = async (string) => {

    return await chrome.page.evaluate( (string) => {

        const regex = new RegExp(string, 'i')

        return regex.test( document.querySelector('body').innerText )

    }, string)
}

module.exports = chrome

Here's my new code but I have no idea what I'm doing, it's obviously wrong and makes no sense. 这是我的新代码,但是我不知道自己在做什么,这显然是错误的,没有任何意义。

chrome.init = async (options) => {
    return {browser: await new Chrome(options)
}

class Chrome {
    async constructor(options) {
        this.browser = await puppeteer.launch(options)
    }

    newPage() {
        return await this.browser.newPage()
    }
}

class Page {
    async constructor() {
        this.page = await chrome.browser.newPage()
    }

}

So how do I make my old code work using classes instead of an object? 那么,如何使用类而不是对象来使旧代码工作?

You have some typo and constructors are not async. 您有一些错字,并且构造函数不是异步的。

Other than that, you simply have to pass the right browser function to the Page class. 除此之外,您只需要将正确的浏览器功能传递给Page类。 You can extend Chrome with page and use super , or keep them separate, but the page must have access to the browser at some point. 您可以使用页面扩展Chrome并使用super ,也可以将它们分开,但是该页面有时必须能够访问浏览器。

First, we will launch the browser, and return it. 首先,我们将启动浏览器并返回它。 Await will take care of promises. 等待将兑现诺言。

const puppeteer = require('puppeteer');

class Chrome {
    constructor(options) {
        this.browser = puppeteer.launch(options);
        return this.browser;
    }
}

Then we pass it to the page constructor and use it from there. 然后,将其传递给页面构造函数,然后从那里使用它。

class Page {
    constructor(browser) {
        this.browser = browser;
        this.page = browser.newPage();
        return this.page;
    }

    async pageContains(string){
        return await this.browser.page.evaluate( (string) => {
            const regex = new RegExp(string, 'i')
            return regex.test( document.querySelector('body').innerText )
        }, string)
    }
}

Then, we call them and make them usable. 然后,我们调用它们并使它们可用。 And return the browser and page object if needed. 并根据需要返回浏览器和页面对象。

const getChrome = async (options) => {
    const browser = await new Chrome(options);
    const page = await new Page(browser);
    return { browser, page }
}

Now we can use them. 现在我们可以使用它们了。

(async ()=>{
    const page = (await getChrome({headless: false})).page;
    await page.goto('http://example.com');
})()

I am pretty sure it can be refactored and what I wrote here is not the best practice, but this will get you going. 我很确定它可以重构,而我在这里写的并不是最佳实践,但这可以助您一臂之力。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM