1

I am new to JavaScript and chrome extensions. I am writing an extension that needs to get a value out of the local storage of the web page to which it is a page action. Here is a snippet of manifest.

manifest.json:

{
    "content_scripts": [
        {
            "matches": ["http://url/*"],
            "js": ["user_story_template.js"]
        }
    ],
    "page_action": {
        "default_title": "Promotify",
        "default_icon": "promotify_20.png",
        "default_popup": "popup.html"
    },
    "permissions": [
        "tabs",
        "http://url/*",
        "declarativeContent",
        "storage"
    ]
}

Here is a snippet of the content script:

function get_token(){
  //alert("Starting processing...");
  var auth_token = ""; 
  chrome.storage.local.get('token', function(result){
  auth_token = result.token;
}); 

I have looked for answers and a lot of them say that this way should work. I should be able to do this from the content script. However, am not getting any value. I have tried using HTML also to no avail. When I go into the inspector on the page and look under resources there is a value for token in local storage.Can anyone see what I might be doing wrong? I am sure that I am missing something.

Some of the places I have looked for the answer:

Easy way to get Local Storage Value to Content Script [Chrome Extension]

Access chrome local storage used in both content script and popup?

Community
  • 1
  • 1
Jared
  • 31
  • 1
  • 5
  • 1
    Are you trying to access the inspected page's local storage or the background page? You have to inject a script via a content script (or use the `executeScript` method) in order to access the inspected page's `localStorage` api. – Daniel Lizik Feb 29 '16 at 18:36
  • 1
    `chrome.storage.local` is local storage *for the extension*, it's entirely separate from the website's own `localStorage`. As Daniel says, you'll need to actually run some code in the website's execution context to access its `localStorage`, which may need to send a message to pass that data to a different part of the extension. – Jeremy Feb 29 '16 at 18:38
  • @JeremyBanks Mind making that into an answer? – Xan Mar 01 '16 at 01:05
  • @Xan Naw, I don't have the time now to flesh it out into a good-enough answer for me to post. Somebody else can have at it. – Jeremy Mar 01 '16 at 01:10

1 Answers1

1

chrome.storage will only get your extension's storage, to get the storage belonging to the current page, you need to use Window.localStorage in content scripts

var token = localStorage.getItem("token");
Haibara Ai
  • 10,703
  • 2
  • 31
  • 47
  • Thank you for the response. I have tried using Window.storage in the content script but that doesn't work either. – Jared Mar 03 '16 at 13:53
  • @Jared, what if you call `localStorage.getItem("token")` directly in F12 tool? – Haibara Ai Mar 04 '16 at 00:52
  • On the webpage, I can call window.localStorage.getItem("token") in the chrome inspector it can access the token. maybe my content script isn't being injected right? – Jared Mar 04 '16 at 19:11
  • @Jared, yes, then I would suggest you check if the content script be injected. You can just open F12 tool -> Sources -> Content scripts and find if there exists your content scripts. – Haibara Ai Mar 05 '16 at 03:33
  • I checked the inspector and looks like my content script is not showing up on the content-script tab. – Jared Mar 07 '16 at 13:42
  • @Jared, that means your content script is not injected into web page. According to your question post, did your `matches` part in `manifest.json` exactly look like `"http://url/*`? You should know that only matches some url which contains `"url"`. What about using `` instead? – Haibara Ai Mar 08 '16 at 00:42
  • Thank you for your responses. no, that was just put in because I didn't want to put one of my companies internal URLs. I did get the script to finally start showing up as a content script in the F12 tool of the webpage. I added a webNavigation.onHistoryStateUpdated listener to the background script. However, I still can't get the token. – Jared Mar 08 '16 at 14:11