简体   繁体   English

window.addEventListener未在点击事件上执行

[英]window.addEventListener not being executed on click events

This is a very basic Firefox addon , it opens a new tab and loads the specified html file. 这是一个非常基本的Firefox插件,它将打开一个新tab并加载指定的html文件。
It attachs a ContentScript to the tab but for some reason alert('in event listener'); 它将ContentScripttab但是由于某种原因alert('in event listener'); is never executed? 永远不会执行?
Although alert('in script'); 尽管alert('in script'); does run. 确实运行。
Whys that? 为什么?

main.js

var tabs = require("sdk/tabs");

tabs.open({
  url: require('sdk/self').data.url('html/view.html'),
  isPinned: true,
  onOpen: function onOpen(tab) {

    console.log("onOpen");
    tab.attach({
      contentScriptFile: require('sdk/self').data.url('js/controller.js')
    });
  }
});

controller.js

window.addEventListener('click', function(event) {

  alert('in event listener');

}, false);

alert('in script');
alert(document.getElementById("text").value);

// output
//   console.error: MyApp: 
//   Message: TypeError: document.getElementById(...) is null

view.html

<html>
    <body>
        <textarea id="text">yo</textarea>
    </body>
</html>

moving to solution. 转向解决方案。

Change onOpen to onReady and instead of document.addEventListener use window.addEventListener . onOpen更改为onReady ,而不是document.addEventListener使用window.addEventListener bobbyrne01 reports that it works, no clue why this fixed it though. bobbyrne01报告说它可以工作,但是不知道为什么它可以解决它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM