4

I'm trying to make a little extension that checks the google search results for wikipedia articles, and adds a little extra link afterwards. But am having a little trouble parsing the search results. Its very simple at present.

Manifest:

{
"name": "Test",
"version": "0.1",
"description": "Test Test",
"icons":{
    "128":"icon_128.png"
    },
"permissions": [
    "tabs",
    "http://www.google.com/*",
    "https://www.google.com/*"
],
"content_scripts": [
    {
      "matches": ["http://www.google.com/*", "https://www.google.com/*"],
      "css": ["style.css"],
      "js": ["jquery-1.7.min.js", "injector.js"],
      "run_at": "document_end"
    }
],
"manifest_version": 2
}

And injector:

function findWikipediaLinks(){
console.log("here I am!");
console.log($('a'));
//.css({'background-color': 'yellow'});
}

findWikipediaLinks();

The problem seems to be that the code here runs before the actual search results are shown. (the results logged are the a's in the google header bar. Is there a way to time this propperly?

Tim
  • 5,732
  • 2
  • 27
  • 35
  • Have you tried running your code when the DOM is ready? In jquery you can do it this way: `$(function() {/*code here*/});` – MMM Feb 07 '12 at 17:19
  • @MMM Yeah - still gives the same result :( – Tim Feb 07 '12 at 17:21
  • Just a suggestion, with Google Instant results the content is loaded using AJAX. You probably need to intercept that event. How about a loop that runs every couple seconds? – MMM Feb 07 '12 at 17:23
  • Please provide the URL(s) you are testing with. – cheeken Feb 07 '12 at 17:25

1 Answers1

4

Google is loading results via AJAX so you need to use an event listener for DOMNodeInserted events.

function filterResultInserts(event) {
  console.log(event);
}

target.addEventListener('DOMNodeInserted', filterResultInserts);

Within filterResultInserts you will have to look for classes or ids that match results and modify them.

abraham
  • 46,583
  • 10
  • 100
  • 152
  • @Tim trying to implement something similar, got the final code that worked for you? – pqn Jun 25 '13 at 04:29
  • @PremN. It was a while ago, but I remember being able to use js/jquery to parse the results after each inserted dom node, like above. Sadly though, I don't still have the code. Sorry. – Tim Jun 25 '13 at 21:07