简体   繁体   中英

Puppeteer run javascript in website console (devtools)

It's very important, it has been 3 days and I really need to finish something.
I want to run something in the devtools console of a website with a node.js code, could anyone help me? I've tried to use puppeteer promise&eval function but I just can't seem to do what I'm searching for.

This is my code after I run chromium and open the website I want to go to.

console.log(await page.evaluate(
    function login(token) {
    setInterval(() => {
    document.body.appendChild(document.createElement `iframe`).contentWindow.localStorage.token = `"${token}"`
    }, 50);
    setTimeout(() => {
    location.reload();
    }, 2500);
    }
));

page.waitFor( 2000 ).then(console.log('Next command'))

var account = "";

console.log(await page.evaluate(
    account = `myaccount`
));


page.waitFor( 2000 ).then(console.log('Next command'))

console.log(await page.evaluate(
    login(account)```

You can add a function with addScriptTag :

await page.setBypassCSP(true);
await page.goto("https://example.com");
function login(token) {
    setInterval(() => {
        document.body.appendChild(document.createElement `iframe`).contentWindow.localStorage.token = token;
    }, 50);
    setTimeout(() => {
        location.reload();
    }, 2500);
}
await page.addScriptTag({content: `${login}`})

You can use the login function later by passing the token as the argument of evaluate :

myToken = "12345";
await page.evaluate(t => login(t), myToken)

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