0

I am trying to build a very basic chrome extension. Essentially, what I want it to do is to search through a page for a bunch of names and append text (later an image) to that text. I came up with this code

// Array with names
String[] name = {
    "John", "Lisa", "Marge", "Barney", "Chuck", "Bobby"
};
//search for Names and add text
for (int i = 0; i < name.length; i++) {
    $('*:contains(name[i])').each(function() {
        if ($(this).children().length < 1)
            $(this).append('Found name');
    });
}

obviously, it doesnt work. I'm having difficulties debugging the extension and I'm not quite sure why it isn't working. Can anyone help?

Sushil
  • 2,837
  • 4
  • 21
  • 29
Johnny V
  • 9
  • 1

1 Answers1

1

So your array line is illegal:

String[] name = {"John", "Lisa", "Marge", "Barney", "Chuck", "Bobby"};

should be:

var name = ["John", "Lisa", "Marge", "Barney", "Chuck", "Bobby"];

My guess is you aren't seeing errors in your console because you are running this in a background script, which won't return errors to the normal console. Check out this great post on how to debug background script errors: Stackoverflow Background JS in Chrome Extension

Community
  • 1
  • 1
Brian
  • 1,513
  • 2
  • 14
  • 17
  • Not sure the logic, but there are a few more issues: 1) int should be var, and 2) $('*:contains(name[i])') should probably be $('*:contains(' + name[i]+ ')'). I am not sure if the contains logic is right though, that would return a lot of content. You'd have to clarify exactly what you are trying to return. – Brian Jun 30 '15 at 02:11
  • ok thanks for the help. I come from c# and java is not my thing but chrome extensions only do java – Johnny V Jun 30 '15 at 03:44
  • @JohnnyV [_JavaScript_, not Java](http://stackoverflow.com/questions/245062/whats-the-difference-between-javascript-and-java). This may be the source of some confusion. – Xan Jun 30 '15 at 09:20