我在代码中添加了10个函数,我甚至都没有调用它们,但代码停止工作!

Del*_*ens 1 javascript xul firefox-addon

单独,这段代码有效:

CustomButton = {

1: function () {
  alert("Just testing") 
  },

}
Run Code Online (Sandbox Code Playgroud)

我添加下面的代码,上面的代码停止工作:

function getvisitingnow() {
    return document.location;
}
function getcontents(uri) {
    var req = new XMLHttpRequest();
    req.open('GET', uri, true);
    req.onreadystatechange = function (aEvt) {
        if (req.readyState == 4) {
            if(req.status == 200) {
                return req.responseText;
            }
        }
    };
    req.send();
}
function regexforsitefound(uri, searchcontents) {
    var re = new RegExp("\\<div class=g\\>.*?(?:\\<a href=\\\"?(.*?)\\\"?\\>.*?){2}\\</div\\>", "mi");
    var sitefound = searchcontents.match(re);
    if (sitefound[0]) return sitefound[0] else return null;
}
function regexforcategoryfound(uri, searchcontents) {
    var re = new RegExp("\\<div class=g\\>.*?(?:\\<a href=\\\"?(.*?)\\\"?\\>.*?){2}\\</div\\>", "mi");
    var categoryfound = searchcontents.match(re);
    if (categoryfound[1]) return categoryfound[1] else return null;
}
function regexfordomainname(uri) {
    var re = new RegExp("http://(?:[A-Za-z0-9-]+\\.)?[A-Za-z0-9-]+\\.[A-Za-z0-9-]+/?", "si");
    var domainname = uri.match(re);
    if (domainname) return domainname;
}
function regexforparentdir(uri) {
    var re = new RegExp("http://(?:[A-Za-z0-9-]+\\.)?[A-Za-z0-9-]+\\.[A-Za-z0-9-]+/?", "si");
    var parentdir = uri.match(re);
    if (parentdir) return parentdir;
}
function getcomparisonlink(visitingnow) {
    var searchuri = null;
    var searchcontents = null;
    var uri = visitingnow;
    while(true) {
        searchuri = 'http://www.google.com.br/search?';
        searchuri += 'q='+ uri +'&btnG=Search+Directory&hl=en&cat=gwd%2FTop';
        searchcontents = getcontents(searchuri);
        var sitefound = regexforsitefound(searchcontents);
        if (sitefound) {
            var categoryfound = regexforcategoryfound(searchcontents);
            if (categoryfound) {
                return categoryfound;
            }
        } else {
            var domainname = regexfordomainname(uri);
            if (!domainname) {
                var parentdir = regexforparentdir(uri);
                uri = parentdir;
            } else {
                return null;
            }
        }
    }
}
function clickedlink(event){
    var visitingnow = getvisitingnow(); 
    if (visitingnow) {
        getcomparisonlink(visitingnow);
        if (comparisonlink) {
            tab.open(comparisonlink);
        };
    }
}
function createBookmarkItem() {
    const XUL_NS = "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul";
    var item = document.createElementNS(XUL_NS, "toolbarbutton");
    item.setAttribute("id", "Testing-Doit-Button2");
    item.setAttribute("class", "bookmark-item pagerank");
    item.setAttribute("tooltiptext", "Do it!");
    item.setAttribute("oncommand", "testing_doit();");
    return item;
}
function placeBookmarkItem() {
    var toolbar = document.getElementById("PersonalToolbar");
    var button = createBookmarkItem();
    toolbar.appendChild(button);
}
Run Code Online (Sandbox Code Playgroud)

为什么?

Pau*_*aul 9

尝试逐个添加您的功能.看看你的代码停止工作的功能.然后清空功能内容,一次只能将它放回去.再次检查代码停止工作的位置.关于应该有一个语法错误.

但正如Bobby建议的那样,更简单的方法是尝试使用Firefox Errorlog,或者使用Firebug.


Osc*_*hed 5

一个小的JavaScript错误可能会破坏很多东西.你忘了在两个地方添加分号.

在sitefound [0]之后需要有一个分号:

function regexforsitefound(uri, searchcontents) {
    var re = new RegExp("\\<div class=g\\>.*?(?:\\<a href=\\\"?(.*?)\\\"?\\>.*?    ){2}\\</div\\>", "mi");
    var sitefound = searchcontents.match(re);
    if (sitefound[0]) return sitefound[0] else return null; 
}
Run Code Online (Sandbox Code Playgroud)

这里有一个类别[1]之后:

function regexforcategoryfound(uri, searchcontents) {
    var re = new RegExp("\\<div class=g\\>.*?(?:\\<a href=\\\"?(.*?)\\\"?\\>.*?){2}\\</div\\>", "mi");
    var categoryfound = searchcontents.match(re);
    if (categoryfound[1]) return categoryfound[1] else return null;
}
Run Code Online (Sandbox Code Playgroud)