mirror of
https://github.com/ClovertaTheTrilobita/SanYeCao-blog.git
synced 2026-07-03 23:51:26 +00:00
update darkmode
This commit is contained in:
parent
eaad8a3080
commit
c8fbea7b07
1 changed files with 45 additions and 17 deletions
|
|
@ -5,6 +5,16 @@
|
||||||
|
|
||||||
let pagePath = `/posts/${slug}/`;
|
let pagePath = `/posts/${slug}/`;
|
||||||
|
|
||||||
|
function getTheme() {
|
||||||
|
return localStorage.getItem("color-theme") === "dark" ? "dark" : "light";
|
||||||
|
}
|
||||||
|
|
||||||
|
function syncRemark42Theme() {
|
||||||
|
if (window.REMARK42?.changeTheme) {
|
||||||
|
window.REMARK42.changeTheme(getTheme());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
onMount(() => {
|
onMount(() => {
|
||||||
const scriptId = "remark42-script";
|
const scriptId = "remark42-script";
|
||||||
|
|
||||||
|
|
@ -13,28 +23,46 @@
|
||||||
site_id: "cloverta-blog",
|
site_id: "cloverta-blog",
|
||||||
components: ["embed"],
|
components: ["embed"],
|
||||||
show_rss_subscription: false,
|
show_rss_subscription: false,
|
||||||
theme: localStorage.getItem("color-theme") ?? "light",
|
theme: getTheme(),
|
||||||
url: pagePath,
|
url: pagePath,
|
||||||
page_id: pagePath,
|
page_id: pagePath,
|
||||||
};
|
};
|
||||||
|
|
||||||
if (document.getElementById(scriptId)) return;
|
if (!document.getElementById(scriptId)) {
|
||||||
|
const script = document.createElement("script");
|
||||||
|
script.id = scriptId;
|
||||||
|
script.async = true;
|
||||||
|
script.innerHTML = `
|
||||||
|
!function(e,n){
|
||||||
|
for(var o=0;o<e.length;o++){
|
||||||
|
var r=n.createElement("script"),c=".js",d=n.head||n.body;
|
||||||
|
"noModule"in r?(r.type="module",c=".mjs"):r.async=!0;
|
||||||
|
r.defer=!0;
|
||||||
|
r.src=window.remark_config.host+"/web/"+e[o]+c;
|
||||||
|
d.appendChild(r);
|
||||||
|
}
|
||||||
|
}(window.remark_config.components||["embed"],document);
|
||||||
|
`;
|
||||||
|
document.body.appendChild(script);
|
||||||
|
}
|
||||||
|
|
||||||
const script = document.createElement("script");
|
syncRemark42Theme();
|
||||||
script.id = scriptId;
|
|
||||||
script.async = true;
|
const observer = new MutationObserver(() => {
|
||||||
script.innerHTML = `
|
syncRemark42Theme();
|
||||||
!function(e,n){
|
});
|
||||||
for(var o=0;o<e.length;o++){
|
|
||||||
var r=n.createElement("script"),c=".js",d=n.head||n.body;
|
observer.observe(document.documentElement, {
|
||||||
"noModule"in r?(r.type="module",c=".mjs"):r.async=!0;
|
attributes: true,
|
||||||
r.defer=!0;
|
attributeFilter: ["class"],
|
||||||
r.src=window.remark_config.host+"/web/"+e[o]+c;
|
});
|
||||||
d.appendChild(r);
|
|
||||||
}
|
window.addEventListener("storage", syncRemark42Theme);
|
||||||
}(window.remark_config.components||["embed"],document);
|
|
||||||
`;
|
return () => {
|
||||||
document.body.appendChild(script);
|
observer.disconnect();
|
||||||
|
window.removeEventListener("storage", syncRemark42Theme);
|
||||||
|
};
|
||||||
});
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue