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.