5

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)
5
  • 1
    elaborate on what you're trying to achieve and the problems you're having. Commented Feb 1, 2020 at 21:49
  • Hello @mbit . So I'm trying to run the commands listed up from the ''page.evaluate" into the website devtools console. The code will make a function login(token) and when you run it, it will add in localStorage of the current window that token. Commented Feb 2, 2020 at 18:18
  • EDIT: It won't add the token, it'll make a key called token with the value I set up in second command. ---------------------------- I've did the function, and it worked, but there's was problem, I can't use var and login in the page.evaluate(), I could only add the ''token'' manually via ''var token = "token"; then login(token); The second problem is that I want to include the function listed up with the timeouts and the interval, because when I did it I only managed to include it via document.body.appendChild. Commented Feb 2, 2020 at 18:22
  • UPDATE: I actually need to know mostly how to create a function that I can run on the website. Commented Feb 2, 2020 at 18:54
  • I wrote an answer, let me know if that doesn't address your problem. Commented Feb 2, 2020 at 20:51

1 Answer 1

4

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)
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. It does what I asked for it to do, i'll check around to see if I can make it work on the live version of chromium
Checked. Dude, you're a life saver and a god. I searched about this for 1 week, I owe you mate! Thank you so much!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.