2011-03-29 64 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。

相關問題