help chrome.storage.local.get

472 views
Skip to first unread message

Thien Hokhac

unread,
Jun 23, 2016, 6:07:36 AM6/23/16
to Chromium-Extensions-Announce
file manifest.json

{
  "manifest_version": 2,
  "name": "gg",
  "description": "jj",
  "version": "1.0",
  "browser_action": {
    "default_icon": "images/icon.png"
    },
     "background": {
        "persistent": false,
        "scripts": ["js/jquery.min.js","js/popup.js"]
        },
  "icons": {
    "64": "images/icon_64.png"
  },
  "web_accessible_resources": [
  "styles/popup.css", "styles/jquery-ui.css", "js/jquery-ui.js", "js/main-pixel.js"
  ],
  "permissions": [
  "downloads",
  "activeTab",
  "tabs",
  "storage",
  ],
  "content_scripts": [{
  "js": ["js/jquery.min.js", "js/contentscript.js"],
  "matches": [
  "http://*/*",
  "https://*/*",
  "*://*/*"
  ],
  "css": ["styles/style.css"],
  "run_at": "document_end",
  "all_frames": true
  }],
  "content_security_policy": "script-src 'self' https://www.google-analytics.com 'unsafe-eval'; object-src 'self'; style-src * 'unsafe-inline';"
}



file contentscript.js


chrome.storage.onChanged.addListener(function(changes, namespace) {
for (key in changes) {
var storageChange = changes[key];
}
});


chrome.storage.local.get("data2", function(items) {
if (!chrome.runtime.error) {
console.log(items.data2);
}
});


file popup.js

var code = [

  'var mainWrap = document.createElement("input");',
  'mainWrap.id = "value"',
  'document.body.appendChild(mainWrap);',

  'var react = document.createElement("script");',
  'react.src = chrome.extension.getURL("main.js");',
  'react.setAttribute("type", "text/javascript");',
  'document.body.appendChild(react);'

].join("\n")


chrome.browserAction.onClicked.addListener( function(tab){
  if (tab) {
    chrome.tabs.executeScript(tab.id, {
      code: code 
    });
  }
});



});



file main.js 

//error here
$('#value').val(items.data2);  // underfile


please help 

Antony Sargent

unread,
Jun 23, 2016, 9:43:21 AM6/23/16
to Thien Hokhac, Chromium-Extensions-Announce
The chrome.storage functions are asynchronous, so it looks like you have 2 problems:

-You seem to be trying to access "items" before the callback to chrome.storage.local.get has run
-The "items" parameter to the storage.local.get callback is only in scope during the lifetime of the callback, so even if your code in main.js ran after the callback fired, items would no longer be a valid reference to it



--
You received this message because you are subscribed to the Google Groups "Chromium-Extensions-Announce" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To post to this group, send email to chromium-...@chromium.org.
Visit this group at https://groups.google.com/a/chromium.org/group/chromium-extensions/.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/633d1528-c509-4174-ac67-0e8c60d59236%40chromium.org.
For more options, visit https://groups.google.com/a/chromium.org/d/optout.

Reply all
Reply to author
Forward
0 new messages