chrome.extension.getBackgroundPage()在一段时间后返回null [英] chrome.extension.getBackgroundPage() returns null after awhile

查看:136
本文介绍了chrome.extension.getBackgroundPage()在一段时间后返回null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我的chrome扩展在chrome启动时加载时,一切似乎都没有问题,并且chrome.extension.getBackgroundPage()返回正确的值(从popup.js中启动)。但过了一段时间(2-3分钟)后,特别是如果浏览器不起作用,该函数将返回null。只有关闭并重新打开chrome才能解决问题。
我尝试通过以下方式操作它:

  if(chrome.extension.getBackgroundPage()== null)窗口。 location.reload(真); 

正如为什么chrome.extension.getBackgroundPage()返回null?,这有时会有所帮助,但大多数时候它只是刷新应用程序,因此必须关闭chrome 。
有人知道什么可能是错误的吗?根据引用的页面( https://developer.chrome.com/extensions/event_pages.htmlrel =nofollow noreferrer>事件和背景页面之间的区别)有一个更好的选择来获取背景,同时仍然使用事件页面:


如果您的扩展使用extension.getBackgroundPage,请切换到
runtime.getBackgroundPage。新的方法是异步的,因此
可以在返回之前启动事件页面。

我只是在异步回调中完成你的工作,它将接收后台页面作为函数参数。


以下是方法的详细说明 runtime.getBackgroundPage


When my chrome extension loads on chrome startup, everything seems to be ok and chrome.extension.getBackgroundPage() returns the right value (lunched from popup.js). But after awhile (2-3 minutes), especially if the browser is let alone, the function returns null. Only closing and re-opening chrome solves the problem. I tried to manipulate it by:

if (chrome.extension.getBackgroundPage() == null) window.location.reload(true);

As suggested at Why does chrome.extension.getBackgroundPage() return null?, which sometimes helps, but most of the times it just keeps refreshing the app so that chrome must be closed. Does anybody have an idea what could be wrong?

解决方案

According to the referenced page (Difference between Event and Background Page) there is a better option to get the background while still using Event Page :

If your extension uses, extension.getBackgroundPage, switch to runtime.getBackgroundPage instead. The newer method is asynchronous so that it can start the event page if necessary before returning it.

This worked well for me, just do your job in the asynchronous callback, which receives the background page as a function parameter.

Here is the specification of the method runtime.getBackgroundPage

这篇关于chrome.extension.getBackgroundPage()在一段时间后返回null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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