Last active
January 7, 2020 19:30
-
-
Save kuceb/8f07c33a07066d3ba3f903c35bf8918a to your computer and use it in GitHub Desktop.
tampermonkey/greasemonkey script that inverts favicons for use in dark desktop themes. Just have it run on sites you'd like to invert favicons for.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
const getFavicon = () => { | |
return ( | |
document.querySelector('link[href*=".ico"]') | |
|| document.querySelector('link[href*=".png"]') | |
) | |
} | |
const changeFavicon = async function () { | |
const faviconEl = getFavicon() | |
if (faviconEl.href.includes('data:')) { | |
return | |
} | |
const img = new Image() | |
img.crossOrigin = 'Anonymous' | |
await new Promise((resolve) => { | |
img.src = faviconEl.href | |
img.onload = resolve | |
}) | |
const canvas = document.createElement('canvas') | |
//canvas.style.display = 'none' | |
//document.body.appendChild(canvas) | |
canvas.width = img.width | |
canvas.height = img.height | |
const ctx = canvas.getContext('2d') | |
ctx.filter = 'invert(1) hue-rotate(180deg) saturate(2)' | |
ctx.drawImage(img, 0, 0) | |
const dataUrl = canvas.toDataURL() | |
faviconEl.href = dataUrl | |
} | |
const favEl = getFavicon() | |
const mo = new MutationObserver(changeFavicon) | |
mo.observe(favEl, { attributes: true }) | |
changeFavicon() | |
})() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment