【问题标题】:Call chrome extension API from GWT从 GWT 调用 chrome 扩展 API
【发布时间】:2014-05-21 01:31:36
【问题描述】:

我需要从 GWT 前端向 chrome 扩展发送消息。为此,我在 GWT 中使用 JSNI 并调用以下方法:

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

但是,这不起作用,我得到:

Cannot read property 'sendMessage' of undefined

还有其他方法吗?

【问题讨论】:

    标签: java google-chrome gwt google-chrome-extension


    【解决方案1】:

    网页无法正常调用 chrome API(inline install 除外)。

    要实现您想要做的,您需要遵循here 概述的过程。 假设您的前端位于http://example.com/,那么您需要在扩展的清单中声明您期望来自该域的消息:

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

    只有这样chrome.runtime.sendMessage 才会暴露在网页中。请注意,您还需要提供扩展程序的 ID。

    【讨论】:

    • 我已经在externally_connectable中添加了网页,直接在开发控制台调用就可以了
    • 澄清一下,你从哪里得到这条消息?
    • 在开发控制台中,但如果我通过 GWT 触发它。
    • @Le_Coeur 如果从网页的控制台本身,你可以打电话吗?如果没有,说明设置有问题,如果有,说明 JSNI 方面有问题。
    • @Le_Coeur 很高兴它成功了;我将保留我的答案,因为它是相关的,但是当你能够这样做时,你的答案显然需要被标记。
    【解决方案2】:

    我发现了一个问题,我需要从 $wnd(窗口)对象调用 chrome。以下作品:

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

    【讨论】:

      猜你喜欢
      • 2016-06-12
      • 1970-01-01
      • 2023-03-20
      • 2013-12-08
      • 1970-01-01
      • 1970-01-01
      • 2011-09-27
      • 2016-09-13
      • 2023-03-10
      相关资源
      最近更新 更多