From d836b4554cb879e75c331379e706e4027a1e9c26 Mon Sep 17 00:00:00 2001 From: Andrei Rybak Date: Wed, 14 Feb 2024 21:39:12 +0100 Subject: [PATCH] confluence-space-avatar-favicons: better loading listener The listener for 'load' event seems to not be doing its job for some reason, and it might be better to avoid it. Quote from Stack Overflow: "load" A very different event, **load**, should only be used to detect a *fully-loaded page*. It is an incredibly popular mistake to use load where DOMContentLoaded would be much more appropriate, so be cautious. Rewrite the loading listener of confluence-space-avatar-favicons.user.js to use a combination of checking document.readyState and a listener for DOMContentLoaded event, as per recommendation from Stack Overflow. This combination is a recommended replacement for jQuery's $(document).ready from . --- confluence-space-avatar-favicons.user.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/confluence-space-avatar-favicons.user.js b/confluence-space-avatar-favicons.user.js index 52c0673..7f5e9ed 100644 --- a/confluence-space-avatar-favicons.user.js +++ b/confluence-space-avatar-favicons.user.js @@ -7,7 +7,7 @@ // @include https://*confluence*/* // @match https://confluence.example.com/* // @icon https://wac-cdn-2.atlassian.com/image/upload/f_auto,q_auto/dam/jcr:5d1374c2-276f-4bca-9ce4-813aba614b7a/confluence-icon-gradient-blue.svg?cdnVersion=691 -// @version 3 +// @version 4 // @license MIT // @grant none // ==/UserScript== @@ -21,6 +21,10 @@ console.log(LOG_PREFIX, ...toLog); } + function warn(...toLog) { + console.warn(LOG_PREFIX, ...toLog); + } + function changeFavicon() { log("Trying..."); const avatar = document.querySelector(".avatar-img") || document.querySelector('img[data-testid="space-icon"]'); @@ -42,7 +46,11 @@ } } - window.addEventListener('load', function() { + // https://youmightnotneedjquery.com/#ready + if (document.readyState !== 'loading') { changeFavicon(); - }, false); + } else { + warn('Cannot load yet. Setting up a listener...'); + document.addEventListener('DOMContentLoaded', changeFavicon); + } })();