2012-02-07 75 views
4

我試圖做一個小擴展,檢查維基百科文章的谷歌搜索結果,並在之後添加一些額外的鏈接。但是在解析搜索結果時遇到一些麻煩。目前它非常簡單。Chrome擴展程序解析谷歌搜索結果

清單:

{ 
"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 
} 

和噴油器:

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

findWikipediaLinks(); 

的問題似乎是,這裏的代碼運行顯示實際搜索結果之前。 (記錄的結果是在谷歌的標題欄的一個的。有沒有辦法來這個時間propperly?

+0

你在DOM準備好的時候試過運行你的代碼嗎?在jquery中你可以這樣做:'$(function(){/ * code here * /});' – MMM 2012-02-07 17:19:06

+0

@MMM是的 - 仍然給出相同的結果:( – Tim 2012-02-07 17:21:02

+0

就像使用Google即搜即得結果,內容使用AJAX加載。您可能需要攔截該事件。如何循環運行,每隔幾秒鐘? – MMM 2012-02-07 17:23:50

回答

4

谷歌是通過AJAX加載的結果,所以你需要使用一個event listenerDOMNodeInserted事件。

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

target.addEventListener('DOMNodeInserted', filterResultInserts); 

filterResultInserts之內,你將不得不尋找匹配結果並修改它們的類或id。

+0

啊完美。乾杯。 – Tim 2012-02-07 17:30:26

+0

@Tim試圖實現類似的東西,得到了適合你的最終代碼? – pqn 2013-06-25 04:29:29

+0

@PremN。這是不久前,但我記得能夠使用js/jquery來解析每個插入的dom節點後的結果,就像上面一樣。可悲的是,我還沒有代碼。抱歉。 – Tim 2013-06-25 21:07:33