google-chrome - 与监听器不一致 - 一台计算机上的 onUpdated 和另一台计算机上的 onReplaced

标签 google-chrome google-chrome-extension

我正在 eventPage.js 中使用下一个代码(由 google 替换 backgroundPage),但我遇到了一些奇怪的不一致问题。 这是场景:

第一台机器:

打开新标签 ->

onActivated 
onUpdated  
onUpdated  
onUpdated

输入网址 ->

onUpdated
onUpdated 

输入另一个 URL ->

onUpdated
onUpdated 

第二台机器:

打开新标签 ->

onActivated

输入网址

onReplaced
onActivated

输入另一个网址

onReplaced 
onActivated
<小时/>

这是我的代码:

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");
   });

经过大量调试,我发现造成这种差异的原因是 Google Chrome 设置中的“预测网络操作以提高页面加载性能”选项。

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

这是第二台机器的预期行为吗?

从文档中我可以以某种方式理解 onReplaced 状态:

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

虽然它的记录非常少,并且无法知道该选项在某种程度上与 onUpdated onReplaced 状态相关,但我真的不明白第二台机器中的 onActivated 状态以及为什么第一台机器和第二台机器之间存在差异第二台机器。

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

谢谢

最佳答案

选中“预测网络操作...”选项后,Chrome 会尝试预测您的下一个操作(即您接下来可能请求的资源或页面)并将其加载到背景(在提出请求之前)。一旦您实际请求该资源或页面,Chrome 就不必先加载它,然后再为您提供服务;相反,它只为预加载的实例提供服务。这会提高性能(只要对您的下一步操作的预测准确)。

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

<小时/>

来自 Chrome 的 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.

关于google-chrome - 与监听器不一致 - 一台计算机上的 onUpdated 和另一台计算机上的 onReplaced,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21224658/

相关文章:

javascript - chrome 弹出窗口关闭时的事件

javascript - 可以通过 Chrome 扩展程序修改窗口对象吗?

javascript - 如何在 chrome 浏览器中禁用 pepflashplayer.dll

html - CSS 动画不适用于 Mac chrome 但适用于 iPad Chrome

javascript - 使用 let in for 循环的时间死区

javascript - 为什么在 Chrome 选项卡中更改 url 在输入指定 url 时不会更新选项卡 Url?

javascript - 更改事件窗口 (chrome.tabs)

html - 警告 : <link rel=preload> must have a valid `as` value

html - 为什么我的 CSS3 动画在 Google Chrome 中不流畅(但在其他浏览器中非常流畅)?

html - ng服务一次时如何防止自动重新加载页面?