Greasemonkey button click handler not working?

Deadly 提交于 2019-12-24 21:58:56

问题


So, I have this script that will grab the links off of the table on THIS page (the 'x' are links on a logged in users page)...

So I'm trying to use a popup loop thing that Brock helped me with for another script... the links get added properly into the 'linksToOpen' array (or did before I added the button, listener and 'openLinksInSequence' function)... everything seems to be fine and I get no error messages... but my button DOESN'T work!

// ==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);

So, why isn't my button working?


回答1:


You're setting the id of your div to the same as your button which is invalid. And your trying to add the event listener to an undefined object. DOM elements don't show up automatically as JS variables. You'd have to var checkButton = document.getElementById("checkButton"); before you set the event listener.



来源:https://stackoverflow.com/questions/19999634/greasemonkey-button-click-handler-not-working

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!