Using SingleFile in a different extension
Hello guys! First of all, I'd like to Thank you @gildas-lormeau, and all the contributors for the great package! Great work 👏 I'd like to ask your advice on the following.
I've spent a lot of time trying to make something similar to what your extension (or CLI tool) does. But the result of SinglePage is unbeatable for me.
I need to capture the page in the current state (via the Chrome Extension) and send the HTML content to the backend. Is there some kind of API (or elegant way to do it) in SinglePage to make exactly what your extension does but not to download it, but to get the content in the extension?
Thank you very much in advance
Hi! Thank you for the kind words. I documented the procedure to integrate the core part of SingleFile into an extension here: https://github.com/gildas-lormeau/SingleFile/wiki/How-to-integrate-the-API-of-SingleFile-into-an-extension. Please let me know if you need more details.
Wow, I didn't expect to receive such a detailed answer. Thank you very much!
I'm just curious. Do you anticipate any difficulties with migrating to manifest v3? Manifest v2 support timeline doesn't look optimistic.
You're welcome! I did the effort to port SingleFile to the manifest V3. This will result in some regressions. The most notable is that the "auto-save" feature will no longer work. See here for more info: https://github.com/gildas-lormeau/SingleFile-Lite
Thank you so much @gildas-lormeau. I'll try using the core functionality of SingleFile Lite with manifest v3. Looks to be pretty much the same from an implementation standpoint.
@gildas-lormeau If you don't mind, I'd like to ask you what will be the way of executing the p.3 in the document you've written.
Let's say we have an extension popup with the button which should trigger getPageData there. So what will the extension object containing getPageData method in this case?
Executing content script (by chrome.scripting.executeScript) on the page to be captured will not work as there's no extension or singlefile defined on that page. Could you please clarify a little?
Besides that, it works great. Thank you!
@iasinov You should use message passing to solve this issue, see https://developer.chrome.com/docs/extensions/mv3/messaging/. This is how the background page/service worker get the result from getPageData in SingleFile.
@gildas-lormeau thanks for your answer. Yes, I understand I need to use messaging to communicate between the parts of the extension.
The thing which confuses me is where the singlefile object lives.
Do I understand right that the extension on document_start should inject dist/single-file.js together with the other files (which you've listed in the doc) and the singlefile object (with getPageData method inside) should become available in the window object of the page to be captured? And after that, I can execute the script from the popup which calls this function?
And I think I'm wrong as there's no singlefile object defined in the window 😂
I owe you a lot!
I guess your problem is due to the fact that you're running the code in a MV3 extension. Indeed, I noticed that chrome-browser-polyfill.js should be included in single-file-backgound.js because background.service_worker accepts only 1 file in the manifest.json file. I attached a zip file containing a project with this issue fixed (manually) and a popup page able to communicate with the injected content script. The content script logs the singlefileobject (see content.js).
Looks like this is what I needed! Thank you so much!
You're welcome. Just to warn you and remind you, the code is under AGPL license.
@gildas-lormeau this is me again 😂 Sorry for bothering you, but I still can't find the answer to the problem. Looks like something is missing.
When I run the const { content } = await singlefile.getPageData({...}) in the content script (of the extension you've attached above) I get the result too fast. Looks like nothing is being downloaded/taken from the cache. And the content variable contains the HTML but all the resources are like data:null;base64,.
I tried both with Manifest v2 & v3. Just the same result. If you have an idea of what is the problem / how it can be fixed, I'll be very grateful
The issue was related to the support of the "accept" header I recently added and the fact that I did not finished the job regarding the "mini" MV3 version. The bug should be fixed now if you update the code from GitHub. Note that I added a wiki page for MV3 here: https://github.com/gildas-lormeau/SingleFile-Lite/wiki/How-to-integrate-the-API-of-SingleFile-Lite-into-an-extension.
Here is also the zip file with the issue fixed. Note that you have to call extension.getPageData instead of singlefile.getPageData (dist/extension-core.js has also been added as a content script).
Now it works like a charm! I can't thank you enough!
You're welcome!
Hi @gildas-lormeau!
Can you suggest me an example to use the extension (like this comment https://github.com/gildas-lormeau/SingleFile/issues/854#issuecomment-1011140043) in a project extension with plasmo framework: https://docs.plasmo.com/framework
Thanks
@nicolalopatriello The standard API to use is browser.runtime.sendMessage, I don't know how what is the corresponding API in plasmo unfortunately.
In your"singlefile-mini3.zip" there is a extension.getPageData() in content.js file.
I'm trying to use the library in the same way in my plasmo framework extension but, in my opinion, i'm stucked with same file imports.
The idea is to call getPageData() method from my content script.
Can you try a simple example from this sketch: https://github.com/PlasmoHQ/examples/tree/main/with-src
npm create plasmo -- --with-src
Can you try and help me? Thanks!
https://github.com/PlasmoHQ/examples https://docs.plasmo.com
@nicolalopatriello Sorry, I didn't notice that the link for the MV3 documentation did not exist actually. It's fixed now, see https://github.com/gildas-lormeau/SingleFile-Lite/wiki/How-to-integrate-the-API-of-SingleFile-Lite-into-an-extension
Okok, but in step 2:
"You can replace /lib/single-file-background.js with your background script and import /lib/single-file-background.js in your script."
If you have a file like this: https://github.com/PlasmoHQ/examples/blob/main/with-src/src/background.ts
what should be the import of single-file-background.js?
This is my folder structure:
Thanks
You should use importScripts.