Greasy Fork is available in English.

updateContent

Smoothly updates the content of an HTML element with an erase and write effect.

Dette script bør ikke installeres direkte. Det er et bibliotek, som andre scripts kan inkludere med metadirektivet // @require https://update.greatest.deepsurf.us/scripts/480392/1283450/updateContent.js

  1. // ==UserScript==
  2. // @name updateContent
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.3
  5. // @description Smoothly updates the content of an HTML element with an erase and write effect.
  6. // @author IgnaV
  7. // @grant none
  8. // ==/UserScript==
  9.  
  10. const updateContent = (element, newContent, forceUpdate = false, totalTime = 2000) => {
  11. const currentContent = element.innerText;
  12. if (forceUpdate || currentContent !== newContent) {
  13. const intervalTime = totalTime / (currentContent.length + newContent.length);
  14. let i = currentContent.length - 1;
  15. const eraseInterval = setInterval(() => {
  16. element.innerText = currentContent.substring(0, i--);
  17. if (i < 0) {
  18. clearInterval(eraseInterval);
  19. let j = 0;
  20. const writeInterval = setInterval(() => {
  21. element.innerText = newContent.substring(0, ++j);
  22. if (j === newContent.length) {
  23. clearInterval(writeInterval);
  24. }
  25. }, intervalTime);
  26. }
  27. }, intervalTime);
  28. }
  29. };