0

我在后台脚本中有一些代码。我希望能够发射类似的东西:

$(document).trigger('someEvent', [args]);

然后在弹出窗口中用这样的东西听它:

var backgroundPage = chrome.extension.getBackgroundPage();
$(backgroundPage.document).bind('someEvent', function (args) {...});

但这不起作用。触发代码被触发,但显然我没有绑定到正确的东西。帮助?

4

1 回答 1

2

我真的不知道 JQuery 所以我在那里没有帮助,但我想我指出它似乎确实以 JS 方式工作......

清单.json

{
  "name": "TEST - Background to PopUp Event Triggering",
  "version": "1.0",
  "permissions": [
    "tabs", "http://*/*", "https://*/*"
  ],
  "browser_action": {
      "default_title": "TEST - Background to PopUp Event Triggering",
      "default_icon": "icon.png",
      "default_popup": "popup.html"
  },
   "background": {
    "scripts": ["background.js"]
  },
  "manifest_version" : 2
}

popup.html

<!doctype html>
<html>
  <head>
    <script src="popup.js"></script>
  </head>
  <body>
  </body>
</html>

popup.js

var backgroundPage = chrome.extension.getBackgroundPage();

backgroundPage.document.addEventListener('myCustomEvent', function(e) {
    document.write(e.args + '\n');
})

背景.js

var customEvent = document.createEvent('Event');
customEvent.initEvent('myCustomEvent', true, true);

setInterval(function() {
    customEvent.args=["bunyips","smell"];
    document.dispatchEvent(customEvent);
}, 3000);
于 2012-07-27T20:42:09.913 回答