Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using SingleFile in a different extension #854

Open
iasinov opened this issue Jan 6, 2022 · 10 comments
Open

Using SingleFile in a different extension #854

iasinov opened this issue Jan 6, 2022 · 10 comments

Comments

@iasinov
Copy link

@iasinov iasinov commented Jan 6, 2022

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

@gildas-lormeau
Copy link
Owner

@gildas-lormeau gildas-lormeau commented Jan 6, 2022

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.

@iasinov
Copy link
Author

@iasinov iasinov commented Jan 7, 2022

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.

@gildas-lormeau
Copy link
Owner

@gildas-lormeau gildas-lormeau commented Jan 7, 2022

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

@iasinov
Copy link
Author

@iasinov iasinov commented Jan 7, 2022

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
Copy link
Author

@iasinov iasinov commented Jan 9, 2022

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

@gildas-lormeau
Copy link
Owner

@gildas-lormeau gildas-lormeau commented Jan 10, 2022

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

@iasinov
Copy link
Author

@iasinov iasinov commented Jan 10, 2022

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

@gildas-lormeau
Copy link
Owner

@gildas-lormeau gildas-lormeau commented Jan 10, 2022

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

@iasinov
Copy link
Author

@iasinov iasinov commented Jan 10, 2022

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

@gildas-lormeau
Copy link
Owner

@gildas-lormeau gildas-lormeau commented Jan 10, 2022

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants