在谷歌搜索结果中添加文本
append text in google search result
我试图使用自定义chrome扩展在谷歌搜索结果中附加文本。但是我得到这个错误:
Uncaught RangeError: Maximum call stack size exceeded.
这是我的manifest.json
"content_scripts": [
{
"matches":["<all_urls>"],
"js" : ["jquery.min.js", "contentscript.js"],
"run_at": "document_end"
}
]
contentscript.js:
function changeResultText(event) {
$('h3.r').append('<b>bbbbbbbbbbbbbbbbbbbbbb</b>');
}
document.addEventListener('DOMNodeInserted', changeResultText);
如何解决这个问题?
这是一个工作示例,您可以在每个搜索结果的顶部设置自定义文本,图像。
manifest.json:
{
...
"content_scripts": [
{
"js" : ["jquery.min.js", "contentscript.js"],
"css": ["contentscript.css"],
"matches": [ "'u003Call_urls>" ],
"run_at": "document_end",
"all_frames": false
}
]
...
}
contentscript.js:
console.log("Custom Script Injected...");
// MutationObserver configuration data: Listen for "childList" mutations in the specified element and its descendants
var config = {
childList: true,
subtree: true
};
var regex = /<a.*?>[^<]*<'/a>/;
// Traverse 'rootNode' and its descendants and modify '<a>' tags
function modifyLinks(rootNode) {
var nodes = [rootNode];
var replace = '';
while (nodes.length > 0) {
var node = nodes.shift();
if (node.tagName == "A" && node.parentElement.parentElement.className == 'rc') {
replace = "<div class='"store-logo'">'r'n <img src='any-logo.png' width='30' height='20' > </div>";
replace += "<div class='"cashback-text'">Any Title</div>'r'n";
replace += node.innerHTML;
node.innerHTML = replace;
} else {
// If the current node has children, queue them for further processing, ignoring any '<script>' tags.
[].slice.call(node.children).forEach(function(childNode) {
if (childNode.tagName != "SCRIPT") {
nodes.push(childNode);
}
});
}
}
}
// Observer1: Looks for 'div.search'
var observer1 = new MutationObserver(function(mutations) {
// For each MutationRecord in 'mutations'...
mutations.some(function(mutation) {
// ...if nodes have beed added...
if (mutation.addedNodes && (mutation.addedNodes.length > 0)) {
// ...look for 'div#search'
var node = mutation.target.querySelector("div#search");
if (node) {
// 'div#search' found; stop observer 1 and start observer 2
observer1.disconnect();
observer2.observe(node, config);
if (regex.test(node.innerHTML)) {
// Modify any '<a>' elements already in the current node
modifyLinks(node);
}
return true;
}
}
});
});
// Observer2: Listens for '<a>' elements insertion
var observer2 = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.addedNodes) {
[].slice.call(mutation.addedNodes).forEach(function(node) {
// If 'node' or any of its desctants are '<a>'...
if (regex.test(node.outerHTML)) {
// ...do something with them
modifyLinks(node);
}
});
}
});
});
/* Start observing 'body' for 'div#search' */
observer1.observe(document.body, config);
//document.addEventListener('DOMNodeInserted', observer1.observe(document.body, config));
contentscript.css:
.store-logo{
float: left;
margin-right: 10px;
height: 19px;
}
.cashback-text{
color: #f43632 !important;
}
相关文章:
- 在highcharts.js中向点弹出窗口动态添加文本
- 向d3圆添加文本
- 如何在已经有图标的滚动到顶部按钮中添加文本
- 如何在背景图像上添加文本
- 如何使用javascript在asp.net网页表单上动态添加文本框
- 如何从DIV数组向DIV添加文本
- 我可以通过javascript在鼠标光标旁边动态添加文本吗?
- 如何使用 jQuery 在文本区域中的每一行添加文本
- 单击向输入添加文本值的链接
- 使用.html()添加文本输入时出现意外行为
- 在d3.js中添加文本标签以强制定向图链接
- 当我尝试添加文本效果时,有东西阻止了JavaScript的运行'手风琴'在html文档中
- 在每个条形图的中间添加文本标签&鼠标悬停事件
- 通过在 JavaScript 中使用正文标签的 ID 向
标签添加文本
- JavaScript在段落中的页面加载后添加文本
- eventListner,用于在另一个文本框获得焦点时添加文本框(vanilla-js)
- 如何根据复选框在输入中添加文本
- Bootstrap 3通过点击按钮添加文本并显示在新选项卡中
- 用户动态添加文本框时,文本框中的echo值
- jqueryTe正在添加文本