onVisibilityChange

Waits until the tab is focused, executing a callback function when it happens.

目前为 2023-11-20 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.gf.qytechs.cn/scripts/480374/1283357/onVisibilityChange.js

  1. // ==UserScript==
  2. // @name onVisibilityChange
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description Waits until the tab is focused, executing a callback function when it happens.
  6. // @author IgnaV
  7. // @grant none
  8. // ==/UserScript==
  9.  
  10. const onVisibilityChange = (onFocus, onBlur, minTime = 0) => {
  11. let lastExecutionTime = 0;
  12.  
  13. document.addEventListener('visibilitychange', function() {
  14. const currentTime = Date.now();
  15.  
  16. if (currentTime - lastExecutionTime >= minTime) {}
  17. if (document.visibilityState === 'visible') {
  18. onFocus?.();
  19. } else {
  20. onBlur?.();
  21. }
  22.  
  23. lastExecutionTime = currentTime;
  24. });
  25. };

QingJ © 2025

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