2
0
mirror of https://github.com/fork-maintainers/iceraven-browser synced 2024-11-03 23:15:31 +00:00

[fenix] For https://github.com/mozilla-mobile/fenix/issues/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.
This commit is contained in:
Mihai Branescu 2020-04-23 23:19:17 +03:00 committed by Jeff Boek
parent 431f3f989f
commit 5ed60d32d3

View File

@ -2,7 +2,7 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this * 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/. */ * 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) { function collectLinks(urls) {
let anchors = document.getElementsByTagName("a"); let anchors = document.getElementsByTagName("a");
@ -25,18 +25,26 @@ function sendLinks() {
browser.runtime.sendNativeMessage("MozacBrowserAds", message); browser.runtime.sendNativeMessage("MozacBrowserAds", message);
} }
const events = ["pageshow", "load", "unload"];
var timeout; var timeout;
window.onload = function() { const eventLogger = event => {
timeout = setTimeout(sendLinks, ADLINK_CHECK_TIMEOUT_MS); switch (event.type) {
}; case "load":
window.onpageshow = function(event) {
if (event.persisted) {
timeout = setTimeout(sendLinks, ADLINK_CHECK_TIMEOUT_MS); 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() { events.forEach(eventName =>
clearTimeout(timeout); window.addEventListener(eventName, eventLogger)
}; );