我在将 JavaScript 数组传递给我在Add-on Builder中编写的插件时遇到问题。
为了进行通信,我使用事件并使用数组发送事件,但附加组件(内容脚本)获取的是对象,而不是数组。
这是事件:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xml:lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>
<script type="application/x-javascript">
$(function() {
$(window).bind('Runner-PageEvent', function(event) {
console.log('PAGE: Reakcja na Runner-PageEvent na stronie');
});
$(window).bind('RunnerResult', function(event) {
console.log('PAGE: Result is ' + event.originalEvent.detail.Result);
//// PROBLEM!!!
console.log('PAGE: Should be array: ' + event.originalEvent.detail.array); // firebug shows object
});
$(window).bind('Runner-DetectCallback', function(event) {
console.log('PAGE: Reakcja na Runner-DetectCallback na stronie');
$('#browser-detection').text('Extension detected').css('background-color', 'green').css('color', 'white');
});
var event = new CustomEvent("Runner-Detect", {});
window.dispatchEvent(event);
console.log('PAGE: Runner-Detect sent');
});
function CallExtension() {
var event = new CustomEvent("Runner-PageEvent", { detail : {
a: "messageA",
b: "messageB",
c: "messageC",
d: "messageD",
arrayA: ["a", "b", "c", "d"],
arrayB: [0, "info", "info2", 3]
}});
window.dispatchEvent(event);
console.log('PAGE: CALL EXTENSION clicked');
}
</script>
</head>
<body>
<div id="browser-detection" style="background-color: red">No extension</div>
<br/>
Run extension: <button onclick="CallExtension()">Run!</button>
</body>
</html>
Firebug 将事件显示为具有一个属性的对象detail.tab
作为具有四个项目的数组。
内容脚本接收一个对象e
,其中e.detail.tab
是一个对象(但应该是一个数组)。
window.addEventListener(
'eventname',
function(e) {
// console.log(e.detail.tab.length); -> produce an error on console (Ctrl+Shift+J)
// console.log(e.detail.tab[0]); -> as above
for(var x in e.detail.tab){
console.log(x);
console.log(e.detail.tab[x]);
}
self.port.emit('SendToExtension', e.detail);
}
);
我不知道是 Add-on Builder 有问题还是我做错了什么?
请帮忙!