views:

144

answers:

5

Alone, this code works:

CustomButton = {

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

}

I add the code below and the code above stops working:

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);
}

Why?

+8  A: 

try adding your functions one by one. see at which function your code stops working. then empty the function contents only to put it back with pieces at a time. check again where your code stops working. about there should be a syntax error.

But as Bobby suggests, the easier way is to try Firefox Errorlog, or maybe Firebug.

Paul
I am surprised this got so many up-votes. Seems monotonous and annoying, and there are so many easier ways to find a syntax error!
Josh Stodola
+5  A: 

One little JavaScript-error can break a lot of things. You have forgotten to add semicolons in two places.

There needs to be a semicolon after sitefound[0] here:

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; 
}

and one after categoryfound[1] here:

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;
}
Oscar Kilhed
A: 

The comma after the function in CustomButton can break code the code in IE. Also, if you are using CustomButton the first time here, you should introduce it with var. I know these are not the issues you asked for, but otherwise, everything seems correct.

Tom Bartel
+4  A: 
if (sitefound[0]) return sitefound[0] else return null;

This syntax is invalid.

Try:

if (sitefound[0])
    return sitefound[0];
else 
    return null;
Ben
curly-braces are not required, but their use (in the original) might have made it clearer that the syntax was wrong. I often skip the curly-braces if I have one condition; but I one I use an else, I throw in the curlies.
Michael Paulukonis
With curly braces, this could've been condensed to one line
Josh Stodola
With the use of proper syntax, all JS code can be condensed to one line. It's not the most maintainable in that format....
Michael Paulukonis
+1  A: 

If you are a Mac user, open (a recent version) of Safari and hit

⌥⌘ + i,

which opens up a great panel with lots of charts and data about the client-server interaction. You can also see and locate javascript errors, or debug javascript in a console directly. neat.

For Firefox, try the excellent firebug to see, what went wrong where .. in their own words: [with Firebug] .. you can edit, debug, and monitor CSS, HTML, and JavaScript live in any web page.

The MYYN