2012-02-10 19 views
0

我在下面的代碼中將內容腳本中的數據傳遞給background.html,但由於某種原因它不適用於我..這裏是代碼..如何在Chrome擴展中的contentcript和background.html之間傳遞數據

Contentscript.js

var a1 ="Shan"; 
    chrome.extension.sendRequest({method:"text",txt:a1}, function(response) { 
     d=response.data; 
     alert(d); 
    }); 

background.html

if(request.method == "text") 
    { 
     sendResponse({data:request.key}); 
    } 
    else 
    { 
    sendResponse({data:request.key}); 
    }  

我的問題是,爲什麼我不能夠通過變量 「A1」 到background.html?是否無法完成?

+0

你是否按照文檔? http://code.google.com/chrome/extensions/messaging.html您發佈的代碼似乎並不完整。如果這是你所有的,我建議再看看文檔。 – 2012-02-10 17:22:35

回答

1

因爲密鑰被命名爲txt而不是key

chrome.extension.sendRequest({method:"text",txt:a1} 
              ^^^ Your definition: txt 

sendResponse({data:request.key}); 
          ^^^ Should be txt as well 

警告:我已經驗證您無法回收sendResponse方法。在發射sendResponse後,分機將不會對未來的sendResponse呼叫做出響應。
因此,每個chrome.extension.sendRequest只有一個sendResponse

+0

我已經用鑰匙進行過測試,但它不起作用。爲什麼? – Shan 2012-02-10 17:32:13

+0

@Shan你必須保持一致。使用'txt'或'key'。更新代碼後,重新加載您的擴展以查看效果。 – 2012-02-10 17:36:22

+0

問題是變量a1沒有傳遞給background.html ..但如果你給引號中的東西「工作」,它會傳遞給background.html .. – Shan 2012-02-10 17:58:33

相關問題