From 8a80fbe14c2534796964e6360cdc7eb9d7418747 Mon Sep 17 00:00:00 2001 From: Mihai Branescu Date: Thu, 23 Apr 2020 23:19:17 +0300 Subject: [PATCH] For #6558 - change from modifying event to event listener The initial design replaced the onload method, which was used by others such as Bing. With listeners it's additive, we do not replace anything. --- app/src/main/assets/extensions/ads/ads.js | 30 ++++++++++++++--------- 1 file changed, 19 insertions(+), 11 deletions(-) diff --git a/app/src/main/assets/extensions/ads/ads.js b/app/src/main/assets/extensions/ads/ads.js index 911fbe48a..152994f93 100644 --- a/app/src/main/assets/extensions/ads/ads.js +++ b/app/src/main/assets/extensions/ads/ads.js @@ -2,7 +2,7 @@ * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ - const ADLINK_CHECK_TIMEOUT_MS = 1000; +const ADLINK_CHECK_TIMEOUT_MS = 1000; function collectLinks(urls) { let anchors = document.getElementsByTagName("a"); @@ -25,18 +25,26 @@ function sendLinks() { browser.runtime.sendNativeMessage("MozacBrowserAds", message); } +const events = ["pageshow", "load", "unload"]; var timeout; -window.onload = function() { - timeout = setTimeout(sendLinks, ADLINK_CHECK_TIMEOUT_MS); -}; - -window.onpageshow = function(event) { - if (event.persisted) { +const eventLogger = event => { + switch (event.type) { + case "load": timeout = setTimeout(sendLinks, ADLINK_CHECK_TIMEOUT_MS); - } + break; + case "pageshow": + if (event.persisted) { + timeout = setTimeout(sendLinks, ADLINK_CHECK_TIMEOUT_MS); + } + break; + case "unload": + clearTimeout(timeout); + default: + console.log('Event:', event.type); + } }; -window.onunload = function() { - clearTimeout(timeout); -}; +events.forEach(eventName => + window.addEventListener(eventName, eventLogger) +);