0

I have a dire need to implement the following:

  1. Collect and structure data from http://source.site (no injection needed, just DOM)
  2. Pass it somehow the background script (workers in V3? so confusing)
  3. Register global hotkeys for the extension accessible on any * normal page/all tabs/everywhere
  4. When specific hotkey is pressed, the data that was sent to the background script earlier is inserted into clipboard (or active element of type INPUT)

Issues with #2: How can I pass complex object/data it to persistent background worker that ALWAYS active, even if all tabs/pages closed and just startup page open? I know you can use convoluted and ugly window.postMessage to communicate between injected and content scripts - is it the same here?

Issues with #3: after some extensive googling I found out that with manifest V3 google also tightened this and now super-global hotkeys are not possible from background scripts. So my only choice is to ALWAYS execute content script on * addresses and just write old good window.onkeydown?

Issues with #4: it appears only injected scripts can affect clipboard. Current active/focused element can be accessed from content script so I'm back to applying content scripts to every web-site then?

Right now, the only way I see this being done is:

  • execute parsing on a hotkey registered on content script and collect data if URI matches http://source.site
  • store data in extension's localStorage?
  • on tab focus event, check if the URI != http://source.site and then fetch data so it is ready
  • register the rest of the hotkeys and insert data on press

But it feels like there has to be a better, more sane way.

Kirikan
  • 119
  • 6
  • 1) Use a content script. 2) Use [messaging](https://developer.chrome.com/extensions/messaging). 3) Global hotkeys still work. 4) Simply use offscreen document + execCommand as shown in the [official example](https://github.com/GoogleChrome/chrome-extensions-samples/tree/main/functional-samples/cookbook.offscreen-clipboard-write). – wOxxOm Jun 28 '23 at 04:29
  • How would I register global hotkey now so that it is accessible in background worker? Can you expand all of this into an answer so I can accept it? – Kirikan Jun 28 '23 at 07:55
  • Same as always, AFAIK, by adding `global` key, see [the documentation](https://developer.chrome.com/docs/extensions/reference/commands/#scope). – wOxxOm Jun 28 '23 at 07:58
  • What about [persistence of the data](https://stackoverflow.com/questions/66618136/persistent-service-worker-in-chrome-extension)? I will take penalty of creating offscreen html document every time I need to write to clipboard (up to 20 times in less than 1minuite), but deserializing/serializing string from JSON to object every time from localStorage is... ass? – Kirikan Jun 28 '23 at 08:13
  • It's not a problem, at least currently: just don't close the document. – wOxxOm Jun 28 '23 at 08:22
  • So I keep the state on the script that is loaded by offscreen page? Or the worker? – Kirikan Jun 28 '23 at 09:37
  • Keep the state in the offscreen document. – wOxxOm Jun 28 '23 at 13:00
  • @wOxxOm The issue is that I cannot communicate from content script with offscreen page script. – Kirikan Jun 28 '23 at 15:19
  • You can use chrome.runtime.connect in the content script to initiate connection and chrome.runtime.onConnect in the offscreen document, see [the documentation](https://developer.chrome.com/extensions/messaging#connect). – wOxxOm Jun 28 '23 at 16:08
  • @wOxxOm Thanks! I've come across yours and "Jackie Chan's" comments on [GoogleGroups](https://groups.google.com/a/chromium.org/g/chromium-extensions/c/D5Jg2ukyvUc/m/VaSvEfoHAgAJ) about the error of duplicate offscreen page. None of the solution actually worked for me, because it STILL outputs this error into extension widget no matter what. Have you ever found a solution to this? – Kirikan Jun 28 '23 at 18:38
  • Simply use try-catch. – wOxxOm Jun 28 '23 at 20:01
  • That's what I did, it still drops the error as uncaught. – Kirikan Jun 28 '23 at 20:34
  • Since it can't happen I guess there's a typo or you look at an old error in chrome://extensions UI or the error is caused by something else. – wOxxOm Jun 28 '23 at 20:52

0 Answers0