0

Well I need to use puppeteer in my Chrome extension. I was trying ways to do this and found the puppeteer-web, it's a bundled puppeteer, and using it for a few days I realized he can't run puppeteer in headless mode. So I gave up on running puppeteer-web and tried to run it through my extension like this:

Open a local node.js server which actually gets the puppeteer run in headless mode and then the extension makes an ajax request to that application in order to run it.

App.js => This is the code that runs the server and calls the function that will run the puppeteer:

const express = require('express');
const path = require('path');
var $ = require('jquery');
const pages = require('./pages');
const server = express();
var router = express.Router();

router.all('*', function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Headers", "X-Requested-With");
  res.header("Access-Control-Allow-Methods", "PUT, GET,POST,OPTIONS");
  next();
 }); //here I tried to solve the "method not allowed". it not works.


server 
.use(express.urlencoded({extended:true}))
.use(express.static('public'))
//set the path of the jquery file to be used from the node_module jquery package
.use('/jquery',express.static(path.join(__dirname+'/node_modules/jquery/dist/')))

.set('views',path.join(__dirname,"views"))
.set('view engine','hbs')
 
.get('/', pages.index)
 
.listen(process.env.PORT || 5502) 

pages.js => The code that runs puppeteer:

const puppeteer = require('puppeteer');
const path = require('path');


module.exports = {
    index: async function follow(){
            try{
                
            // Aqui cria uma instÂncia no browser
            const browser = await puppeteer.launch({
                headless:true,
                args:["--no-sandbox",'--disable-setuid-sandbox']
                
            });
        
            // Aqui carrega a página
            const page = await browser.newPage();
            await page.setDefaultNavigationTimeout(0); //seta tempo ilimitado de espera
            await page.goto("https://www.instagram.com/");

            [....]
}

test.html => Front-end that makes the request:

<script>
        const xhr = new XMLHttpRequest();
        xhr.onload = () => {
            if(xhr.status === 200){
                console.log(xhr.responseText);
            }else{
                console.log("error")
            }
        }
        xhr.open("POST", "../src/pages.js");
        xhr.send(null);

        
    </script>

After all, the request printed on the console: "405 (Method Not Allowed)"

does anyone know how to do this? It's possible?

sigleane
  • 63
  • 6
  • 1
    For future reference, it's preferable to [edit your old post](https://stackoverflow.com/questions/69798461/it-is-possible-run-puppeteer-in-headless-mode-by-ajax?noredirect=1#comment123402833_69798461) if it doesn't have any answers rather than creating a whole new one. I'm going to re-post [Parallelism of Puppeteer with Express Router Node JS. How to pass page between routes while maintaining concurrency](https://stackoverflow.com/questions/66935883/66946923#66946923) which shows how to communicate with Puppeteer via curl, trivially adjustable to `fetch`. – ggorlen Nov 02 '21 at 22:24
  • 1
    As I mentioned in that post, you can't just do a POST to a script like `xhr.open("POST", "../src/pages.js");` and expect it to work. You'll need to run your server like `node App.js`, then point your browser or curl to `localhost:5502` (or whatever `process.env.PORT` is). As such, this doesn't really seem to be a Puppeteer question -- the problem seems to be "how do I run an Express server and access a route?" Anyway, you've not defined a POST route in Express, only GET, so even with a running server, there's no endpoint. I recommend working through a basic Express tutorial. – ggorlen Nov 02 '21 at 22:28

0 Answers0