如何检查一个标签是否已经在background.js中重新加载? [英] How to check if a tab has been reloaded in background.js?

查看:184
本文介绍了如何检查一个标签是否已经在background.js中重新加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Chrome扩展程序,它需要检测该选项卡是否已重新加载,也就是说,用户刷新了页面(通过按下刷新按钮或将光标放在URL后面并按Enter键)没有URL更改。如果发生这种情况,那么我将重新初始化在 background.js 中定义的变量。

我想知道如何得到这个 is_reload 布尔值?我试图使用 windows.performance.navigation.type ,但它在 background.js 中没有任何影响。 p>

解决方案

使用 webNavigation



从后台上下文中的脚本中使用的API来确定页面何时实际上重新加载,而不仅仅是在没有导航的页面中更新的URL是 webNavigation 。您可能需要 webNavigation.onCommitted 监听器,这是唯一一个有关为什么页面正在浏览的信息的事件( transitionType transitionQualifiers ),但 webNavigation.onDOMContentLoaded webNavigation.onCompleted 也可以使用,具体取决于您的实际需求。



如果哟我们的目标确实是确定它是一个重载,而不仅仅是一个负载,您需要为每个选项卡保留当前URL的记录,并将新URL与旧URL进行比较,或者使用 TransitionType 包含在 webNavigation.onCommitted transitionType 属性,它将显式声明它为reload


$ b

$ b

  webNavigation.onBeforeNavigate  - > arg [0] = {frameId:0,parentFrameId:-1,processId:-1,tabId:411,timeStamp:1500401223978.314,url:http://www.example .com /} 
webRequest.onBeforeRequest - > arg [0] = {frameId:0,method:GET,parentFrameId:-1,requestId:260870,tabId:411,timeStamp:1500401223979.044, :main_frame,url:http://www.example.com/}
webRequest.onBeforeSendHeaders - > arg [0] = {frameId:0,method:GET,parentFrameId: - 1,requestHeaders:[{name:Upgrade-Insecure-Requests,value 1},{name:用户代理,值:Mozilla / 5.0(Windows NT 10.0; Win64; x64)AppleWebKit / 537.36(KHTML,像Gecko)Chrome / 59.0.3071.115 Safari / 537.36 },{ 名称: 接受, 值: text / html的,应用/ XHTML + xml的,应用/ XML; q = 0.9,图像/ WEBP,图像/ APNG,* / *; q = 0.8 },{name:Accept-Encoding,value:gzip,deflate},{name:Accept-Language,value:en-US,en; q = 0.8 }],requestId:260870,tabId:411,timeStamp:1500401223979.3242,type:main_frame,url:http://www.example.com/}
webRequest.onSendHeaders - > arg [0] = {frameId:0,method:GET,parentFrameId: - 1,requestHeaders:[{name:Upgrade-Insecure-Requests,value 1},{name:用户代理,值:Mozilla / 5.0(Windows NT 10.0; Win64; x64)AppleWebKit / 537.36(KHTML,像Gecko)Chrome / 59.0.3071.115 Safari / 537.36 },{ 名称: 接受, 值: text / html的,应用/ XHTML + xml的,应用/ XML; q = 0.9,图像/ WEBP,图像/ APNG,* / *; q = 0.8 },{name:Accept-Encoding,value:gzip,deflate},{name:Accept-Language,value:en-US,en; q = 0.8 }],requestId:260870,tabId:411,timeStamp:1500401223979.538,type:main_frame,url:http://www.example.com/}
webRequest.onHeadersReceived - > arg [0] = {frameId:0,method:GET,parentFrameId:-1,requestId:260870,responseHeaders:[{name:Content-Encoding 值: gzip的},{ 名称: 接受范围, 值: 字节},{ 名称: 缓存控制, 值:最大年龄= 604800},{name:Content-Type,value:text / html},{name:Date,value:2017年7月18日星期二18:07:03 GMT},{name:Etag,value:\359670651 \},{name:Expires,value:2017年7月25日星期二18:07 :03 GMT},{name:Last-Modified,value:Fri,2013年8月9日23:54:35 GMT},{name:Server,value: ECS(rhv / 818F)},{name:Vary,value:Accept-Encoding},{name:X-Cache,value:HIT}, name:Content-Length,value:606}],statusCode:200,statusLine:HTTP / 1.1 200 OK,tabId:411,timeStamp:1500401224072.296, type:main_frame,url:http://www.example.com/}
webRequest.onResponseStarted - > arg [0] = {frameId:0,fromCache:false,ip:93.184.216.34,method:GET,parentFrameId: - 1,requestId:260870 responseHeaders响应:[{ 名称: 内容编码, 值: gzip的},{ 名称: 接受范围, 值: 字节},{ 名 : 缓存控制, 值: 最大年龄= 604800},{ 名: 内容类型, 值: text / html的},{ 名: 日期 ,value:2017年7月18日18:07:03 GMT},{name:Etag,value:\359670651 \},{name: 2017年7月25日星期三18:07:03 GMT},{name:Last-Modified,value:星期五,2013年8月9日23时54分35秒GMT },{name:Server,value:ECS(rhv / 818F)},{name:Vary,value:Accept-Encoding},{name X-缓存, 值: 命中},{ 名称: 内容长度, 值: 606}], 的StatusCode:200, 状态行:HTTP / 1.1 200 OK,tabId:411,timeStamp:1500401224072.5032,type:main_frame,url:http://www.example.com/}
webRequest.onCompleted - > ; arg [0] = {frameId:0,fromCache:false,ip:93.184.216.34,method:GET,parentFrameId: - 1,requestId:260870 responseHeaders响应:[{ 名称: 内容编码, 值: gzip的},{ 名称: 接受范围, 值: 字节},{ 名 : 缓存控制, 值: 最大年龄= 604800},{ 名: 内容类型, 值: text / html的},{ 名: 日期 ,value:2017年7月18日18:07:03 GMT},{name:Etag,value:\359670651 \},{name: 2017年7月25日星期三18:07:03 GMT},{name:Last-Modified,value:星期五,2013年8月9日23时54分35秒GMT },{name:Server,value:ECS(rhv / 818F)},{name:Vary,value:Accept-Encoding},{name X-缓存, 值: 命中},{ 名称: 内容长度, 值: 606}], 的StatusCode:200, 状态行:HTTP / 1.1 200 OK,tabId:411,timeStamp:1500401224074.0261,type:main_frame,url:http://www.example.com/}
tabs.onUpdated - > ; arg [0] = 411 :: arg [1] = {status:loading,url:http://www.example.com/} :: arg [2] = {active :真 可听:假 autoDiscardable:, 丢弃 真:假, 高度:902, 突出显示:真, ID:411, 隐身:假, 指数:1 mutedInfo:{ 静音:假}, 固定:假的, 选择:真正的 状态: 装载, 称号: www.example.com, URL: http://www.example.com/\",\"width\":1282,\"windowId\":10}
tabs.onZoomChange - > arg [0] = {newZoomFactor:1,oldZoomFactor:1,tabId:411,zoomSettings:{mode:automatic,scope:per-origin}}
webNavigation.onCommitted - > arg [0] = {frameId:0,processId:107,tabId:411,timeStamp:1500401224079.4019,transitionQualifiers:[]transitionType:reloadurl http://www.example.com/}
--->>以下是您可以告诉它是重新加载的位置------------------ -------------------------------------------------- ------------------------------ ^^^^^^^^^^^^^^^^^^^^ ^^^^^
history.onVisited - > arg [0] = {id:42,lastVisitTime:1500401224077.579,title:示例域,typedCount:1,url:http://www.example.com/ ,visitCount:12}
tabs.onUpdated - > arg [0] = 411 :: arg [1] = {title:示例域} :: arg [2] = {active:true,audible:false,autoDiscardable:true,丢弃 :假, 高度 :902, 突出显示 :真, ID :411, 隐身 :假, 指数 :1, mutedInfo :{ 静音 :假}, 固定 :false,selected:true,status:加载,title:示例域,url:http://www.example.com/,width:1282, windowId:10}
webNavigation.onDOMContentLoaded - > arg [0] = {frameId:0,processId:107,tabId:411,timeStamp:1500401224093.404,url:http://www.example.com/}
webNavigation.on完成 - > arg [0] = {frameId:0,processId:107,tabId:411,timeStamp:1500401224094.768,url:http://www.example.com/}
tabs.onUpdated - > arg [0] = 411 :: arg [1] = {status:complete} :: arg [2] = {active:true,audible:false,autoDiscardable:true, :假, 高度 :902, 突出显示 :真, ID :411, 隐身 :假, 指数 :1, mutedInfo :{ 静音 :假}, 固定: false,selected:true,status:complete,title:Example Domain,url:http://www.example.com/,width:1282,windowId :10}

以下是用户选择地址栏并点击输入

  webNavigation.onBeforeNavigate  - > arg [0] = {frameId:0,parentFrameId:-1,processId:-1,tabId:411,timeStamp:1500403090082.833,url:http://www.example .com /} 
webRequest.onBeforeRequest - > arg [0] = {frameId:0,method:GET,parentFrameId:-1,requestId:261350,tabId:411,timeStamp:1500403090083.163, :main_frame,url:http://www.example.com/}
webRequest.onBeforeSendHeaders - > arg [0] = {frameId:0,method:GET,parentFrameId: - 1,requestHeaders:[{name:Upgrade-Insecure-Requests,value 1},{name:用户代理,值:Mozilla / 5.0(Windows NT 10.0; Win64; x64)AppleWebKit / 537.36(KHTML,像Gecko)Chrome / 59.0.3071.115 Safari / 537.36 },{ 名称: 接受, 值: text / html的,应用/ XHTML + xml的,应用/ XML; q = 0.9,图像/ WEBP,图像/ APNG,* / *; q = 0.8 },{name:Accept-Encoding,value:gzip,deflate},{name:Accept-Language,value:en-US,en; q = 0.8 }],requestId:261350,tabId:411,timeStamp:1500403090083.3289,type:main_frame,url:http://www.example.com/}
webRequest.onSendHeaders - > arg [0] = {frameId:0,method:GET,parentFrameId: - 1,requestHeaders:[{name:Upgrade-Insecure-Requests,value 1},{name:用户代理,值:Mozilla / 5.0(Windows NT 10.0; Win64; x64)AppleWebKit / 537.36(KHTML,像Gecko)Chrome / 59.0.3071.115 Safari / 537.36 },{ 名称: 接受, 值: text / html的,应用/ XHTML + xml的,应用/ XML; q = 0.9,图像/ WEBP,图像/ APNG,* / *; q = 0.8 },{name:Accept-Encoding,value:gzip,deflate},{name:Accept-Language,value:en-US,en; q = 0.8 }],requestId:261350,tabId:411,timeStamp:1500403090083.4739,type:main_frame,url:http://www.example.com/}
webRequest.onHeadersReceived - > arg [0] = {frameId:0,method:GET,parentFrameId: - 1,requestId:261350,responseHeaders:[{name:Accept- Ranges 值: 字节},{ 名称: 缓存控制, 值: 最大年龄= 604800},{ 名称: 日期, 值:星期二, 2017年7月18日18:38:09 GMT},{name:Expires,value:2017年7月25日星期二18:38:09 GMT},{name:Last-Modified ,value:Fri,2013年8月9日23:54:35 GMT},{name:Server,value:ECS(rhv / 818F)},{name:Vary 值: 接受编码},{ 名: X-缓存, 值: HIT},{ 名: 内容编码, 值: 压缩},{ 名 : 内容类型, 值: text / html的},{ 名: Etag的, 值: \ 359670651\ },{ name:Content-Length,value:606}],statusCode:200,statusLine:HTTP / 1.1 200 OK,tabId:411,timeStamp:1500403090130.209, type:main_frame,url:http://www.example.com/}
webRequest.onResponseStarted - > arg [0] = {frameId:0,fromCache:true,ip:93.184.216.34,method:GET,parentFrameId: - 1,requestId:261350 responseHeaders响应:[{ 姓名: 接受范围, 值: 字节},{ 名称: 缓存控制, 值: 最大年龄= 604800}, {name:Date,value:2017年7月18日星期二18:38:09 GMT},{name:Expires,value:2017年7月25日星期二18:38 :09 GMT},{name:Last-Modified,value:Fri,2013年8月9日23:54:35 GMT},{name:Server,value: ECS(rhv / 818F)},{name:Vary,value:Accept-Encoding},{name:X-Cache,value:HIT}, 名: 内容编码, 值: 压缩},{ 名: 内容类型, 值: text / html的},{ 名: Etag的, 值: \ 359670651\ },{ 名称: 内容长度, 值: 606}], 的StatusCode:200, 状态行:HTTP / 1.1 200 OK,tabId:411,timeStamp:1500403090130.3289,type:main_frame,url:http://www.example.com/}
webRequest.onCompleted - > ; arg [0] = {frameId:0,fromCache:true,ip:93.184.216.34,method:GET,parentFrameId: - 1,requestId:261350 responseHeaders响应:[{ 姓名: 接受范围, 值: 字节},{ 名称: 缓存控制, 值: 最大年龄= 604800}, {name:Date,value:2017年7月18日星期二18:38:09 GMT},{name:Expires,value:2017年7月25日星期二18:38 :09 GMT},{name:Last-Modified,value:Fri,2013年8月9日23:54:35 GMT},{name:Server,value: ECS(rhv / 818F)},{name:Vary,value:Accept-Encoding},{name:X-Cache,value:HIT}, 名: 内容编码, 值: 压缩},{ 名: 内容类型, 值: text / html的},{ 名: Etag的, 值: \ 359670651\ },{ 名称: 内容长度, 值: 606}], 的StatusCode:200, 状态行:HTTP / 1.1 200 OK,tabId:411,timeStamp:1500403090131.1592,type:main_frame,url:http://www.example.com/}
tabs.onUpdated - > ; arg [0] = 411 :: arg [1] = {status:loading} :: arg [2] = {active:true,audible:false,autoDiscardable:true, :假, 高度 :902, 突出显示 :真, ID :411, 隐身 :假, 指数 :1, mutedInfo :{ 静音 :假}, 固定: false,selected:true,status:loading,title:示例域,url:http://www.example.com/,width:1282,windowId :10}
tabs.onZoomChange - > arg [0] = {newZoomFactor:1,oldZoomFactor:1,tabId:411,zoomSettings:{mode:automatic,scope:per-origin}}
webNavigation.onCommitted - > arg [0] = {frameId:0,processId:107,tabId:411,timeStamp:1500403090135.629,transitionQualifiers:[from_address_bar],transitionType:reload :http://www.example.com/}
--->>这里是您可以告诉它重新加载的地方--------------- -------------------------------------------------- ------------------------------------------------- ^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
--->>这告诉你它是用户在地址栏中按[Enter] ---------------------------------------------- ^^^^ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
history.onVisited - > arg [0] = {id:42,lastVisitTime:1500403090134.133,title:示例域,typedCount:1,url:http://www.example.com/ ,visitCount:12}
webNavigation.onDOMContentLoaded - > arg [0] = {frameId:0,processId:107,tabId:411,timeStamp:1500403090144.5688,url:http://www.example.com/}
webNavigation.on完成 - > arg [0] = {frameId:0,processId:107,tabId:411,timeStamp:1500403090146.102,url:http://www.example.com/}
tabs.onUpdated - > arg [0] = 411 :: arg [1] = {status:complete} :: arg [2] = {active:true,audible:false,autoDiscardable:true, :假, 高度 :902, 突出显示 :真, ID :411, 隐身 :假, 指数 :1, mutedInfo :{ 静音 :假}, 固定: false,selected:true,status:complete,title:Example Domain,url:http://www.example.com/,width:1282,windowId :10}



或者,使用内容脚本告诉页面重新加载的后台脚本 h2>

您可以使用 content_scripts 条目> manifest.json ,它被注入到你想要检测的域中被加载/重载。内容脚本每次运行时都可以发送消息,使用 runtime.sendMessage() 转换为后台脚本(在 runtime.onMessage 侦听器)告诉后台脚本页面已被加载/重载。



如果您正在监视有限数量的域,这将更可取,因为用户在安装扩展程序时收到的警告只会声明扩展程序将有权访问指定的域,而不是所有域。

I am writing a Chrome extension which needs to detect if the tab has been reloaded, that is to say, the user refreshed the page (either by pressing the refresh button, or put a cursor behind the URL and press Enter) with no URL change. If it happens, then I will reinitialize my variables defined in background.js.

I am wondering how could I get this "is_reload" boolean value? I tried to make use of windows.performance.navigation.type, but it doesn't have any effect in the background.js.

解决方案

Use webNavigation

The API to use from a script in the background context to determine when a page is actually reloaded, not just the URL being updated in-page without navigation, is webNavigation. You may want a webNavigation.onCommitted listener, which is the only event which has information as to why the page is navigating (transitionType and transitionQualifiers), but webNavigation.onDOMContentLoaded or webNavigation.onCompleted will also work, depending on what your actual needs are.

If your goal really is to determine that it's a reload, not just a load, you will either need to keep a record of the current URL for each tab and compare the new URL against the old one, or use the TransitionType Object contained in the webNavigation.onCommitted's transitionType property, which will explicitly declare it a "reload".

These are the events which occur when you reload a tab by clicking on the "reload this page" button:

webNavigation.onBeforeNavigate    ->  arg[0]= {"frameId":0,"parentFrameId":-1,"processId":-1,"tabId":411,"timeStamp":1500401223978.314,"url":"http://www.example.com/"}        
webRequest.onBeforeRequest        ->  arg[0]= {"frameId":0,"method":"GET","parentFrameId":-1,"requestId":"260870","tabId":411,"timeStamp":1500401223979.044,"type":"main_frame","url":"http://www.example.com/"}        
webRequest.onBeforeSendHeaders    ->  arg[0]= {"frameId":0,"method":"GET","parentFrameId":-1,"requestHeaders":[{"name":"Upgrade-Insecure-Requests","value":"1"},{"name":"User-Agent","value":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36"},{"name":"Accept","value":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"},{"name":"Accept-Encoding","value":"gzip, deflate"},{"name":"Accept-Language","value":"en-US,en;q=0.8"}],"requestId":"260870","tabId":411,"timeStamp":1500401223979.3242,"type":"main_frame","url":"http://www.example.com/"}        
webRequest.onSendHeaders          ->  arg[0]= {"frameId":0,"method":"GET","parentFrameId":-1,"requestHeaders":[{"name":"Upgrade-Insecure-Requests","value":"1"},{"name":"User-Agent","value":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36"},{"name":"Accept","value":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"},{"name":"Accept-Encoding","value":"gzip, deflate"},{"name":"Accept-Language","value":"en-US,en;q=0.8"}],"requestId":"260870","tabId":411,"timeStamp":1500401223979.538,"type":"main_frame","url":"http://www.example.com/"}        
webRequest.onHeadersReceived      ->  arg[0]= {"frameId":0,"method":"GET","parentFrameId":-1,"requestId":"260870","responseHeaders":[{"name":"Content-Encoding","value":"gzip"},{"name":"Accept-Ranges","value":"bytes"},{"name":"Cache-Control","value":"max-age=604800"},{"name":"Content-Type","value":"text/html"},{"name":"Date","value":"Tue, 18 Jul 2017 18:07:03 GMT"},{"name":"Etag","value":"\"359670651\""},{"name":"Expires","value":"Tue, 25 Jul 2017 18:07:03 GMT"},{"name":"Last-Modified","value":"Fri, 09 Aug 2013 23:54:35 GMT"},{"name":"Server","value":"ECS (rhv/818F)"},{"name":"Vary","value":"Accept-Encoding"},{"name":"X-Cache","value":"HIT"},{"name":"Content-Length","value":"606"}],"statusCode":200,"statusLine":"HTTP/1.1 200 OK","tabId":411,"timeStamp":1500401224072.296,"type":"main_frame","url":"http://www.example.com/"}        
webRequest.onResponseStarted      ->  arg[0]= {"frameId":0,"fromCache":false,"ip":"93.184.216.34","method":"GET","parentFrameId":-1,"requestId":"260870","responseHeaders":[{"name":"Content-Encoding","value":"gzip"},{"name":"Accept-Ranges","value":"bytes"},{"name":"Cache-Control","value":"max-age=604800"},{"name":"Content-Type","value":"text/html"},{"name":"Date","value":"Tue, 18 Jul 2017 18:07:03 GMT"},{"name":"Etag","value":"\"359670651\""},{"name":"Expires","value":"Tue, 25 Jul 2017 18:07:03 GMT"},{"name":"Last-Modified","value":"Fri, 09 Aug 2013 23:54:35 GMT"},{"name":"Server","value":"ECS (rhv/818F)"},{"name":"Vary","value":"Accept-Encoding"},{"name":"X-Cache","value":"HIT"},{"name":"Content-Length","value":"606"}],"statusCode":200,"statusLine":"HTTP/1.1 200 OK","tabId":411,"timeStamp":1500401224072.5032,"type":"main_frame","url":"http://www.example.com/"}        
webRequest.onCompleted            ->  arg[0]= {"frameId":0,"fromCache":false,"ip":"93.184.216.34","method":"GET","parentFrameId":-1,"requestId":"260870","responseHeaders":[{"name":"Content-Encoding","value":"gzip"},{"name":"Accept-Ranges","value":"bytes"},{"name":"Cache-Control","value":"max-age=604800"},{"name":"Content-Type","value":"text/html"},{"name":"Date","value":"Tue, 18 Jul 2017 18:07:03 GMT"},{"name":"Etag","value":"\"359670651\""},{"name":"Expires","value":"Tue, 25 Jul 2017 18:07:03 GMT"},{"name":"Last-Modified","value":"Fri, 09 Aug 2013 23:54:35 GMT"},{"name":"Server","value":"ECS (rhv/818F)"},{"name":"Vary","value":"Accept-Encoding"},{"name":"X-Cache","value":"HIT"},{"name":"Content-Length","value":"606"}],"statusCode":200,"statusLine":"HTTP/1.1 200 OK","tabId":411,"timeStamp":1500401224074.0261,"type":"main_frame","url":"http://www.example.com/"}        
tabs.onUpdated                    ->  arg[0]= 411 :: arg[1]= {"status":"loading","url":"http://www.example.com/"} :: arg[2]= {"active":true,"audible":false,"autoDiscardable":true,"discarded":false,"height":902,"highlighted":true,"id":411,"incognito":false,"index":1,"mutedInfo":{"muted":false},"pinned":false,"selected":true,"status":"loading","title":"www.example.com","url":"http://www.example.com/","width":1282,"windowId":10}    
tabs.onZoomChange                 ->  arg[0]= {"newZoomFactor":1,"oldZoomFactor":1,"tabId":411,"zoomSettings":{"mode":"automatic","scope":"per-origin"}}        
webNavigation.onCommitted         ->  arg[0]= {"frameId":0,"processId":107,"tabId":411,"timeStamp":1500401224079.4019,"transitionQualifiers":[],"transitionType":"reload","url":"http://www.example.com/"}
--->>Here is where you can tell it's a reload --------------------------------------------------------------------------------------------------^^^^^^^^^^^^^^^^^^^^^^^^^
history.onVisited                 ->  arg[0]= {"id":"42","lastVisitTime":1500401224077.579,"title":"Example Domain","typedCount":1,"url":"http://www.example.com/","visitCount":12}        
tabs.onUpdated                    ->  arg[0]= 411 :: arg[1]= {"title":"Example Domain"} :: arg[2]= {"active":true,"audible":false,"autoDiscardable":true,"discarded":false,"height":902,"highlighted":true,"id":411,"incognito":false,"index":1,"mutedInfo":{"muted":false},"pinned":false,"selected":true,"status":"loading","title":"Example Domain","url":"http://www.example.com/","width":1282,"windowId":10}    
webNavigation.onDOMContentLoaded  ->  arg[0]= {"frameId":0,"processId":107,"tabId":411,"timeStamp":1500401224093.404,"url":"http://www.example.com/"}        
webNavigation.onCompleted         ->  arg[0]= {"frameId":0,"processId":107,"tabId":411,"timeStamp":1500401224094.768,"url":"http://www.example.com/"}        
tabs.onUpdated                    ->  arg[0]= 411 :: arg[1]= {"status":"complete"} :: arg[2]= {"active":true,"audible":false,"autoDiscardable":true,"discarded":false,"height":902,"highlighted":true,"id":411,"incognito":false,"index":1,"mutedInfo":{"muted":false},"pinned":false,"selected":true,"status":"complete","title":"Example Domain","url":"http://www.example.com/","width":1282,"windowId":10}   

Here are the events for when the user selects the address bar and hits Enter:

webNavigation.onBeforeNavigate   ->  arg[0]= {"frameId":0,"parentFrameId":-1,"processId":-1,"tabId":411,"timeStamp":1500403090082.833,"url":"http://www.example.com/"}        
webRequest.onBeforeRequest       ->  arg[0]= {"frameId":0,"method":"GET","parentFrameId":-1,"requestId":"261350","tabId":411,"timeStamp":1500403090083.163,"type":"main_frame","url":"http://www.example.com/"}        
webRequest.onBeforeSendHeaders   ->  arg[0]= {"frameId":0,"method":"GET","parentFrameId":-1,"requestHeaders":[{"name":"Upgrade-Insecure-Requests","value":"1"},{"name":"User-Agent","value":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36"},{"name":"Accept","value":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"},{"name":"Accept-Encoding","value":"gzip, deflate"},{"name":"Accept-Language","value":"en-US,en;q=0.8"}],"requestId":"261350","tabId":411,"timeStamp":1500403090083.3289,"type":"main_frame","url":"http://www.example.com/"}        
webRequest.onSendHeaders         ->  arg[0]= {"frameId":0,"method":"GET","parentFrameId":-1,"requestHeaders":[{"name":"Upgrade-Insecure-Requests","value":"1"},{"name":"User-Agent","value":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36"},{"name":"Accept","value":"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"},{"name":"Accept-Encoding","value":"gzip, deflate"},{"name":"Accept-Language","value":"en-US,en;q=0.8"}],"requestId":"261350","tabId":411,"timeStamp":1500403090083.4739,"type":"main_frame","url":"http://www.example.com/"}        
webRequest.onHeadersReceived     ->  arg[0]= {"frameId":0,"method":"GET","parentFrameId":-1,"requestId":"261350","responseHeaders":[{"name":"Accept-Ranges","value":"bytes"},{"name":"Cache-Control","value":"max-age=604800"},{"name":"Date","value":"Tue, 18 Jul 2017 18:38:09 GMT"},{"name":"Expires","value":"Tue, 25 Jul 2017 18:38:09 GMT"},{"name":"Last-Modified","value":"Fri, 09 Aug 2013 23:54:35 GMT"},{"name":"Server","value":"ECS (rhv/818F)"},{"name":"Vary","value":"Accept-Encoding"},{"name":"X-Cache","value":"HIT"},{"name":"Content-Encoding","value":"gzip"},{"name":"Content-Type","value":"text/html"},{"name":"Etag","value":"\"359670651\""},{"name":"Content-Length","value":"606"}],"statusCode":200,"statusLine":"HTTP/1.1 200 OK","tabId":411,"timeStamp":1500403090130.209,"type":"main_frame","url":"http://www.example.com/"}        
webRequest.onResponseStarted     ->  arg[0]= {"frameId":0,"fromCache":true,"ip":"93.184.216.34","method":"GET","parentFrameId":-1,"requestId":"261350","responseHeaders":[{"name":"Accept-Ranges","value":"bytes"},{"name":"Cache-Control","value":"max-age=604800"},{"name":"Date","value":"Tue, 18 Jul 2017 18:38:09 GMT"},{"name":"Expires","value":"Tue, 25 Jul 2017 18:38:09 GMT"},{"name":"Last-Modified","value":"Fri, 09 Aug 2013 23:54:35 GMT"},{"name":"Server","value":"ECS (rhv/818F)"},{"name":"Vary","value":"Accept-Encoding"},{"name":"X-Cache","value":"HIT"},{"name":"Content-Encoding","value":"gzip"},{"name":"Content-Type","value":"text/html"},{"name":"Etag","value":"\"359670651\""},{"name":"Content-Length","value":"606"}],"statusCode":200,"statusLine":"HTTP/1.1 200 OK","tabId":411,"timeStamp":1500403090130.3289,"type":"main_frame","url":"http://www.example.com/"}        
webRequest.onCompleted           ->  arg[0]= {"frameId":0,"fromCache":true,"ip":"93.184.216.34","method":"GET","parentFrameId":-1,"requestId":"261350","responseHeaders":[{"name":"Accept-Ranges","value":"bytes"},{"name":"Cache-Control","value":"max-age=604800"},{"name":"Date","value":"Tue, 18 Jul 2017 18:38:09 GMT"},{"name":"Expires","value":"Tue, 25 Jul 2017 18:38:09 GMT"},{"name":"Last-Modified","value":"Fri, 09 Aug 2013 23:54:35 GMT"},{"name":"Server","value":"ECS (rhv/818F)"},{"name":"Vary","value":"Accept-Encoding"},{"name":"X-Cache","value":"HIT"},{"name":"Content-Encoding","value":"gzip"},{"name":"Content-Type","value":"text/html"},{"name":"Etag","value":"\"359670651\""},{"name":"Content-Length","value":"606"}],"statusCode":200,"statusLine":"HTTP/1.1 200 OK","tabId":411,"timeStamp":1500403090131.1592,"type":"main_frame","url":"http://www.example.com/"}        
tabs.onUpdated                   ->  arg[0]= 411 :: arg[1]= {"status":"loading"} :: arg[2]= {"active":true,"audible":false,"autoDiscardable":true,"discarded":false,"height":902,"highlighted":true,"id":411,"incognito":false,"index":1,"mutedInfo":{"muted":false},"pinned":false,"selected":true,"status":"loading","title":"Example Domain","url":"http://www.example.com/","width":1282,"windowId":10}    
tabs.onZoomChange                ->  arg[0]= {"newZoomFactor":1,"oldZoomFactor":1,"tabId":411,"zoomSettings":{"mode":"automatic","scope":"per-origin"}}        
webNavigation.onCommitted        ->  arg[0]= {"frameId":0,"processId":107,"tabId":411,"timeStamp":1500403090135.629,"transitionQualifiers":["from_address_bar"],"transitionType":"reload","url":"http://www.example.com/"}        
--->>Here is where you can tell it's a reload ------------------------------------------------------------------------------------------------------------------^^^^^^^^^^^^^^^^^^^^^^^^^
--->>This tells you it was the user hitting [Enter] in the address bar----------------------------------------------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
history.onVisited                ->  arg[0]= {"id":"42","lastVisitTime":1500403090134.133,"title":"Example Domain","typedCount":1,"url":"http://www.example.com/","visitCount":12}        
webNavigation.onDOMContentLoaded ->  arg[0]= {"frameId":0,"processId":107,"tabId":411,"timeStamp":1500403090144.5688,"url":"http://www.example.com/"}        
webNavigation.onCompleted        ->  arg[0]= {"frameId":0,"processId":107,"tabId":411,"timeStamp":1500403090146.102,"url":"http://www.example.com/"}        
tabs.onUpdated                   ->  arg[0]= 411 :: arg[1]= {"status":"complete"} :: arg[2]= {"active":true,"audible":false,"autoDiscardable":true,"discarded":false,"height":902,"highlighted":true,"id":411,"incognito":false,"index":1,"mutedInfo":{"muted":false},"pinned":false,"selected":true,"status":"complete","title":"Example Domain","url":"http://www.example.com/","width":1282,"windowId":10}    

Alternately, use a content script to tell the background script the page has reloaded

You can use a content script, defined in a content_scripts entry in your manifest.json, which is injected into the domains you desire to detect are loaded/reloaded. Each time the content script is run it can send a message, using runtime.sendMessage(), to the background script (received in a runtime.onMessage listener) telling the background script that the page has been loaded/reloaded.

If you're monitoring a limited number of domains, this would be preferable, as the warning the user gets upon installing the extension will only state that the extension will have access to the specified domains, instead of all domains.

这篇关于如何检查一个标签是否已经在background.js中重新加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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