I'm using jwt token for user authentication on my website, so once a user logs in, I store the token in localStorage, and I want to have the token on my chrome extension, such that the user is also authenticated while using the extension.
I've tried passing the token using chrome.runtime.sendMessage
on the react side and chrome.runtime.onMessageExternal.addListener
on the chrome side to connect them but that didn't work.
In the manifest.json I have specified the extension to communicate with my website
"externally_connectable": {
"matches": ["http://localhost:3000/"]
}
// example from https://developer.chrome.com/extensions/messaging
// The ID of the extension we want to talk to.
var editorExtensionId = "abcdefghijklmnoabcdefhijklmnoabc";
// Make a simple request:
chrome.runtime.sendMessage(editorExtensionId, {openUrlInEditor: url},
function(response) {
if (!response.success)
handleError(url);
});
chrome.runtime.onMessageExternal.addListener(
function(request, sender, sendResponse) {
console.log('I'm here');
if (sender.url == blocklistedWebsite)
return; // don't allow this web page access
if (request.openUrlInEditor)
console.log(request.openUrlInEditor);
});
I think the message is not sent because I didn't see anything in the console, and I get an error in the chrome extension saying "Uncaught Error: extension.sendRequest, extension.onRequest, and extension.onRequestExternal are deprecated. Please use runtime.sendMessage, runtime.onMessage, and runtime.onMessageExternal instead." But I'm already using the suggested ones.
I've also tried to use the chrome.storage.sync.get
method to access the localStorage from the extension but couldn't get it to work.
Please help, thanks in advance!