Greasemonkey按钮单击处理程序不起作用?

时间:2013-11-15 11:05:56

标签: javascript greasemonkey addeventlistener

所以,我有这个脚本会抓取THIS页面上的表格链接('x'是登录用户页面上的链接)...

所以我正在尝试使用Brock帮助我使用另一个脚本的弹出循环...链接被正确添加到'linksToOpen'数组中(或者在我添加按钮,监听器和'openLinksInSequence'之前做了功能)......一切似乎都很好,我没有收到任何错误消息......但我的按钮不起作用!

// ==UserScript==
// @name        Unicreatures Accoplishment Checker
// @namespace   http://trueidiocy.us
// @description Marks off completed accomplishments
// @include     http://unicreatures.com/accomplishments.php
// @include     http://www.unicreatures.com/accomplishments.php
// @include     http://unicreatures.com/accomplishments.php?
// @include     http://www.unicreatures.com/accomplishments.php?
// @version     1
// @grant       GM_addStyle
// ==/UserScript==

var mytable = document.getElementById('right').getElementsByTagName('table')[4];
var links=mytable.getElementsByTagName('a');
var i;
var linksToOpen     = [];
var mywin2          = null;


var zNode       = document.createElement ('div');
zNode.innerHTML = '<button id="checkButton" type="button">'
            + 'Check Accomplishments</button>'
            ;

zNode.setAttribute ('id', 'checkButton');

mytable.parentNode.insertBefore(zNode, mytable);



function checkAccomplishments (zEvent) {



for(i=0;i < links.length;i++) {
  if (links[i].href.indexOf('family') > -1) {


    linksToOpen.push (links[i].href);

    links[i].innerHTML="*";

}
}
alert(linksToOpen)

openLinksInSequence ();
};

function openLinksInSequence () {
    if (mywin2) {
        mywin2.close ();
        mywin2      = null;
    }

   if (linksToOpen.length) {
        var link    = linksToOpen.shift ();
        mywin2      = window.open (link, "my_win2");

        mywin2.addEventListener ('load', openLinksInSequence, false);
    }
}


checkButton.addEventListener ("click", checkAccomplishments, true);

那么,为什么我的按钮不起作用?

1 个答案:

答案 0 :(得分:2)

您将div的ID设置为与无效的按钮相同。并且您尝试将事件侦听器添加到未定义的对象。 DOM元素不会自动显示为JS变量。在设置事件监听器之前,您必须var checkButton = document.getElementById("checkButton");