0

I'm working on a Chrome extension where I need to use Puppeteer for web scraping and automation tasks. I understand that Puppeteer is typically used in a Node.js environment, but I want to run it within the background.js or content_script of my Chrome extension.

I came across this Stack Overflow question, but the accepted answer only discusses how to integrate Puppeteer into the popup.html of the extension. Unfortunately, that solution doesn't meet my requirements.

Is it possible to use Puppeteer directly in the background.js or content_script files of a Chrome extension? If so, what are the necessary steps or modifications I need to make? I would greatly appreciate any guidance or alternative solutions for achieving this.

Thank you in advance for your help!

TAGOPI
  • 13
  • 5
  • You can scrape the contents directly using a *content script*. – wOxxOm Jun 21 '23 at 05:23
  • @wOxxOm. But i don't want to open the tab. I don't want the user to see what is going on . I want to do it "headless-ly". Just go the x website , take data and display it to the user without him seeing the process and all. Thanks for the comment , i thought no one would answer the question , really apreciate it :) – TAGOPI Jun 22 '23 at 23:07
  • Maybe you can do it inside an [offscreen document](https://developer.chrome.com/docs/extensions/reference/offscreen/)'s iframe? See also [Getting around X-Frame-Options DENY in a Chrome extension?](https://stackoverflow.com/a/69177790) Anyway, puppeteer-inside-browser can't open an invisible process, even if you load it. – wOxxOm Jun 23 '23 at 04:41
  • @wOxxOm Thank you so much man. I can finally continue my devlopement. I previouslly to set up a cloud function server to run puppeteer and exchange data to the extension and a million other way to get around this. But you finally solved it up. And by the way how did you know about it, a searched a lot but couldn't find a solution to it. Anyway you are probablly a senior software engeneer at google or somthing. Any way just wanted to express my gratitude, thanks. – TAGOPI Jun 23 '23 at 12:56
  • @wOxxOm I just realized that i can only use chrome.offscreen to load a .html document. Wich is not what i wanted, my goal is to scrap data of websites on the internet. Not my own html document. Would apreciat any feedback. thanks. – TAGOPI Jun 24 '23 at 16:23
  • You would add an iframe inside the offscreen document, and its src would point to the web site. Your *content script* would run inside the iframe and scrape it, then send the data to the parent or to the background script. – wOxxOm Jun 24 '23 at 16:40
  • @wOxxOm Is there a way to view the logs of the offscreenDocument ? – TAGOPI Jun 24 '23 at 20:20
  • @wOxxOm i have setted up the chrome.offscreen and added the iframe. Now im stuck at the X-frame options: i took a look at the link you provided me. But im not sure why it doesn't work. Where should i put the chrome.runtime.onInstalled.addListiner code ? – TAGOPI Jun 24 '23 at 23:09
  • 1) Open chrome://extensions then the details of the extension then click the offscreen link. 2) In the background script (service worker). The answer I've linked should contain all the necessary info. – wOxxOm Jun 25 '23 at 05:03

0 Answers0