I am working on one of the bookmark manager website's chrome extension. Once user signed in the web application we are saving authentication token in local storage. How do I access same authentication token in the chrome extension to auto sign into the application, Once user click's on extension's icon
Asked
Active
Viewed 2,392 times
3 Answers
2
First, set an event listener to the browserAction.onClicked event which accesses local storage and gets the token. You can then pass your token to your login function. The content of the event listener will work in other files, but the event listener itself will not (it needs to be running in the background to listen for the event).
background.js
chrome.browserAction.onClicked.addListener(function () {
// event listener
chrome.storage.local.get("token", function(items) {
let token = items["token"]
login(token)
})
})
function login(token) {
// login code here
}
Ben Botvinick
- 2,837
- 3
- 16
- 41
-
Thanks for replay. I tried this one unable to get web app local storage token in the content script. – myfavs.in Jul 13 '18 at 05:06
-
Did you make sure to put it in your permissions in the manifest? – Ben Botvinick Jul 13 '18 at 05:09
-
Yes. "permissions": [ "contextMenus", "storage", "notifications", "tabs", "*://*/*", "
" ], – myfavs.in Jul 13 '18 at 05:11 -
What is the error? The reason is likely that you're searching for the wrong key in the items object returned from the storage request – Ben Botvinick Jul 13 '18 at 05:11
1
you can use messages to send the token.
content script:
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.method == "getLoginToken") {
sendResponse({token: localStorage["token"]});
}
});
and when you want to access the token
chrome.runtime.sendMessage({method: "getLoginToken"}, function(response) {
if (response && response.token) {
console.log(response.token);
}
});
Jujhar Singh
- 373
- 5
- 16
-
Thanks for quick response. With this, I can able to access extension's local storage. But I need to access the website(defined in manifest.json(homepage_url)) local storage, – myfavs.in Jul 13 '18 at 05:02
-
`Content scripts are files that run in the context of web pages.` but this does exactly that @myfavs.in – Jujhar Singh Jul 13 '18 at 08:43
0
chrome.storage.local.get(['key'], function(result) {
console.log('Value currently is ' + result.key);
});
you want to pass the token property name to this function.
function getLocalStorage(key, callback){
chrome.storage.local.get(key, function(value){
callback(value);
});
}
getLocalStorage('YourStorageName', function(data){
//process
})
Dananjaya Ariyasena
- 606
- 3
- 15
-
@Jujhar Why do you store values for each tab? for a process like a login we have to have one token value. so no need to store several tokens under tabs. – Dananjaya Ariyasena Jul 13 '18 at 04:36
-
Thanks for replay. I tried this one unable to get web app local storage token in the content script. – myfavs.in Jul 13 '18 at 05:04
-
Content scripts run in the context of webpages, not extension pages. Therefore, if you're accessing localStorage from your contentscript, it will be the storage from that webpage, not the extension page storage. check this link https://stackoverflow.com/questions/3937000/chrome-extension-accessing-localstorage-in-content-script – Dananjaya Ariyasena Jul 13 '18 at 05:37