SingleFile icon indicating copy to clipboard operation
SingleFile copied to clipboard

Using SingleFile in a different extension

Open iasinov opened this issue 4 years ago • 26 comments

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

iasinov avatar Jan 06 '22 16:01 iasinov

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.

gildas-lormeau avatar Jan 06 '22 21:01 gildas-lormeau

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.

iasinov avatar Jan 07 '22 08:01 iasinov

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

gildas-lormeau avatar Jan 07 '22 13:01 gildas-lormeau

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.

iasinov avatar Jan 07 '22 13:01 iasinov

@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 avatar Jan 09 '22 10:01 iasinov

@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 avatar Jan 10 '22 14:01 gildas-lormeau

@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!

iasinov avatar Jan 10 '22 15:01 iasinov

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).

singlefile-mv3-test.zip

gildas-lormeau avatar Jan 10 '22 16:01 gildas-lormeau

Looks like this is what I needed! Thank you so much!

iasinov avatar Jan 10 '22 16:01 iasinov

You're welcome. Just to warn you and remind you, the code is under AGPL license.

gildas-lormeau avatar Jan 10 '22 16:01 gildas-lormeau

@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

iasinov avatar Jan 12 '22 11:01 iasinov

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).

singlefile-mini3.zip

gildas-lormeau avatar Jan 12 '22 15:01 gildas-lormeau

Now it works like a charm! I can't thank you enough!

iasinov avatar Jan 13 '22 07:01 iasinov

You're welcome!

gildas-lormeau avatar Jan 13 '22 16:01 gildas-lormeau

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 avatar Sep 11 '23 16:09 nicolalopatriello

@nicolalopatriello The standard API to use is browser.runtime.sendMessage, I don't know how what is the corresponding API in plasmo unfortunately.

gildas-lormeau avatar Sep 11 '23 17:09 gildas-lormeau

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 avatar Sep 11 '23 19:09 nicolalopatriello

@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

gildas-lormeau avatar Sep 11 '23 19:09 gildas-lormeau

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:

image

Thanks

nicolalopatriello avatar Sep 11 '23 20:09 nicolalopatriello

You should use importScripts.

gildas-lormeau avatar Sep 11 '23 21:09 gildas-lormeau