2015-11-19 69 views
0

我正在嘗試.prepend()將圖像傳遞到與pageMod的每個Google搜索結果鏈接。它適用於第一個結果頁面,但在加載其他頁面或編輯搜索條件時不起作用。這是因爲我的腳本是在頁面的末尾加載的,動態內容更改不會再次加載我的腳本。我該如何解決這個問題?.prepend()在頁面動態頁面上使用pageMod和jQuery的所有鏈接

index.js

var self = require("sdk/self"); 
var pageMod = require("sdk/page-mod"); 

pageMod.PageMod({ 
    include: /^https?:\/\/.*\.google\..+/, 
    contentStyleFile: self.data.url("style.css"), 
    contentScriptFile: [self.data.url("jquery.min.js"), self.data.url("script.js")] 
}); 

的script.js

$('.r a').each(function(index) { 
    var main = "http://****.***"; 
    var url = $(this).attr('href'); 

    $(this).prepend("<img width='16px' height='16px' src='" + main + url + "'> "); 
}) 

回答

1

你應該承載搜索結果

var Col = document.getElementById('center_col'); 
if(Col) { 
    var observer = new MutationObserver(function(mutations) { 
    mutations.forEach(function(mutation) { 
     // perform prepend image 
     }      
    });  
    });     

    var cfg = { attributes: true, attributeFilter : ['style'], childList: false };      
    observer.observe(Col, cfg); 
} 
+0

測試了這一點,在父元素上使用MutationObserver儘快地!現在非常感謝你! –

+0

非常感謝! –

相關問題