Chrome扩展程序刷新页面不可见时 [英] Chrome extension refresh page when is not visible

查看:145
本文介绍了Chrome扩展程序刷新页面不可见时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(抱歉我的英文不好)

我正在开发一个基本的Chrome扩展,对网站进行一些更改。这一切都工作正常,但我不能得到这个工作:
当用户不在页面上(是在另一个选项卡或最小化浏览器),我需要刷新页面每隔5分钟,并显示通知如果有什么改变。我的问题是要检测用户是否不在网页上...我试图做到这一点,但没有奏效:

I'm developing a basic chrome extension that make some changes to a site. It is all working fine but I can't get this to work: When the user is not on the page (is on another tab or minimize the browser) i need to refresh the page every 5 minutes and show a notification if something changed. My problem is to detect if the user is "not on the page"... I tried to do this but didn't worked:

window.addEventListener('focus', function() {        
    isVisible = true;
});

window.addEventListener('blur', function() {
   isVisible = false;       
});

由于某些原因,模糊不会被调用(或仅在有时会被调用)...还有另外一种方法可以做到这一点吗?

The "blur" for some reason do not get called (or is called only sometimes)... there is another way to do this?

推荐答案

使用 Page Visibility API

function handleVisibilityChange() {
  if (document.webkitHidden) {
    // refresh every 5 minutes
  } else {
    // stop refreshing every 5 minutes
  }
}

document.addEventListener("webkitvisibilitychange", handleVisibilityChange, false);

这篇关于Chrome扩展程序刷新页面不可见时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆