2012-02-10 76 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

相关问题