dynamic change utterances theme with coder theme (#676)

* dynamic change utterances theme with coder theme

* add myself into CONTRIBUTORS.md
This commit is contained in:
Zhan Shuo
2022-05-30 02:38:59 +08:00
committed by GitHub
parent 40eeed8256
commit 9ea82c5c82
3 changed files with 63 additions and 8 deletions

View File

@@ -34,6 +34,48 @@ function setTheme(theme) {
body.classList.remove('colorscheme-' + inverse);
body.classList.add('colorscheme-' + theme);
document.documentElement.style['color-scheme'] = theme;
function waitForElm(selector) {
return new Promise(resolve => {
if (document.querySelector(selector)) {
return resolve(document.querySelector(selector));
}
const observer = new MutationObserver(mutations => {
if (document.querySelector(selector)) {
resolve(document.querySelector(selector));
observer.disconnect();
}
});
observer.observe(document.body, {
childList: true,
subtree: true
});
});
}
if (theme === 'dark') {
const message = {
type: 'set-theme',
theme: 'github-dark'
};
waitForElm('.utterances-frame').then((iframe) => {
iframe.contentWindow.postMessage(message, 'https://utteranc.es');
})
}
else {
const message = {
type: 'set-theme',
theme: 'github-light'
};
waitForElm('.utterances-frame').then((iframe) => {
iframe.contentWindow.postMessage(message, 'https://utteranc.es');
})
}
}
function rememberTheme(theme) {