Skip to content

Commit

Permalink
confluence-space-avatar-favicons: better loading listener
Browse files Browse the repository at this point in the history
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.

<https://stackoverflow.com/a/36096571/1083697>

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 <https://youmightnotneedjquery.com>.
  • Loading branch information
rybak committed Feb 14, 2024
1 parent a1c3d26 commit d836b45
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions confluence-space-avatar-favicons.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -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==
Expand All @@ -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"]');
Expand All @@ -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);
}
})();

0 comments on commit d836b45

Please sign in to comment.