I have a webpage and a Chrome extension built for that webpage.
Once the user successfully login into my website. I trigger this:
localStorage.app_isLoggedIn = true;
localStorage.app_loggedInTimeStamp = new Date();
(I verify this : saves successfully)
Chrome Extension Details
manifest file:
{
"manifest_version" : 2 ,
"name" : "app",
"description" : "Welcome to app",
"version" : "1.0",
"browser_action" : {
"default_icon" : "app.png",
"default_popup" : "popup.html"
},
"background": {
"page": "background.html"
},
"permissions" : [
"storage",
"background",
"tabs"
],
"web_accessible_resources" : [
"*.html",
"*.js"
]
}
============
authenticate.js - this is the first script file on my page(popup.html)
// create to authenticate weather app has been logged in recently or not
// IF NOT LOGGED IN: the user should be redirected to app web page
// IF LOGGED IN: the plugin would proceed
var _MIN_PER_DAY = 1000 * 60;
// a and b are javascript Date objects
function dateDiffInHours(a, b) {
// Discard the time and time-zone information.
var utc1 = Date.UTC(a.getFullYear(), a.getMonth(), a.getDate());
var utc2 = Date.UTC(b.getFullYear(), b.getMonth(), b.getDate());
return Math.floor((utc2 - utc1) / _MIN_PER_DAY);
}
// MECHANISM USED : HTML 5 Local storage
var maxDaysToGoWithoutLogin = 5; // default : has to be configurable
var maxHoursToGoWithoutLogin = maxDaysToGoWithoutLogin * 24; // default : has to be configurable
var st = localStorage.app_isLoggedIn;
var timeStamp = localStorage.app_loggedInTimeStamp;
console.log("status" + st + " ---- timestamp " + timeStamp);
if (st != undefined && st != "undefined" && st != null && st != 'null' && timeStamp != undefined && timeStamp != "undefined" && timeStamp != null && timeStamp != "null") {
console.log("status and stamps correct");
// the user is logged in
// now check for time stamp
var localStorageTimeStampForLastLogin = new Date(localStorage.app_loggedInTimeStamp);
var diff = dateDiffInHours(localStorageTimeStampForLastLogin, new Date());
alert(diff);
alert(maxHoursToGoWithoutLogin);
if (diff > maxHoursToGoWithoutLogin) {
// EXPIRED : requires relogin
chrome.tabs.create({ url: "https://app.in" });
} else {
// success : the plugin would proceed
}
} else {
chrome.tabs.create({ url: "https://app.in" });
}
After this, I click on my Chrome extension.
The behavior I want
The Chrome plugin should check the HTML 5 local storage variables that I saved when I logged into my application.
If the user is not logged in to my app: then the user should be redirected to my web application.
If the user is logged in to my app: the plugin should pop out and the plugin would now show my app data.
The Problem
The Chrome extension is not able to access the local storage that was set by my application. (the above console messages output)
statusundefined ---- timestamp undefined
When I check the local storage of the plugin popup window, I get this in my chrome dev tools.
localStorage
Storage {length: 0}
Please See: I don't think this link is my use case Chrome extension: accessing local storage in content script