Access localStorage from a content script

3,208 views
Skip to first unread message

Jared

unread,
Mar 9, 2016, 10:18:07 AM3/9/16
to Chromium-Extensions-Announce
I have an extension in which I need to access the web page's localStorage. I have a content script and was under the impression that that could be done. I have been unable to accomplish this. Any help would be great.

Here is some of the code

Manifest.json

"name": "Promotify",
  "version": "1.0",
  "manifest_version": 2,
  "description": "Creates a user story from an issue using its description to create sub-tasks",
  "icons": {"20": "promotify_20.png",
            "128": "promotify_128.png"},
  "background": {
    "scripts": ["background.js"],
    "persistent": false
    
  },
  
  "content_scripts": [
    {   
      "matches": ["http://domain/project/*"],
      "js": ["user_story_template.js"]
    }   
  ],
  "page_action": {
    "default_title": "Promotify",
    "default_icon": "promotify_20.png",
    "default_popup": "popup.html" 
  },
  "permissions":["tabs",
                 "http://domain/project/*",
                 "declarativeContent",
                 "storage",
                 "webNavigation"
                 ]
 
Snippet of content script:
var token = ""
chrome.storage.local.get('token', function(result){
    token = result.token;
    });

Vikas Singh

unread,
Mar 10, 2016, 12:41:22 AM3/10/16
to Chromium-Extensions-Announce
store ur url like this...............................................

function webstorage(url)
{   
       if(typeof(Storage)!== 'undefined')
    {       
      if(localStorage.getItem("global_counter") == null)  
      {
        localStorage.setItem("global_counter", 7);
      }
      var global_value = Number(localStorage.getItem("global_counter"));
     
      if(localStorage.getItem("url:"+date.getDate()+":0") == null)
      {  
        localStorage.setItem("localname"+global_value,(date.getDate()*100)+(date.getMonth()+1));
        localStorage.setItem(":"+global_value+"_Counts",1);
        localStorage.setItem("url:"+date.getDate()+":0" ,url);
        global_value = global_value - 1;
        localStorage.setItem("global_counter",global_value);        
      }
      else
      { var counts = Number(localStorage.getItem(":"+(global_value+1)+"_Counts"));
        localStorage.setItem("url:"+date.getDate()+":"+counts,url);
        localStorage.setItem(":"+(global_value+1)+"_Counts", counts+1);
      }

      if(localStorage.getItem("global_counter") == 0)
      { localStorage.setItem("global_counter",1);
        removeContent();  
        }
    }
    else
    {
      document.getElementById("xyz").innerHTML = "Sorry your browser does not support HTML5 web storage";
    }
}

and fetchdata from url like this............................................

function fetchHistory()
  
  var globalCount = 7-Number(localStorage.getItem("global_counter"));
  var months = ['january','february','march','april','may','june','july','august','september','october','november','december'];
   for(m=0;m<globalCount;m++)
  { 
      var datestamp = Number(localStorage.getItem("localname:"+(7-m)));
    var Cdate =  parseInt(datestamp/100); 
    var Cmonth = datestamp - (Cdate*100); 
    var count = Number(localStorage.getItem(":"+(7-m)+"_Counts"));
    for(j=1;j<13;j++)
    {
      if(Cmonth == j)
        { var month = months[j-1]; }
    }
     for(k=0;k<count;k++)
    {
      var his = localStorage.getItem('url:'+Cdate+":"+k);
if(his!=null && his.indexOf('https://www.youtube.com/watch?v=') == 0){
var equalPos = his.indexOf("=");
     
}
}
    document.getElementById('xyx).innerHTML = data;
}

Jared

unread,
Mar 18, 2016, 3:17:31 PM3/18/16
to Chromium-Extensions-Announce
I have tried to use HTML5 webstorage without success. The content script doesn't seem to have access to the site's localStorage.

Antony Sargent

unread,
Mar 22, 2016, 12:29:41 PM3/22/16
to Jared, Chromium-Extensions-Announce
See the documentation on isolated worlds: https://developer.chrome.com/extensions/content_scripts#execution-environment

The typical workaround if you need to access something in the context of the page itself is to inject a <script> tag into the DOM, possibly using something like postMessage to communicate back to your content script. 


--
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/6a286008-d034-4b3b-a0ac-0aac2ed8ac8e%40chromium.org.

Reply all
Reply to author
Forward
0 new messages