Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using externally_connectable to Send Data from WWW to Chrome Extension

I'm looking to send data from a website to the LocalStorage of a chrome extension.

I've tried the below answer which is what I'm looking for, however it appears to not be functioning at all on Chrome 29 stable. Could anyone advise as to why this may be/tweaks?


1 Answers

It's not possible to manage extension's localstorage from a website because of security rules.

But there is a method that allows you to send message from a website to an extension.

Consequently, you may send your data to extension with this method and get this data on extension's background page then do whatever you want with it (saving to exension's localstorage).

It's really well explained in DOCs,

1. In your manifest.json, you will let your website to allow sending message.

"externally_connectable": {
  "matches": ["*://*.example.com/*"]
}

2. chrome.runtime.sendMessage will be available on your website. (javascript)

chrome.runtime.sendMessage("your extension id will be here", 
                           {data: { anyDataKey : "example"}});

3. In your background.js create a message listener for external messages.

chrome.runtime.onMessageExternal.addListener(
  function(request, sender, sendResponse) {
    if (request.data)
      alert("Hi, there is message from the website");
      var data = request.data;
      // now the data is on your extension side, just save it to extension's localstorage.
  });
like image 85
Okan Kocyigit Avatar answered Oct 19 '25 15:10

Okan Kocyigit