Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome storage API not working with content script

I'm using chrome storage API to save data that needs to be accessed on content script page. When I load data in extension settings page like this

document.write("chrome.storage.sync.get(\"password\", function(items) {\n");
document.write("var true_password = items.password;\n");
document.write("});\n");

it works, but the same code doesn't work with content script...

I'm getting this error

Uncaught TypeError: Cannot read property 'sync' of undefined

Any idea why?


EDIT

manifest.json
{
"name":"app",
"description":"app...",
"version":"0.1",
"manifest_version":2,
"options_page": "options.html",
"permissions": [ "storage" ],

"content_scripts": [
    {
      "matches": ["https://www.my_page.com/*"],
      "js": ["lock.js"]
    }
  ]
}
like image 452
opengl Avatar asked Mar 25 '26 14:03

opengl


1 Answers

Anything you write directly to the page's DOM, be it with document.write or inserting a <script> element, is no longer considered to be a content script.

Instead, it executes outside of the "isolated world" in the page's own context. That means it has no access to content script APIs or variables/functions defined in your content script.

You can still communicate with page-level code, though.

like image 183
Xan Avatar answered Mar 28 '26 04:03

Xan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!