是否可以使用消息传递 api 将请求发送到将要收听它的同一页面?它似乎不起作用!
一种解决方案是在其他页面上设置侦听器,然后将请求重定向回父页面。但这是一个黑客,我真的不想这样做:(
编辑(更新)
背景.html (0)
chrome.extension.sendRequest({action:'foo'}, function(response) {
//do stuff
});
index.html (1)
chrome.extension.sendRequest({action:'foo'}, function(response) {
//do stuff
});
背景.html (2)
chrome.extension.onRequest.addListener(function(request,sender,sendResponse) {
if(request.action=='foo') //do stuff
});
...
...
//the code form (0)
从上面(1)有效,但(0)无效:(
编辑 2
从 (2)
chrome.extension.onRequest.addListener(function(request,sender,sendResponse) {
switch(request.action) {
case 'foo': //do some stuff here
break;
}
});
上面的开关已经变得相当长了..我猜大约有 30 个条目..加上除了戳事件监听器之外没有办法调用开关。(或者我错过了什么?)
我想case 'foo':
在同一个文件中执行它下面的代码,因此我尝试在(0)中调用它。