0

UPDATE在应用程序的sendMessage从后台脚本内容脚本失败

从我可以告诉,这是不可能从后台脚本发送消息到使用“的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 我无法从my background脚本发送消息到内容脚本。当我尝试查找标签ID时,它告诉我即使我的应用程序具有该权限,我也没有权限。当我收到来自内容脚本的消息并打印出sender对象时,它会显示tab.id = -1。将消息发送到内容脚本的API需要标签ID!

chrome.tabs.sendMessage(integer tabId, any message, function responseCallback)

错误:

chrome.tabs is not available: You do not have permission to access this API. Ensure that the required permission or manifest property is included in your manifest.json.

Error in event handler for 'undefined': Cannot call method 'sendMessage' of undefined TypeError: Cannot call method 'sendMessage' of undefined at chrome-extension://panoaieakcofaegcjfbmhndaekfgpijh/scripts/background.js:109:16 at Event.dispatchToListener (event_bindings:356:21) at Event.dispatch_ (event_bindings:342:27) at Event.dispatch (event_bindings:362:17) at miscellaneous_bindings:167:33 at Event.dispatchToListener (event_bindings:356:21) at Event.dispatch_ (event_bindings:342:27) at 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" 
} 
+0

不要把你的答案的问题里面。如果您有答案,请将其作为答案发布。 – kiamlaluno

+0

@kiamlaluno我不认为它是一个答案。这是一种解决方法。 –

回答

1

有没有这样的Chrome app称为“内容脚本”的事情。您的清单文件看起来像是一个Chrome扩展的混合体。打开chrome://extensions/,启用开发者模式,并且您会看到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 
}); 
+0

这对我不起作用(这正是我试图做的)。 'main.js'永远不会收到消息。我可能使用了错误的术语,但我的“主页”从来没有收到过这个消息。 –

+0

@DonRhummy然后显示你的*真实*清单和代码,而不是其他东西,似乎并不代表你的代码。 –

+0

这是我的清单。你会注意到它没有声明任何内容脚本。即使删除背景和标签权限也不会使这项工作。 –

相关问题