0

Well.., I am new to puppeteer and electron too.

I have a basic puppeteer code working as below :

const puppeteer = require('puppeteer');


(async () => 
{
    const browser = await puppeteer.launch({ headless: false });

    const page = await browser.newPage();
    await page.setViewport({ width: 1366, height: 768});

    await page.goto('https://www.google.co.in', {waitUntil: 'networkidle2'});

})();

That's great. Now i do not wants to open a browser whenever my electron app starts. Rather i want to open it on a particular event and within a javascript function. Which will be called whenever i want. As below :

global.RunCommand = function(param)
{
    const browser = await puppeteer.launch({ headless: false });

    const page = await browser.newPage();
    await page.setViewport({ width: 1366, height: 768});

    await page.goto('https://www.google.co.in', {waitUntil: 'networkidle2'});


    return 'Done : ' + param;
}

which throws :

const browser = await puppeteer.launch({ headless: false });
                    ^^^^^^^^^

SyntaxError: Unexpected identifier
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:606:28)
at Object.Module._extensions..js (module.js:653:10)
at Module.load (module.js:561:32)
at tryModuleLoad (module.js:504:12)
at Function.Module._load (module.js:496:3)

Any help will be appreciated and thanks in advance.

1
  • pupeeteer is not an electron project Commented May 27, 2018 at 13:32

1 Answer 1

3

global.RunCommand is a async function, you must always declare the async word in an asynchronous function

global.RunCommand = async function(param)
{
 ...
}
Sign up to request clarification or add additional context in comments.

2 Comments

Yes it worked but i want to return the string 'done' on_complete; But it returning string 'done' before completing code..!!??
@sandhyasasane You can use a callback function for that

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.