简体   繁体   中英

'MessageChannel is undefined' triggered on pages with reCaptcha v2

I'm currently trying to scrape some sites that have recaptcha implemented, but whenever i load the page's source i get this error: (node:15536) UnhandledPromiseRejectionWarning: ReferenceError: MessageChannel is not defined .

I have tried to implement the fix here: https://github.com/jsdom/jsdom/issues/2448#issuecomment-536242756 , but it still throws the error.

Here is my code:

const jsdom = require('jsdom');
const request = require('request');

const { JSDOM } = jsdom;

const url = 'https://www.google.com/recaptcha/api2/demo';

function getPage() {
    return new Promise(function(resolve, reject) {
    request.get(url, function(error, response, body) {
        if (error) {
            reject(error);
        }
        else {
            resolve(body);
        }
    }
    });
}

(async() => {
    const dom = new JSDOM(await getPage(), {
        url,
        contentType: 'text/html',
        runScripts: 'dangerously',
        resources: 'usable',
        pretendToBeVisual: true,
        beforeParse(window) {
            window.MessageChannel = require('worker_threads').MessageChannel;
        }
    });
})();

Thanks in advance,

iLinked

MessageChannel is not available in the iframe context.

beforeParse functions won't run in frames: https://github.com/jsdom/jsdom/issues/2920

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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