sendMessage从后台脚本到app中的内容脚本失败

时间:2013-06-29 02:26:32

标签: google-chrome google-chrome-extension google-chrome-devtools google-chrome-app

更新

据我所知,使用“sendMessage”函数从后台脚本向内容脚本发送消息是不可能。但是有一个可怕的解决方法,

在内容脚本的window.onload中,向后台脚本发送消息:

chrome.runtime.sendMessage( { action: "messaging", window: "app" }, this.listenForFutureMessages );

同样在内容脚本中,具有以下功能:

listenForFutureMessages: function(someAction)
{
    //Take some action based on the message

    //If we want the background script to be able to contact
    //us again, we need to give them another callback. This
    //is because Chrome only allows one use per callback
    chrome.runtime.sendMessage( { action: "messaging", window: "app" }, this.listenForFutureMessages );
},

在后台脚本中,让侦听器执行以下操作:

chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse)
    {
        if ( request.action === "messaging" )
        {
            //Save the callback for later
            this.listeners[ request.window ] = sendResponse;

            //Tell chrome we will be using the callback later
            return true;
        }
    }
);

当您的后台脚本想要向内容脚本发送消息时,只需将其称为:

this.listeners[ "app" ]( { someProperty: "some value" } );

这是一种愚蠢的方法,但这实际上是可行的。希望这可以帮助其他需要此功能的人。

ORIGINAL 我无法从后台脚本向内容脚本发送消息。当我尝试找到标签ID时,它告诉我即使我的应用具有该权限,我也没有权限。当我从内容脚本收到消息并打印出sender对象时,它会显示tab.id = -1。将消息发送到内容脚本的API需要标签ID!

  

chrome.tabs.sendMessage(整数tabId ,任何消息,函数responseCallback)

错误:

  

chrome.tabs不可用:您无权访问此API。确保manifest.json中包含所需的权限或清单属性。

     

'undefined'的事件处理程序出错:无法调用未定义的方法'sendMessage'TypeError:无法调用undefined方法'sendMessage'       在chrome-extension://panoaieakcofaegcjfbmhndaekfgpijh/scripts/background.js:109:16       在Event.dispatchToListener(event_bindings:356:21)       在Event.dispatch_(event_bindings:342:27)       在Event.dispatch(event_bindings:362:17)       在miscellaneous_bindings:167:33       在Event.dispatchToListener(event_bindings:356:21)       在Event.dispatch_(event_bindings:342:27)       在Event.dispatch(event_bindings:362:17)       at Object.chromeHidden.Port.dispatchOnMessage(miscellaneous_bindings:253:22)

那么如何联系我的内容脚本? (我有多个窗口,需要能够单独联系他们)

我的清单:

{
    "manifest_version": 2,
    "name": "App",
    "description": "App",
    "version": "0.75",
    "minimum_chrome_version": "27",
    "offline_enabled": true,
    "icons": 
    {
        "16": "images/icon16.png",
        "48": "images/icon48.png",
        "128": "images/icon128.png"
    },
    "app": 
    {
        "background": 
        {
            "scripts": 
            [
                "scripts/background.js"
            ]
        }
    },
    "permissions": 
    [
        "unlimitedStorage",
        "fullscreen",
                {
            "fileSystem": 
            [
                "write"
            ]
        },
        "background",
        "<all_urls>",
        "tabs"
    ],
    "update_url": "http://192.168.1.121/app.xml"
}

1 个答案:

答案 0 :(得分:1)

没有这样的东西叫做#34;内容脚本&#34;在Chrome app。您的清单文件看起来像Chrome扩展程序的混合。打开chrome://extensions/,启用开发者模式,您会看到警告&#34; background&#34;和&#34;标签&#34;权限对于Chrome应用无效。

如果您要实施Chrome应用,请使用chrome.runtime.sendMessagechrome.runtime.onMessage。这些消息可以从事件页面和主页面发送和发送。例如:

// event page (aka background page)
chrome.app.runtime.onLaunched.addListener(function() { 
    chrome.app.window.create('main.html');
});

    // Later, when you want to notify the app window
    chrome.runtime.sendMessage(" ... any message ... ");
<!-- main.html -->
<script src="main.js"></script>
// main.js
chrome.runtime.onMessage.addListener(function(message, sender, sendResponse) {
    // Do something with the message
});