我的用例是我想运行一个可以收听任何页面打开的脚本。然后从该页面中提取一些信息并将其传递到后台页面。我分别使用了page-mod和page-worker。但是消息不会传递给我的background.html中的页面脚本
/*******************main.js******************/
var pageMod = require("page-mod");
var pageWorker = require("page-worker").Page({
contentScriptFile: require("self").data.url("background_contentscript.js"),
contentURL: require("self").data.url("background_local.html"),
});
pageMod.PageMod({
include: ["*"],
contentScriptFile: require("self").data.url("turn.js"),
onAttach: function(worker) {
worker.port.on("turn-event", function(message) {
console.log("Inside Main : PageMode message = " + message);
pageWorker.port.emit("event-from-main", message);
});
}
});
/***************** turn.js*************/
self.port.emit("turn-event", JSON.stringify(document.title));
/**************background_contentscript.js*************/
self.port.on("event-from-main", function(message) {
console.log("Inside background_local.js message = " + message);
var event = document.createEvent('CustomEvent');
event.initCustomEvent("event-from-cs", true, true, JSON.stringify(message));
document.documentElement.dispatchEvent(event);
});
/*********************background_local.html******************/
<!DOCTYPE html>
<html>
<head>
<title>background_local</title>
<script>
document.documentElement.addEventListener("event-from-cs", function(event) {
console.log("event detail message: " + event.detail);
}, false);
</script>
</head>
<body>
<p>Hello</p>
</body>
</html>
消息确实到达background_contentscript.js,但从未到达background_local.html。我在这里做错了什么,或者看起来像SDK的错误?