Le_Coeur
Le_Coeur

Reputation: 1541

Call chrome extension API from GWT

I need to send message from GWT frontend to chrome extension. For this purpose I use JSNI in GWT and call the following method:

static native void sendMessageToChromeExtension() /*-{
    chrome.runtime.sendMessage(...);
}-*/;

However, this does not work and I'm getting:

Cannot read property 'sendMessage' of undefined

Is there any other way to do this?

Upvotes: 0

Views: 227

Answers (2)

Le_Coeur
Le_Coeur

Reputation: 1541

I found a problem I need to call chrome from $wnd (window) object. The following works:

static native void sendMessageToChromeExtension() /*-{
    $wnd.chrome.runtime.sendMessage(...);
}-*/;

Upvotes: 1

Xan
Xan

Reputation: 77523

Webpages cannot normally call chrome APIs (with the exception of inline install).

To achieve what you wish to do, you need to follow the procedure outlined here. Suppose your frontend lives at http://example.com/, then you need to declare in the extension's manifest that you expect messages from this domain:

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

Only then will chrome.runtime.sendMessage be exposed to the webpage. Note that you'll also need to provide the extension's id.

Upvotes: 1

Related Questions