Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

174
Visualizações
How to show an element again after removing it from the DOM?

I am trying to make a script that will hide chat and show it again after pressing the H button but I don’t know how I need to make the <div> elements from the website show again after I remove it. Here’s my code:

// ==UserScript==
// @name         Cytos Additions
// @namespace    http://tampermonkey.net/
// @match        https://cytos.io/
// @grant        none
// ==/UserScript==

var isToggled = false;

function doc_keyUp(e) {
  switch (e.keyCode) {
    case 72: // h
      if (isToggled) {
      
      }
      else {
        document.getElementsByClassName('_1FlWeEtrJKCnvzX-Y50CLP')[0].remove()
      }
      
      var isToggled = true;
      
      break;
    default:
      break;
  }
}

document.addEventListener('keyup', doc_keyUp, false);

I don’t know what to put under the if(isToggled) statement to make the chat reappear again.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The opposite of removal is insertion; there are various ways to insert an element. Since your element is at index 0, prepending it to its original parent element is what you need here.

If you just remove an element without retaining its reference, it’s going to be garbage-collected and you cannot use it anymore. So store a reference to the element; also, store one to its parent, since that’s the target you need to prepend the element to.

const chatElement = document.getElementsByClassName('_1FlWeEtrJKCnvzX-Y50CLP')[0],
  chatParent = chatElement.parentElement;
let isToggled = false;

function doc_keyUp(e) {
  switch (e.code) {
    case KeyH:
      if (isToggled) {
        chatParent.prepend(chatElement);
      }
      else {
        chatElement.remove();
      }

      isToggled = !isToggled;

      break;
    default:
      break;
  }
}

document.addEventListener("keyup", doc_keyUp, false);

Toggling the variable isToggled can be done with isToggled = !isToggled;, but do not redeclare the variable inside the function, or else it will have no relation to your outer isToggled variable.

Note that keyCode is deprecated. You should use code instead.

There are two more concerns:

  1. If Cytos depends on the chat existing, then removing the element may cause the app to break. Similarly, removing and reinserting elements may cause unforeseen events to be triggered. Consider using case KeyH: chatElement.toggleAttribute("hidden"); break; instead. See the documentation on toggleAttribute and on the hidden property.
  2. The class name _1FlWeEtrJKCnvzX-Y50CLP doesn’t look like a stable class name, but rather like a minified, randomized name. It’s very likely to change and your user script is very likely to break. Familiarize yourself with the DOM API, in particular with document.querySelector, and consider finding a more stable selector.
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda