33 lines
1.3 KiB
JavaScript
33 lines
1.3 KiB
JavaScript
.pragma library
|
|
|
|
function localUrl(value) {
|
|
if (typeof value !== "string" || value.length > 16384 || value.includes("\u0000"))
|
|
throw new Error("Invalid wallpaper path");
|
|
if (value === "") return "";
|
|
let path = value;
|
|
if (value.startsWith("file://")) {
|
|
try { path = decodeURIComponent(value.slice(7)); }
|
|
catch (e) { throw new Error("Invalid file URL"); }
|
|
}
|
|
if (!path.startsWith("/") || path.startsWith("//") || path.includes("\u0000"))
|
|
throw new Error("Choose an absolute local file path");
|
|
return "file://" + path.split("/").map(segment => encodeURIComponent(segment)).join("/");
|
|
}
|
|
|
|
function validate(wallpapers) {
|
|
if (!wallpapers || Array.isArray(wallpapers) || typeof wallpapers !== "object")
|
|
throw new Error("Invalid wallpaper preferences");
|
|
const outputs = Object.keys(wallpapers);
|
|
if (outputs.length > 64) throw new Error("Too many wallpaper overrides");
|
|
outputs.forEach(name => {
|
|
if (!name || name.length > 256) throw new Error("Invalid output name");
|
|
localUrl(wallpapers[name]);
|
|
});
|
|
return wallpapers;
|
|
}
|
|
|
|
function sourceFor(wallpapers, output) {
|
|
wallpapers = wallpapers || {};
|
|
return localUrl(Object.prototype.hasOwnProperty.call(wallpapers, output) ? wallpapers[output] : wallpapers["*"] || "");
|
|
}
|