Greasemonkey按钮单击处理程序不起作用? [英] Greasemonkey button click handler not working?

查看:110
本文介绍了Greasemonkey按钮单击处理程序不起作用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我有这个脚本,它将在页面( x是已登录用户页面上的链接)...

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)...

因此,我正在尝试使用Brock帮助我完成的弹出循环内容对于另一个脚本...将链接正确添加到'linksToOpen'数组中(或在添加按钮,侦听器和'openLinksInSequence'函数之前进行的操作)...一切似乎都很好并且我没有收到任何错误消息...但是我的按钮不起作用!

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?

推荐答案

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

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.

这篇关于Greasemonkey按钮单击处理程序不起作用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆