Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sending message to chrome extension from a web page

I want to send message from the console of the random web page to my chrome extension. chrome.extension.sendMessage doesn't seem to work.

like image 662
dilpreet023 Avatar asked Jul 11 '12 11:07

dilpreet023


People also ask

How do I send a message extension to a website?

According to the official docs you should use postMessage in the sender and message event listener in the receiver. var data = { type: "FROM_PAGE", text: "Hello from the webpage!" }; window. postMessage(data, "*"); Content script: (injected using chrome.

How do I communicate with chrome extensions?

Chrome has documentation on sending messages from webpages to chrome extensions. You add an “externally_connectable” object to your manifest. This will specify which webpages you want to allow to communicate with your extension (in this case, localhost, since I was developing on my machine).

How do I send a message to content script?

When sending a message to the content script, we need to specify which tab to send it to. Therefore, we need to retrieve the active tab information first, and then use tabs. sendMessage . To use the tabs API and to have access to the active tab, you need to add tabs and activeTab under permissions in your manifest.

Can Chrome extensions communicate with each other?

In addition to sending messages between different components in your extension, you can use the messaging API to communicate with other extensions.


2 Answers

According to the official docs you should use postMessage in the sender and message event listener in the receiver.

Here is an example:

Your website's page.html

var data = { type: "FROM_PAGE", text: "Hello from the webpage!" }; window.postMessage(data, "*"); 

Content script: (injected using chrome.tabs.executeScript(tabid, {code:...)

window.addEventListener("message", function(event) {     // We only accept messages from ourselves     if (event.source != window)         return;      if (event.data.type && (event.data.type == "FROM_PAGE")) {         console.log("Content script received message: " + event.data.text);     } }); 

Here page.html (which is not a part of the extension) posts messages to itself, which are intercepted and inspected by the content script. The reverse is possible through similar means.

To pass from content script to extension, you will have to use one of the available message-passing techniques.

It looks complicated and it is somewhat complicated but all this mumbo-jumbo is very secure.

like image 107
Silviu-Marian Avatar answered Oct 19 '22 07:10

Silviu-Marian


Here's a quote from the latest http://developer.chrome.com/extensions/messaging.html, It's much simpler to support this kind of feature now, here's how:

Sending messages from web pages

Similar to cross-extension messaging, your app or extension can receive and respond to messages from regular web pages. To use this feature, you must first specify in your manifest.json which web sites you want to communicate with. For example:

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

This will expose the messaging API to any page which matches the URL patterns you specify. The URL pattern must contain at least a second-level domain - that is, hostname patterns like "", ".com", ".co.uk", and ".appspot.com" and <all_urls> are prohibited. From the web page, use the runtime.sendMessage or runtime.connect APIs to send a message to a specific app or extension. For example:

// 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);   }); 

From your app or extension, you may listen to messages from web pages via the runtime.onMessageExternal or runtime.onConnectExternal APIs, similar to cross-extension messaging. Only the web page can initiate a connection. Here is an example:

chrome.runtime.onMessageExternal.addListener(   function(request, sender, sendResponse) {     if (sender.url == blacklistedWebsite)       return;  // don't allow this web page access     if (request.openUrlInEditor)       openUrl(request.openUrlInEditor);   }); 
like image 24
hewigovens Avatar answered Oct 19 '22 09:10

hewigovens