updateContent

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

目前為 2023-11-20 提交的版本,檢視 最新版本

此腳本不應該直接安裝,它是一個供其他腳本使用的函式庫。欲使用本函式庫,請在腳本 metadata 寫上: // @require https://update.gf.qytechs.cn/scripts/480392/1283449/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, totalTime) => {
  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. };

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址