{"id":78,"date":"2008-09-15T23:44:55","date_gmt":"2008-09-16T04:44:55","guid":{"rendered":"http:\/\/www.timrosenblatt.com\/blog\/2008\/09\/15\/detecting-page-loads-in-a-firefox-extension\/"},"modified":"2008-09-23T07:01:45","modified_gmt":"2008-09-23T12:01:45","slug":"detecting-page-loads-in-a-firefox-extension","status":"publish","type":"post","link":"http:\/\/www.timrosenblatt.com\/blog\/2008\/09\/15\/detecting-page-loads-in-a-firefox-extension\/","title":{"rendered":"Detecting page loads in a Firefox Extension"},"content":{"rendered":"<p>Detecting page loads is a useful ability. It&#8217;s easy enough to throw in a<\/p>\n<blockquote><p>gBrowser.addEventListener(&#8220;load&#8221;, function_name, true);<\/p><\/blockquote>\n<p>But, it causes extra events to get generated. There&#8217;s load events occurring in the browser that aren&#8217;t page loads. The trick to detecting real page loads (but not Back\/Forward navigation when the page is still in memory &#8212; only page loads that have a server hit) is to look at the event&#8217;s target. In this example, &#8220;event&#8221; has been passed into the function specified in the addEventListener call above.<\/p>\n<blockquote><p>if(event.originalTarget instanceof HTMLDocument) {<\/p><\/blockquote>\n<p>will make sure that the load event came from a web page being loaded. You can even go a step further and filter out different types of HTMLDocuments.<\/p>\n<blockquote><p>if(event.originalTarget.defaultView.frameElement) {<\/p><\/blockquote>\n<p>will catch events that were triggered as the result of a frame or iframe. It&#8217;s an important step, because lots of advertising networks embed their ads in iframes, and you probably are more interested in the actual web page than the ads on that page.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Detecting page loads is a useful ability. It&#8217;s easy enough to throw in a gBrowser.addEventListener(&#8220;load&#8221;, function_name, true); But, it causes extra events to get generated. There&#8217;s load events occurring in the browser that aren&#8217;t page loads. The trick to detecting real page loads (but not Back\/Forward navigation when the page is still in memory &#8212; &hellip; <a href=\"http:\/\/www.timrosenblatt.com\/blog\/2008\/09\/15\/detecting-page-loads-in-a-firefox-extension\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Detecting page loads in a Firefox Extension&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[145,236],"tags":[241,238,239,60,240,237],"_links":{"self":[{"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/posts\/78"}],"collection":[{"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/comments?post=78"}],"version-history":[{"count":0,"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/posts\/78\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/media?parent=78"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/categories?post=78"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.timrosenblatt.com\/blog\/wp-json\/wp\/v2\/tags?post=78"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}