hu7sy
hu7sy

Reputation: 961

Want to access local storage of Chrome extension

My manfiest.json is this.

    {
  "manifest_version": 2,

  "name": "GTmetrix Analyzer Plugin",
  "description": "This extension will analyze a page using GTmetrix",
  "version": "2.1",
  "options_page": "options.html",
  "browser_action": {
   "default_icon": "icon.png",
   "default_popup": "popup.html"
  },
  "background": {
    "scripts": ["background.js"],
    "persistent": false
  },
  "permissions": ["tabs", "storage"], 
  "content_scripts": [
      {
          "matches": ["http://ticketchest.geekschicagolabs.com/admin/orderHistory"],
          "css": ["mystyles.css"],
          "js": ["jqueryui-min.js", "content-script.js"]
      }
  ]
}

and my popup.html is

    <!doctype html>
<html>
  <head>
    <title>Ticket chest</title>
    <script src="popup.js"></script>
  </head>
  <body>
    <h1>Ticket chest extension</h1>
    <a href="options.html">Settings</a>
    <!-- <button id="checkPage">Check this page now!</button> -->
  </body>
</html>

and background.js is

    chrome.runtime.onMessage.addListener(function(response, sender, sendResponse){
 localStorage['privateKeyy'] = response;
});

Now I want what user enter in text area it will be also save in local storage of web with local storage of extenison, it is saved in extenstion local storage but not in web page.

and that's content script code

    window.onload = function() {

  document.getElementById('save').onclick = function(){
   var privateKey = document.getElementById("textarea").value;

   chrome.storage.sync.set({'privateKey':privateKey}, function(){
    //document.getElementById('privateKey').value = privateKey;
    //chrome.browserAction.setBadgeBackgroundColor({color:[200, 0, 0, 100]});
    //chrome.tabs.sendMessage(privateKey);
    chrome.storage.local.set({'privateKeyy':privateKey});
    localStorage.setItem("lastname", "Smith");
    alert('save successfully');
   });
   //syncStorage["privateKey"] = privateKey;
  }

  document.getElementById('erase').onclick = function(){
   chrome.storage.sync.remove('privateKey', function(){
    alert('erase successfully');
   });
  }

  document.getElementById('get').onclick = function(){
   chrome.storage.sync.get('privateKey', function(data){
    alert(data.privateKey);
    console.log(data.privateKey);
   });
  }

}

Upvotes: 1

Views: 6058

Answers (1)

jengeb
jengeb

Reputation: 403

If you like to store data in the web page's local storage move your code to a content script (instead of executing the code (only) in the extension's background script). See also here: https://stackoverflow.com/a/23082216/4419582

Upvotes: 3

Related Questions