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?