3

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"]
    }
  ]
}
opengl
  • 141
  • 1
  • 5
  • 15
  • Check http://stackoverflow.com/questions/17998123/chrome-storage-sync-undefined – Xan Mar 30 '15 at 23:56
  • I have `"permissions": [ "storage" ]` in my manifest – opengl Mar 30 '15 at 23:59
  • Is the code actually in your `lock.js`, or are you trying it from the console? – Xan Mar 31 '15 at 00:00
  • And you reloaded both the extension _and the page you're working with_ after adding the permission? – Xan Mar 31 '15 at 00:01
  • I added permission 1h ago – opengl Mar 31 '15 at 00:02
  • So? Did you reload the extension, then the page to refresh the script? Also, double-check your code for typos. This would appear if you misspelled `storage`. – Xan Mar 31 '15 at 00:03
  • Yes, I reloaded the extension, and there are no typos because the same code works in setting.html/.js – opengl Mar 31 '15 at 00:04
  • Then I'm out of ideas. This should be working normally. – Xan Mar 31 '15 at 00:06
  • 1
    Does it matter that I'm writing the code with `document.write("chrome.storage.sync.get(\"password\", function(items) {\n");` in lock.js – opengl Mar 31 '15 at 00:08
  • 1
    _Of course_, since that code leaves the content script context and executes in the page context, which does not have access to the API. Why are you doing this? – Xan Mar 31 '15 at 00:09
  • Then we found the problem, tnx! :) – opengl Mar 31 '15 at 00:10
  • Maybe, but that makes your question unhelpful. You should edit to include that relevant information. – Xan Mar 31 '15 at 00:11
  • There's still the question of why are you using `document.write`. It shouldn't be necessary. – Teepeemm Apr 01 '15 at 03:35
  • Because I need to write new html page when user visits xy.com page, and to write html with js I use `document.write` – opengl Apr 01 '15 at 18:01

1 Answers1

3

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.

Community
  • 1
  • 1
Xan
  • 74,770
  • 16
  • 179
  • 206
  • 2
    Maybe this is a new update "Your extension's content scripts can directly access user data without the need for a background page.": https://developer.chrome.com/extensions/storage – alayli Jun 24 '15 at 02:15
  • @alayli No. What is meant is accessing extension data (options, etc.) without messaging. – Xan Jun 24 '15 at 07:38