Advertisement
valdirdd

JS tool

Feb 20th, 2025
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function keepElement(selector, fatherSelector, elementToAdd) {
  2.     setInterval(() => {
  3.         const element = document.querySelector(selector);
  4.         if (!element) {
  5.             const father = document.querySelector(fatherSelector);
  6.             if (!father) return;
  7.             father.insertBefore(elementToAdd, father.firstChild);
  8.         }
  9.     }, 100)
  10. }
  11. function htmlToNode(html) {
  12.     const template = document.createElement('template');
  13.     template.innerHTML = html;
  14.     const nNodes = template.content.childNodes.length;
  15.     if (nNodes !== 1) {
  16.         throw new Error(
  17.             `html parameter must represent a single node; got ${nNodes}. ` +
  18.             'Note that leading or trailing spaces around an element in your ' +
  19.             'HTML, like " <img/> ", get parsed as text nodes neighbouring ' +
  20.             'the element; call .trim() on your input to avoid this.'
  21.         );
  22.     }
  23.     return template.content.firstChild;
  24. }
  25.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement