Jump to content

MediaWiki:Scramble.js: Difference between revisions

From RetroMUX
Created page with "// https://stackoverflow.com/a/27872144/383904 const randomString = (n, r='') => { while (n--) r += String.fromCharCode((r=Math.random()*62|0, r+=r>9?(r<36?55:61):48)); return r; }; const unscramble = (el) => { const chars = [...el.dataset.scramble]; const tot = chars.length; let iteration = 0; let ch = 0; let solved = ""; el._itv = setInterval(() => { if (iteration > 10) { iteration = 0; solved += chars[ch]; ch += 1; }..."
 
No edit summary
Line 1: Line 1:
// https://stackoverflow.com/a/27872144/383904
const alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!";
const randomString = (n, r='') => {
const shadowContainerName = 'shadow-container';
  while (n--) r += String.fromCharCode((r=Math.random()*62|0, r+=r>9?(r<36?55:61):48));
const scrambleCounterName = 'scramble-counter';
  return r;
const scrambleAttrib = 'data-scramble';
};


const unscramble = (el) => {
function createShadowCopy(container) {
   const chars = [...el.dataset.scramble];
   let shadowContainer = container.cloneNode(true);
   const tot = chars.length;
  shadowContainer.style.display = 'none';
  shadowContainer.setAttribute(shadowContainerName, 'true');
  shadowContainer.removeAttribute(scrambleAttrib);
  container.appendChild(shadowContainer);
 
   container.setAttribute(scrambleCounterName, 0);
    
    
   let iteration = 0;
   scrambleTree(container);
   let ch = 0;
}
  let solved = "";
 
 
function scrambleTree(container) {
  el._itv = setInterval(() => {
   scrambleNodes(container.childNodes);
}
 
function scrambleNodes(nodes) {
  nodes.forEach((node) => {
    if (node.nodeType == Node.TEXT_NODE) {
      let newTextArray = [];
      let oldValue = [...node.nodeValue];
      oldValue.forEach((char) => {
        if (/\S/.test(char)) {
          newTextArray.push(alpha[Math.floor(Math.random() * alpha.length)]);
        }
        else {
          newTextArray.push(char);
        }
      });


    if (iteration > 10) {
      node.nodeValue = newTextArray.join("");
      iteration = 0;
      solved += chars[ch];
      ch += 1;
     }
     }
      
     else if(node.getAttribute(shadowContainerName) === "true") {
    el.textContent = randomString(tot - ch, solved);
       // If we find the shadow copy, we totally just stop.
   
      return;
    if (ch === tot) {
       clearInterval(el._itv);
     }
     }
     iteration += 1;
     else if(node.nodeType == Node.ELEMENT_NODE) {
   }, 30);
      scrambleNodes(node.childNodes);
};
    }
   })
}
 
function unscramble(container) {
  children = Array.from(container.childNodes)
  let shadowCopy = children.find((node) => node.getAttribute && node.getAttribute(shadowContainerName));
 
  console.log(shadowCopy);
 
  if (shadowCopy) {
    while (container.firstChild) {
        container.removeChild(container.firstChild);
      }
 
    container.appendChild(shadowCopy);
    shadowCopy.style.display = '';
  }
}
 
function setLooper(t) {
  t.setAttribute(scrambleCounterName, "5");


const scramble = (el) => {
  t._itv = setInterval((s) => {
  clearInterval(el._itv);
    let iter = parseInt(s.getAttribute(scrambleCounterName));
  el.textContent = randomString([...el.dataset.scramble].length)
 
};
    if(iter > 5) {
      unscramble(s);
      clearInterval(s._itv);
    } else {
      console.log("Iter: " + iter);
      scrambleNodes(s.childNodes);
      iter = iter + 1;
      s.setAttribute(scrambleCounterName, iter.toString());
    }
  }, 1000, t);
}


const scrambler = (el) => {
$('[' + scrambleAttrib + ']').each(function () { createShadowCopy(this); });
  el.addEventListener("mouseenter", () => unscramble(el));
  el.addEventListener("mouseleave", () => scramble(el));
  scramble(el);
};


document.querySelectorAll("[data-scramble]").forEach(scrambler);
$('[' + scrambleAttrib + '="true"]').each(function () { setLooper(this); });

Revision as of 19:54, 24 August 2025

const alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!";
const shadowContainerName = 'shadow-container';
const scrambleCounterName = 'scramble-counter';
const scrambleAttrib = 'data-scramble';

function createShadowCopy(container) {
  let shadowContainer = container.cloneNode(true);
  shadowContainer.style.display = 'none';
  shadowContainer.setAttribute(shadowContainerName, 'true');
  shadowContainer.removeAttribute(scrambleAttrib);
  container.appendChild(shadowContainer);

  container.setAttribute(scrambleCounterName, 0);
  
  scrambleTree(container);
}

function scrambleTree(container) {
  scrambleNodes(container.childNodes);
}

function scrambleNodes(nodes) {
  nodes.forEach((node) => {
    if (node.nodeType == Node.TEXT_NODE) {
      let newTextArray = [];
      let oldValue = [...node.nodeValue];
      oldValue.forEach((char) => {
        if (/\S/.test(char)) {
          newTextArray.push(alpha[Math.floor(Math.random() * alpha.length)]);
        }
        else {
          newTextArray.push(char);
        }
      });

      node.nodeValue = newTextArray.join("");
    }
    else if(node.getAttribute(shadowContainerName) === "true") {
      // If we find the shadow copy, we totally just stop.
      return;
    }
    else if(node.nodeType == Node.ELEMENT_NODE) {
      scrambleNodes(node.childNodes);
    }
  })
}

function unscramble(container) {
  children = Array.from(container.childNodes)
  let shadowCopy = children.find((node) => node.getAttribute && node.getAttribute(shadowContainerName));

  console.log(shadowCopy);
  
  if (shadowCopy) {
    while (container.firstChild) {
        container.removeChild(container.firstChild);
      }

    container.appendChild(shadowCopy);
    shadowCopy.style.display = '';
  }
}

function setLooper(t) {
  t.setAttribute(scrambleCounterName, "5");

  t._itv = setInterval((s) => {
    let iter = parseInt(s.getAttribute(scrambleCounterName));

    if(iter > 5) {
      unscramble(s);
      clearInterval(s._itv);
    } else {
      console.log("Iter: " + iter);
      scrambleNodes(s.childNodes);
      iter = iter + 1;
      s.setAttribute(scrambleCounterName, iter.toString());
    }
  }, 1000, t);
}

$('[' + scrambleAttrib + ']').each(function () { createShadowCopy(this); });

$('[' + scrambleAttrib + '="true"]').each(function () { setLooper(this); });