Skip to content

Instantly share code, notes, and snippets.

@andrejilderda
Created May 6, 2022 17:29
Show Gist options
  • Save andrejilderda/f6673f1b1a986b2dc0cd01607acbff26 to your computer and use it in GitHub Desktop.
Save andrejilderda/f6673f1b1a986b2dc0cd01607acbff26 to your computer and use it in GitHub Desktop.
function onVisibilityChange(callback) {
let visible = true;
const focused = () => {
if (!visible) callback((visible = true));
}
const unfocused = () => {
if (visible) callback((visible = false));
}
window.onfocus = focused;
window.onblur = unfocused;
}
function updateCSSOnFocus(url) {
function handleFocusChange (_visible) {
const $injectedCSS = document.querySelector('#injectedCSS');
if ($injectedCSS) {
return $injectedCSS.href=`${url}?last-reload=${Date.now()}`
}
else {
document.body.insertAdjacentHTML(
'beforeend',
`<link rel="stylesheet" id="injectedCSS" href="${url}">`
);
}
}
onVisibilityChange(handleFocusChange);
handleFocusChange();
}
updateCSSOnFocus('http://localhost:8080/macos-theme-for-joplin.css');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment