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)
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!
no problem, glad I could help :)