1
0
mirror of https://github.com/kkapsner/CanvasBlocker synced 2024-11-10 23:28:51 +01:00
CanvasBlocker/lib/theme.js

69 lines
1.7 KiB
JavaScript
Raw Normal View History

/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
(function(){
"use strict";
let scope;
if ((typeof exports) !== "undefined"){
scope = exports;
}
else {
scope = require.register("./theme", {});
}
const settings = require("./settings");
2021-11-01 13:18:38 +01:00
const extension = require("./extension");
scope.init = function(page){
2021-11-01 13:18:38 +01:00
const basePath = extension.getURL("themes");
2019-11-28 01:26:35 +01:00
const baseLink = document.createElement("link");
baseLink.href = `${basePath}/base/layout.css`;
baseLink.rel = "stylesheet";
baseLink.type = "text/css";
document.head.appendChild(baseLink);
const links = ["layout", page].filter(function(file){
return file;
}).map(function(file){
2019-11-28 01:26:35 +01:00
const link = document.createElement("link");
link.cbFile = file;
link.rel = "alternative";
link.type = "text/css";
document.head.appendChild(link);
return link;
});
function setTheme(theme){
switch (theme){
case "none":
baseLink.rel = "alternative";
links.forEach(function(link){
link.rel = "alternative";
});
break;
case "auto":
if (window.matchMedia("(prefers-color-scheme: dark)").matches){
theme = "dark";
}
else {
theme = "default";
}
// fall through
default:
baseLink.rel = "stylesheet";
links.forEach(function(link){
link.rel = "stylesheet";
link.href = `${basePath}/${theme}/${link.cbFile}.css`;
});
}
}
settings.onloaded(function(){
setTheme(settings.theme);
settings.on("theme", function(){
setTheme(settings.theme);
});
});
};
}());