Kat Cox
Kat Cox

Reputation: 3571

Greasemonkey button click handler not working?

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?

Upvotes: 0

Views: 1172

Answers (1)

mafafu
mafafu

Reputation: 1266

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.

Upvotes: 2

Related Questions