1

i am trying to use puppeteer in wordpress. I added all files from puppeteer core in plugin and use this code:

<?php 
/**
 * Plugin Name: Test Puppetter
 * Description: This plugin brings some good functionalities 
 * Version: 1.0
 * Author: Vladimir Kyatipov
 * Author URI: mailto:kqtipow
 */

 add_action('wp_footer', 'some_info');
 function some_info() {
    ?>
    <script type="text/javascript">
const puppeteer = require('puppeteer');
const URL = 'https://coding.napolux.com';

puppeteer.launch({ headless: true, args: ['--no-sandbox', '--disable-setuid-sandbox'] }).then(async browser => {
    const page = await browser.newPage();
    await page.setViewport({width: 320, height: 600})
    await page.setUserAgent('Mozilla/5.0 (iPhone; CPU iPhone OS 9_0_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13A404 Safari/601.1')

    await page.goto(URL, {waitUntil: 'networkidle0'});
    await page.waitForSelector('body.blog');
    await page.addScriptTag({url: 'https://code.jquery.com/jquery-3.2.1.min.js'})

    const result = await page.evaluate(() => {
        try {
            var data = [];
            $('h3.loop__post-title').each(function() {
                const url = $(this).find('a').attr('href');
                const title = $(this).find('a').attr('title')
                data.push({
                    'title' : title,
                    'url'   : url
                });
            });
            return data; // Return our data array
        } catch(err) {
            reject(err.toString());
        }
    });

    // let's close the browser
    await browser.close();

    // ok, let's log blog titles...
    for(var i = 0; i < result.length; i++) {
        console.log('Post: ' + result[i].title + ' URL: ' + result[i].url);
    }
    process.exit();
}).catch(function(error) {
    console.error('No way Paco!');
    process.exit();
});
    </script>
    <?php
 }

I get error on images. How i can make it work? Is there any way I can use headless browser on wordpress to get some info back to it? Thanks

enter image description here

enter image description here

  • 1
    Looks like you need to look into [puppeteer web](https://stackoverflow.com/a/54654516/231316) – Chris Haas Jan 04 '22 at 14:43
  • 1
    Does this answer your question? [How to run Puppeteer code in any web browser?](https://stackoverflow.com/questions/54647694/how-to-run-puppeteer-code-in-any-web-browser) – ggorlen Jan 04 '22 at 20:21
  • Hello, thanks both of you. I am struggling with running puppeteer web and will open a new topic about it. Thanks – Vladimir Kyatipov Jan 04 '22 at 20:48

0 Answers0