监听器不一致 - onUpdated在一台机器上,并在另一台机器上被替换 [英] Inconsistency with listeners - onUpdated on one machine and onReplaced on another machine

查看:78
本文介绍了监听器不一致 - onUpdated在一台机器上,并在另一台机器上被替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的eventPage.js(backgroundPage替换为google)中使用了下一个代码,我正面临一些奇怪的不一致。
这是场景:

第一台机器

新选项卡 - >

  onActivated 
onUpdated
onUpdated
onUpdated





$ p $ onUpdated
onUpdated

输入另一个网址 - >

  onUpdated 
onUpdated

第二台计算机



打开新标签 - >

  onActivated 

输入网址

<$输入另一个URL

on替换
onActivated

p>

  on替换
onActivated






这是我的代码:

  chrome.tabs .onUpdated.addListener(function(tabId,changeInfo,tab)
{
console.log(onUpdated);
});

chrome.tabs.onActivated.addListener(函数(tabId,changeInfo,tab)
{
console.log(onActivated);
});

chrome.tabs.onReplaced.addListener(function(tabId,changeInfo,tab)
{
console.log(onReplaced);
});

经过大量调试后,我发现这种差异的原因是预测网络在Google Chrome浏览器设置中改进页面加载性能的操作。



在第一台机器中,上述选项未被选中并且按预期工作。

第二台机器的预期行为是什么?



从文档中我可以理解onReplaced状态:


由于预渲染或
即时,一个标签会被替换为另一个标签。

尽管它的记录很差,并且没有办法知道这个选项与onUpdated onreplaced状态有某种关系,但我真的不明白第二台机器上的onActivated状态,以及为什么第一台机器和第二台机器有区别。



我无法在网络上找到有关此行为的任何文档。在stackoverflow上,我几乎找不到任何提及onReplaced侦听器的问题,但没有任何可以使用的信息。



谢谢

预测网络操作...选项,Chrome会尝试预测您的下一个操作(例如,哪些资源或页面您可能会请求下一步)并将其加载到后台(在发出请求之前)。只要您确实请求了该资源或网页,Chrome就不必先加载它,然后将其提供给您;相反,它只是服务于预加载的实例。这可以提高性能(只要预测您的下一步操作准确无误)。



为了提供预先加载的页面,Chrome会替换当前的选项卡(即 onReplaced 被触发),其中页面已经在后台加载(替换标签变为活动状态,因此 onActivated 事件)。由于内容已预先加载到替换标签中,因此没有 onUpdated 事件。






从Chrome的 prerender上的白皮书
$ b


Prerendering 扩展了预取的概念。它不是只下载顶级资源,而是完成向用户显示页面所需的所有工作,而不需要直到用户单击才显示。预渲染的行为与用户中途点击页面上的链接(在后台选项卡中打开它)然后切换到该选项卡的行为类似。然而,在预渲染中,该背景标签对用户是完全隐藏的,并且当用户点击时,其内容被无缝地交换到用户正在查看的相同标签。从用户的角度来看,网页的加载速度比以前快得多。



Web开发人员可以按照以下所述触发预渲染。从Chrome 17开始,根据用户与地址栏的互动情况,Chrome本身会在某些情况下启动预渲染。



I'm using the next code in my eventPage.js (backgroundPage replacement by google) and I'm facing some weird inconsistency. This is the scenario:

First Machine:

Opening new tab ->

onActivated 
onUpdated  
onUpdated  
onUpdated

Entering URL ->

onUpdated
onUpdated 

entering another URL ->

onUpdated
onUpdated 

Second Machine:

Opening new tab ->

onActivated

Entering URL

onReplaced
onActivated

entering another URL

onReplaced 
onActivated


This is my code:

chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab)
{   
    console.log("onUpdated");
       });

chrome.tabs.onActivated.addListener(function(tabId, changeInfo, tab)
{
    console.log("onActivated");
   });

 chrome.tabs.onReplaced.addListener(function(tabId, changeInfo, tab)
 {
    console.log("onReplaced");
   });

After a lot of debugging I've found that the cause of this difference is the option "Predict network actions to improve page load performance" in Google Chrome Settings.

In the first machine the option above is not selected and is working as expected.

Is that expected behavior for the second machine?

From the documentation I can somehow understand the onReplaced status:

Fired when a tab is replaced with another tab due to prerendering or instant.

Although it is very poorly documented and there's no way knowing that that option is somehow related to the onUpdated onReplaced statuses but I really don't understand the onActivated statuses in the second machine and why there's a difference between the first machine and the second machine.

I couldn't find any documentation about this behavior on the web. On stackoverflow I could hardly find one question that mentions the onReplaced listener but didn't had any info I can use.

Thanks

解决方案

Having the "Predict network actions..." option checked, causes Chrome to try to predict your next action (i.e. what resource or page you are likely to request next) and load it in the background (before you make the request). As soon as you do actually request that resource or page, Chrome will not have to first load it and then serve it to you; rather it just serves the pre-loaded instance. This improves performance (as long as the prediction of your next actions is accurate).

To serve a pre-loaded page, Chrome replaces the current tab (that's when the onReplaced is triggered) with a tab that has the page already loaded in the background (the replacing tab becomes active, thus the onActivated event). Because the content has been loaded in the replacing tab beforehand, there is no onUpdated event.


From Chrome's whitepaper on prerender:

Prerendering extends the concept of prefetching. Instead of just downloading the top-level resource, it does all of the work necessary to show the page to the user—without actually showing it until the user clicks. Prerendering behaves similarly to if a user middle-clicked on a link on a page (opening it in a background tab) and then later switched to that tab. However, in prerendering, that "background tab" is totally hidden from the user, and when the user clicks, its contents are seamlessly swapped into the same tab the user was viewing. From the user’s perspective, the page simply loads much faster than before.

Web developers can trigger prerendering as described below. Beginning in Chrome 17, Chrome itself will initiate prerendering in some cases, based on the user's interactions with the address bar.

这篇关于监听器不一致 - onUpdated在一台机器上,并在另一台机器上被替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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