.prepend() 到动态页面上带有 pageMod 和 jQuery 的所有链接
.prepend() to all links with pageMod and jQuery on dynamic pages
我正在尝试使用 pageMod .prepend()
每个 google 搜索结果 link 的图像。它适用于第一个结果页面,但在加载其他页面或编辑搜索词时却不行。那是因为我的脚本是在页面末尾加载的,动态内容更改不会再次加载我的脚本。我该如何解决这个问题?
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 + "'> ");
})
您应该在托管搜索结果的父元素上使用 MutationObserver
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);
}
我正在尝试使用 pageMod .prepend()
每个 google 搜索结果 link 的图像。它适用于第一个结果页面,但在加载其他页面或编辑搜索词时却不行。那是因为我的脚本是在页面末尾加载的,动态内容更改不会再次加载我的脚本。我该如何解决这个问题?
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 + "'> ");
})
您应该在托管搜索结果的父元素上使用 MutationObserver
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);
}