0

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!

pangpp
  • 155
  • 3
  • 12
  • As shown in the [documentation](https://developer.chrome.com/extensions/messaging#external-webpage) you need to declare externally_connectable in your manifest. If you already did that, edit the question accordingly. Also make sure you've clicked the reload icon in chrome://extensions page, and reloaded the web page. See also [this](https://stackoverflow.com/questions/10257301/where-to-read-console-messages-from-background-js-in-a-chrome-extension). – wOxxOm May 29 '19 at 05:44
  • @wOxxOm thanks for sharing the last link, turned out I checked the wrong console all the time, the message was sent from the website to the extension. – pangpp May 29 '19 at 15:28

0 Answers0