每次绘制弹出曲线时添加一个元素
我想编写脚本,将一个元素添加到另一个元素,并在页面上出现特定ID。当用户徘徊在其他元素上时,它将添加到文档中。
就业务逻辑而言:当用户徘徊在用户名上时,出现流行音乐。我想在每个出现的弹出窗口中为孩子添加一个按钮。
我已经看过 this ,与下面的代码相似,它有效。问题是它仅检查一个元素,并且当它出现时,结束了其工作。我知道,要检查每个元素,我应该删除行this.disconnect();
。但是,这只会导致网页挂起。
我该怎么做才能使它起作用?我做错了吗?
// ==UserScript==
// @name ms_teams_integration
// @match https://example.com/*
// @version 1.0
// @grant none
// @run-at document-start
// @require https://greasyfork.org/scripts/12228/code/setMutationHandler.js
// ==/UserScript==
var teamsIcon = ""; // icon in svg format here
setMutationHandler(document, '#user-hover-email', function(nodes) {
this.disconnect();
nodes.forEach(function(n) {
var emailLink = n.getElementsByTagName("a").item(0);
var teamsLink = emailLink.cloneNode(true);
teamsLink.setAttribute(
"href",
teamsLink.getAttribute("href")
.replace("mailto:", "https://teams.microsoft.com/l/chat/0/0?users="));
teamsLink.setAttribute("target", "_blank");
teamsLink.innerHTML = teamsIcon;
n.appendChild(teamsLink);
});
});
I want to write script adding one element to other element with particular id when it appears on page. It is added to a document when user hovers over other element.
In terms of business logic: when user hovers over user name, a popver appears. I want to add a button to a child in each of appearing popovers.
I've looked on this and resembled this construction with the code below, and it works. The issue is it checks only for one element, and when it appears, ends its work. I understand, that in order to check for every element, I should remove line this.disconnect();
. That, however, only causes web page to hang.
What should I do to make it work? Am I doing something wrong?
// ==UserScript==
// @name ms_teams_integration
// @match https://example.com/*
// @version 1.0
// @grant none
// @run-at document-start
// @require https://greasyfork.org/scripts/12228/code/setMutationHandler.js
// ==/UserScript==
var teamsIcon = ""; // icon in svg format here
setMutationHandler(document, '#user-hover-email', function(nodes) {
this.disconnect();
nodes.forEach(function(n) {
var emailLink = n.getElementsByTagName("a").item(0);
var teamsLink = emailLink.cloneNode(true);
teamsLink.setAttribute(
"href",
teamsLink.getAttribute("href")
.replace("mailto:", "https://teams.microsoft.com/l/chat/0/0?users="));
teamsLink.setAttribute("target", "_blank");
teamsLink.innerHTML = teamsIcon;
n.appendChild(teamsLink);
});
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它之所以悬挂,是因为您的代码会导致新突变,该突变会一次又一次地检测到。为了防止它,请检查是否已经添加了新元素:
It hangs because your code causes a new mutation, which is detected again and again. To prevent it, check whether the new element is already added: