最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use addScriptTag() with local file path in Puppeteer - Stack Overflow

programmeradmin0浏览0评论

I am trying to use puppeteer with a local script file.

I get the script file to load when I host the file and use addScriptTag() with the localhost address. This is not ideal. I need to use the local file directly from the path. The current working directory is /maps in this case. I set the relative path as path in the options of the addScriptTag() function but the code just goes dark on me at this stage. There is no error and no stepping into anything.

console.log(`Current directory: ${process.cwd()}`);
// C:\Users\dbauszus\Documents\GitHub\maps
(async () => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.setContent(jsr.templates('./views/report.html').render(), {waitUntil: 'load'});
    // works with an url to the same file.
    // await page.addScriptTag('http://localhost:3000/maps/js/build/report_bundle.js');
    // path for js file on windows C:\Users\dbauszus\Documents\GitHub\maps\public\js\build\report_bundle.js
    await page.addScriptTag({path: 'public\\js\\build\\report_bundle.js'});
    await page.screenshot({path: 'example.png'});
    await browser.close();
})();

Any help will be wele as I find the puppetteer documentation increasingly frustrating and there aren't (m)any working examples as of now.

I am trying to use puppeteer with a local script file.

I get the script file to load when I host the file and use addScriptTag() with the localhost address. This is not ideal. I need to use the local file directly from the path. The current working directory is /maps in this case. I set the relative path as path in the options of the addScriptTag() function but the code just goes dark on me at this stage. There is no error and no stepping into anything.

console.log(`Current directory: ${process.cwd()}`);
// C:\Users\dbauszus\Documents\GitHub\maps
(async () => {
    const browser = await puppeteer.launch();
    const page = await browser.newPage();
    await page.setContent(jsr.templates('./views/report.html').render(), {waitUntil: 'load'});
    // works with an url to the same file.
    // await page.addScriptTag('http://localhost:3000/maps/js/build/report_bundle.js');
    // path for js file on windows C:\Users\dbauszus\Documents\GitHub\maps\public\js\build\report_bundle.js
    await page.addScriptTag({path: 'public\\js\\build\\report_bundle.js'});
    await page.screenshot({path: 'example.png'});
    await browser.close();
})();

Any help will be wele as I find the puppetteer documentation increasingly frustrating and there aren't (m)any working examples as of now.

Share Improve this question asked Oct 16, 2017 at 15:56 Dennis BauszusDennis Bauszus 1,8024 gold badges21 silver badges50 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

It's a version problem. This method was only introduced in the latest update 0.12 from yesterday. I installed puppeteer the day before. Meh!

https://github./GoogleChrome/puppeteer/issues/949

发布评论

评论列表(0)

  1. 暂无评论