2019-05-02 00:30:30 +02:00
|
|
|
/* 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");
|
2019-05-02 00:30:30 +02:00
|
|
|
|
|
|
|
scope.init = function(page){
|
2021-11-01 13:18:38 +01:00
|
|
|
const basePath = extension.getURL("themes");
|
2019-05-02 23:46:20 +02:00
|
|
|
|
2019-11-28 01:26:35 +01:00
|
|
|
const baseLink = document.createElement("link");
|
2019-05-02 23:46:20 +02:00
|
|
|
baseLink.href = `${basePath}/base/layout.css`;
|
2019-05-03 23:17:35 +02:00
|
|
|
baseLink.rel = "stylesheet";
|
2019-05-02 23:46:20 +02:00
|
|
|
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");
|
2019-05-02 23:46:20 +02:00
|
|
|
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`;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-02 00:30:30 +02:00
|
|
|
settings.onloaded(function(){
|
2019-05-02 23:46:20 +02:00
|
|
|
setTheme(settings.theme);
|
2019-05-02 00:30:30 +02:00
|
|
|
settings.on("theme", function(){
|
2019-05-02 23:46:20 +02:00
|
|
|
setTheme(settings.theme);
|
2019-05-02 00:30:30 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
}());
|