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

非常感谢! –