0

私のユースケースは、任意のページを開くことをリッスンできるスクリプトを実行したいということです。次に、そのページから情報を抽出して、バックグラウンド ページに渡します。これには、それぞれ 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 のバグのように見えますか?

4

1 に答える 1

0

「スクリプトの信頼できるページのコンテンツ」セクションをお読みください。

于 2012-12-20T20:26:23.783 に答える