2011-03-29 71 views
1

我有我的background.html到sendRequest将到contentscript以下...消息传递Background.html - >内容脚本

chrome.tabs.getSelected(null,function(tab) 
    { 
     chrome.tabs.sendRequest(tab.id,{req:"func"}); 
    }); 

但是,它似乎并不奏效。我究竟做错了什么?

部分相关的相关清单文件的...

"background_page": "background.html", 
"browser_action": 
{ 
    "default_icon": "icon.png", 
    "popup": "popup.html" 
}, 
"permissions": [ 
    "tabs", 
    "http://*/*", 
    "https://*/*", 
    "notifications", 
    "contextMenus" 
], 
"options_page": "options.html", 
"content_scripts": [ 
    { 
     "matches": ["http://*/*","https://*/*"], 
     "js": ["contentScript.js","jquery.js"], 
     "all_frames": false 
    } 
], 

contentscript ...

chrome.extension.onRequest.addListener(
function(request, sender, sendResponse) 
{ 
switch(request.req) 
{ 
     case "func": 
     func(); 
     sendResponse({}); 
     break; 
default: 
     sendResponse({}); 
} 
}); 
+0

请在内容脚本中显示您的清单和请求侦听器。 – serg 2011-03-29 15:56:02

+0

您是否已将您的Background.html中的代码放在

0

你的代码运行对我罚款。当您发送请求时,可能不会在该页面上加载内容脚本。你在chrome.tabs.getSelected之前是否有某种状况?你不能马上运行它,你需要确保一个标签首先被加载。

PS。同样在你的清单中,你可能会想要在你的内容脚本之前加载jquery。

相关问题