Repository navigation
Expand file tree
/
Copy pathsof-custom.js
More file actions
46 lines (41 loc) · 1.72 KB
/
Copy pathsof-custom.js
File metadata and controls
46 lines (41 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
// Dynamic Favicon switcher for SOF documentation
(function() {
function updateFavicon() {
var theme = document.documentElement.dataset.theme;
var prefersDark = window.matchMedia && window.matchMedia('(prefers-color-scheme: dark)').matches;
var isDark = theme === 'dark' || (theme === 'auto' && prefersDark) || (!theme && prefersDark);
var links = document.querySelectorAll('link[rel="icon"], link[rel="shortcut icon"]');
links.forEach(function(link) {
var href = link.getAttribute('href') || '';
if (href.indexOf('sof-favicon') !== -1) {
var dir = href.substring(0, href.lastIndexOf('/') + 1);
if (theme === 'dark') {
link.href = dir + 'sof-favicon-dark.png';
} else if (theme === 'light') {
link.href = dir + 'sof-favicon-light.png';
} else {
link.href = dir + (isDark ? 'sof-favicon-dark.png' : 'sof-favicon.svg');
}
}
});
}
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', updateFavicon);
} else {
updateFavicon();
}
var observer = new MutationObserver(function(mutations) {
mutations.forEach(function(mutation) {
if (mutation.type === 'attributes' && mutation.attributeName === 'data-theme') {
updateFavicon();
}
});
});
observer.observe(document.documentElement, {
attributes: true,
attributeFilter: ['data-theme']
});
if (window.matchMedia) {
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', updateFavicon);
}
})();