开发者

Greasemonkey Undefined JS Errors

开发者 https://www.devze.com 2023-03-20 13:11 出处:网络
I am getting undefined errors on most of the lines in this greasemonkey script.I\'ve tested on Chrome and Firefox, with the same results on both.I can\'t quite figure out why.I\'m somewhat new to Java

I am getting undefined errors on most of the lines in this greasemonkey script. I've tested on Chrome and Firefox, with the same results on both. I can't quite figure out why. I'm somewhat new to JavaScript but I feel that this is pretty basic and I'm just missing something.

After looking at some related pages, I'm not sure that this Greasemonkey related. It's very likely that it's a generic JavaScript problem. Well here's the script in its entirety.

// ==UserScript==
// @name AutoHotkey Forum Line Numberer
// @namespace http://apps.aboutscript.com/gm/
// @description Add line numbers to AutoHotkey forum code boxes
// @ico开发者_如何学Pythonn http://apps.aboutscript.com/gm/linenumbers/autohotkey.png
// @include http://www.autohotkey.com/forum/viewtopic.php*
// @include http://www.autohotkey.com/forum/posting.php
// @version 0.5
// ==/UserScript==

var linenumbers = {

    count_lines: function(text) {
        var lines = text.split('\n');
        return (lines.length);
    },

    make_div: function(max) {
        var contents = "";
        for (i=1; i<=max; i++) {
            contents += '<span style="margin:0 auto 0 auto; ';
            if (i%2) {
                contents += 'color:#FFAAAA; '
            } else {
                contents += 'color:#FFCCCC; '
            }
            contents += '">' + i + '</span><br>';
        }
        if (max <= 15) {
            contents = '<div style="min-width:25px; height:200px; overflow-    y:auto;">' + contents + '</div>';
        } else {
            contents = '<div>' + contents + '</div>';
        }
        return contents;
    },

    add_numbers: function() {
        var code_tables = document.getElementsByClassName('code');
        for (i in code_tables) {
            var td = code_tables[i];
            var div = td.firstChild;
            var codetext = div.innerText;
            var total_count = this.count_lines(codetext);
            var to_inject = make_div(total_count);
            var newtd = document.createElement('td');
            newtd.innerHTML = to_inject;
            document.body.insertBefore(newtd, td);
        }
    }
};

linenumbers.add_numbers();

So my questions are: What is the problem? How can I fix it? How can I prevent it in the future?

Thanks,

Frankie (parse)

By the way, I think the 'greasemonkey' tag applies and I'm surprised it doesn't exist. If you have 1500+ rep, please add it.


var to_inject = make_div(total_count); You forgot this. var to_inject = this.make_div(total_count);

And you also didn't declare i :

for (i in code_tables) { => for (var i in code_tables) {

for (i=1; i<=max; i++) { => for (var i=1; i<=max; i++) {

so when add_numbers will call make_div, it'll fait because they will both loop on the same i variable.

0

精彩评论

暂无评论...
验证码 换一张
取 消