Reputation: 321
Hi I need to know how to track session of my website from google chrome extension.My website has been added as an extension in google chrome.When the extension icon is clicked it navigates to the home page of my website.It is a login page.So I need to know whether a user has been logged or not.I hope this can be done only using sessions.But I don't know how to track session variable from chrome extension.Please help me.
Upvotes: 1
Views: 2886
Reputation: 48211
One solution is to communicate the login status from your web-page to your extension (as exlained here).
From your webpage you have to send messages to the extension to inform it about the user's login status.
Once the user successfully logs in, make sure you let the extension know:
chrome.runtime.sendMessage(<your_extension_id>, { status: "logged in" });
Once you detect that the users session has ended (either expired or due to manually logging out), make sure you let the extension know:
chrome.runtime.sendMessage(<your_extension_id>, { status: "logged out" });
From your extension listen for messages from the webpage and update accordingly.
Extension source code:
background.js:
var url = "<the_url_of_your_webpage_that_sends_messages>";
/* Listen for external messages (messages from web-pages) */
chrome.runtime.onMessageExternal.addListener(function(msg, sender) {
if (sender.url == url) {
/* OK, this page is allowed to communicate with me */
if (msg.status === "logged in") {
/* Cool, the user is logged in */
alert("Logged in !");
} else if (msg.status === "logged out") {
/* How sad, the user is leaving */
alert("Logged out !");
}
}
});
manifest.json:
{
"manifest_version": 2,
"name": "Test Extension",
"version": "0.0",
"background": {
"persistent": false,
"scripts": ["background.js"]
},
"externally_connectable": {
"matches": ["<the_url_of_your_webpage_that_sends_messages>"]
}
}
Upvotes: 4