4

In my injected content script, I'm attempting to use the chrome storage api:

content.js

document.addEventListener("paste", (event) => {
        let paste = event.clipboardData.getData('text');
        var lookalike = isLikeWallet(paste)
        if (lookalike) {
            chrome.storage.sync.get("wallets", (items) => {
                var wallets = items.wallets;
                if(!wallets.includes(paste)) {
                    alert('alert')
                }
            });
        }
});

It worked the last time I tested it - a few days ago. Now I get the error:

Uncaught TypeError: Cannot read properties of undefined (reading 'sync')
    at HTMLDocument.<anonymous> (contentScript.js:10)

This storage API seems to be the accepted method for accessing shared state variables across your background, popup, and content scripts, and the documentation states: (https://developer.chrome.com/docs/extensions/reference/storage/)

Your extension's content scripts can directly access user data without the need for a background page.

I.e. I don't need to use the messaging API to send data back and forth.

Here's my manifest.json:

{
    "name": "Test extension",
    "description": "Description",
    "version": "1.0",
    "manifest_version": 3,
    "background": {
        "service_worker": "background.js"
      },
      "permissions": ["storage", "activeTab"],
      "action": {
        "default_popup": "popup.html",
        "default_icon": {
            "16": "/images/test16.png",
            "32": "/images/test32.png",
            "48": "/images/test48.png",
            "128": "/images/test128.png"
          }
      },
      "icons": {
        "16": "/images/test16.png",
        "32": "/images/test32.png",
        "48": "/images/test48.png",
        "128": "/images/test128.png"
      },
      "content_scripts": [
        {
          "matches": ["<all_urls>"],
          "js": ["contentScript.js"]
        }
      ]
  }

Thank you in advance for any help! Not sure what's going on here - especially since it worked (and reliably) just a few days ago.

Behold
  • 41
  • 1
  • 2
  • Looks like you add a ` – wOxxOm Sep 26 '21 at 16:09
  • Thanks for the potential fix! Looked through my `html` file, which is popup.html, and the only script element is for `popup.js`. Any other ideas?? – Behold Sep 26 '21 at 16:31
  • The popup script is an extension page so its scripts are not content scripts. Content scripts run only in web pages i.e. http:// and https:// sites, not in the extension pages. – wOxxOm Sep 26 '21 at 16:39
  • yep, just mentioned that b/c I don't think i'm referencing `content.js` with a ` – Behold Sep 26 '21 at 16:44
  • The only other reasons are a bug in the browser or you didn't reload the extension after adding the `storage` permission or you look at an old error in chrome://extensions page UI. – wOxxOm Sep 26 '21 at 16:52
  • thanks for the list! I went back to a git revision where the functionality was working correctly, then went back to the current revision where it wasn't (and updated and reloaded the extension each time), and now it works again. argh... – Behold Sep 26 '21 at 16:59
  • When you reload or update the extension your old content script's event listeners such as `paste` remain so when the listener is triggered it no longer belongs to the current extension, it's "orphaned" and it can't use chrome API. See [Chrome extension: How to remove orphaned script after chrom extension update](https://stackoverflow.com/a/57471345) – wOxxOm Sep 26 '21 at 17:07
  • Ah ok - thank you for the help! I haven't yet been able to reproduce it, but when I do I'll give that a try – Behold Sep 26 '21 at 18:48
  • When/if you do, please [edit] your question to update it. This comment chain is getting rather long. – General Grievance Oct 04 '21 at 12:44

0 Answers0