Chrome extension refresh page when is not visible

為{幸葍}努か 提交于 2020-01-06 04:05:46

问题


(sorry for my bad english)

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?


回答1:


Use the Page Visibility API. Something like the following should work.

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

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


来源:https://stackoverflow.com/questions/8705200/chrome-extension-refresh-page-when-is-not-visible

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!