opengl
opengl

Reputation: 141

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"]
    }
  ]
}

Upvotes: 3

Views: 4068

Answers (1)

Xan
Xan

Reputation: 77523

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.

Upvotes: 3

Related Questions