0

I'm trying to create a chrome extension which has a form where I take notes.

Say I'm reading Wikipedia. Whilst reading I want to open the extension popup, paste/write something, close the popup, read some more, open the popup, past/write some more.

At the end, I'd hit submit and send the data to API.

Every time the popup closes though the data on the form is gone. So I thought, on popup closing, I'd add the data to local storage (not sure if there's another way)

So I'm trying to use the background script.

In the manifest file

"background": {
    "scripts": ["background.js"],
    "persistent": false
  }

And in the background.js

    chrome.runtime.onConnect.addListener(function(externalPort) {
   externalPort.onDisconnect.addListener(function() {
      console.log("onDisconnect - nothing logs");
      localStorage.setItem("test", "test");
   });
   console.log("onConnect - nothing logs");
});
console.log("this logs")

That doesn't work. All the research led me to chrome.runtime.onConnect.addListener but there's no response at all, (the background.js is linked correctly)

relidon
  • 2,142
  • 4
  • 21
  • 37
  • I believe extensions have access to their own storage API: https://developer.chrome.com/apps/storage – DBS Sep 18 '19 at 13:09
  • The correct approach is to save the pasted data immediately in a listener for `input` event because when the popup is closed its environment is destroyed so fast you won't have time. Also, there's nothing wrong with using HTML5 `localStorage` for saving small strings. – wOxxOm Sep 18 '19 at 13:28
  • @wOxxOm isn't that "expensive", on keyup do the operation? Or is there no such thing as expensive in this case? (I don't want to do it on button click) – relidon Sep 18 '19 at 14:38
  • 1
    It's not expensive unless you're running on a 30-year old CPU, but anyway whenever a performance-related concern arises you should always check first using the excellent performance profiler and other modern tools. Also, you can use **debouncing**. – wOxxOm Sep 18 '19 at 14:48
  • Re the last part, see [this](https://stackoverflow.com/questions/24004791/can-someone-explain-the-debounce-function-in-javascript). – wOxxOm Sep 18 '19 at 14:56

0 Answers0