Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

132
Views
Adding an element each time a popover is drawn

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);
    });
});

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

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:

    nodes.forEach(function(n) {
        if (n.__ms_teams_integration) return;
        var emailLink = n.getElementsByTagName("a").item(0);
        // ...............
        // ...............
        n.appendChild(teamsLink);
        n.__ms_teams_integration = true;
    });
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!