radiolondra / ServiceWorker-Highlander

MV3 Extension: Service Worker awakened once stays alive forever
11 stars 1 forks source link

i don't understand how to use for my extension #1

Open hichemita opened 1 year ago

hichemita commented 1 year ago

thank you so much for this solution, i have extension that scrape data from amazon in the background,js and the background is launched after receiving a meesage from popup page so should take just leave my extension and add this socket solution directly or i need to put my scraping code inside the socket code.

radiolondra commented 1 year ago

In this release of Highlander, you can maintain your background.js active using one or more content scripts:

In your case it looks like you only use the popup so include the commonscript.js only there.

Then, you could simply import serviceworker.js in your background.js via importScript("serviceworker.js") and in your popup.js add the call to awakeServiceWorker() function (see popup.js of this repo). You don't need to move/modify your own code.

So it should work.

hichemita commented 1 year ago

thank you again, i already used the script and it works perfect, but there is a problem. in my extension i'm listening to contextmenu click event and sometimes the extension disconnect from the web socket directly and sometimes context menun events aren't captured by the background script. i tried both web sockets servers and same problem.

this was using the web socket solution but now after i used the solution on this repo until now there is no problem thank you very much for sharing with people may god bless.

i understand that you used web socket connection to maintain service worker active so for this can you just explain the mechanism of this solution in this repo

radiolondra commented 1 year ago

i tried both web sockets servers and same problem

I didn't understand which solution you are using, Highlander or Highlander-DNA. I wrote how-to-use instructions for you thinking you were using Highlander (the one with content scripts awaking service worker).

Highlander-DNA, instead, doesn't need any content script to maintan a service worker awake forever. It's not a ready-to-use solution, as a little intervent in the code is needed to adapt the serviceworker.js to the final user needs. For example, the 2 included WebSocket servers used are just as an example and can be safely removed. They were needed just to show how connections are awake forever too, thanks to the Highlander function and to ALL the listeners added in the serviceworker.js (which are needed). The local WebSocket server is used to debug your solution after you close the host browser. Read the Highlander-DNA README and the comments inside the servideworker.js and websockettest.js for more info. You can safely remove all the websocket references and modify the code in serviceworker.js to reach your own needs. Listeners (onInstalled, windows and tabs) are needed to start the extension already active even after a PC reboot and you open the browser and the code inside them must be preserved (you can add other code too in each listener and wherever you want, but the main code must be preserved). To make things simpler, you can simply:

That's all.