Spaces:
Running
Running
File size: 10,803 Bytes
3d50167 6b7272b 3d50167 6b7272b 3d50167 6b7272b 3d50167 b0c5e01 3d50167 b0c5e01 3d50167 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
class KimiPluginManager {
constructor() {
this.plugins = [];
this.pluginsRoot = "kimi-plugins/";
}
// Common security validation for plugin file paths
isValidPluginPath(path) {
return (
typeof path === "string" &&
/^[-a-zA-Z0-9_\/.]+$/.test(path) &&
!path.startsWith("/") &&
!path.includes("..") &&
!/^https?:\/\//i.test(path) &&
path.startsWith("kimi-plugins/")
);
}
async loadPlugins() {
const pluginDirs = await this.getPluginDirs();
this.plugins = [];
let pluginThemeActive = false;
for (const dir of pluginDirs) {
try {
const manifest = await fetch(this.pluginsRoot + dir + "/manifest.json").then(r => r.json());
manifest._dir = dir;
manifest.enabled = this.isPluginEnabled(dir, manifest.enabled);
// Basic manifest validation and path sanitization (deny external or absolute URLs)
const validTypes = new Set(["theme", "voice", "behavior"]);
const isSafePath = p =>
typeof p === "string" &&
/^[-a-zA-Z0-9_\/.]+$/.test(p) &&
!p.startsWith("/") &&
!p.includes("..") &&
!/^https?:\/\//i.test(p);
if (!manifest.name || !manifest.type || !validTypes.has(manifest.type)) {
console.warn(`Invalid plugin manifest in ${dir}: missing name or invalid type`);
continue;
}
if (manifest.style && !isSafePath(manifest.style)) {
console.warn(`Blocked unsafe style path in ${dir}: ${manifest.style}`);
delete manifest.style;
}
if (manifest.main && !isSafePath(manifest.main)) {
console.warn(`Blocked unsafe main path in ${dir}: ${manifest.main}`);
delete manifest.main;
}
this.plugins.push(manifest);
if (manifest.enabled && manifest.style) {
this.loadCSS(this.pluginsRoot + dir + "/" + manifest.style);
}
if (manifest.enabled && manifest.main) {
this.loadJS(this.pluginsRoot + dir + "/" + manifest.main);
}
if (manifest.enabled && manifest.type === "theme" && dir === "sample-theme") {
pluginThemeActive = true;
}
} catch (e) {
console.warn("Failed loading plugin:", dir, e);
}
}
if (pluginThemeActive) {
document.documentElement.setAttribute("data-theme", "plugin-sample-theme");
} else {
// Restore previous or default theme depuis Dexie
if (window.kimiDB && window.kimiDB.getPreference) {
const userTheme = await window.kimiDB.getPreference("colorTheme", "dark");
document.documentElement.setAttribute("data-theme", userTheme);
} else {
document.documentElement.setAttribute("data-theme", "dark");
}
}
this.renderPluginList();
}
async getPluginDirs() {
return ["sample-theme", "sample-voice", "sample-behavior"];
}
loadCSS(href) {
if (!window.KimiDOMUtils) {
console.error("KimiDOMUtils not available for loadCSS");
return;
}
if (!window.KimiDOMUtils.get('link[href="' + href + '"]')) {
if (!this.isValidPluginPath(href)) {
console.error(`Blocked unsafe CSS path: ${href}`);
return;
}
const link = document.createElement("link");
link.rel = "stylesheet";
link.type = "text/css";
link.href = href;
link.onerror = function () {
console.error(`Failed to load plugin CSS: ${href}`);
};
document.head.appendChild(link);
}
}
loadJS(src) {
if (!window.KimiDOMUtils) {
console.error("KimiDOMUtils not available for loadJS");
return;
}
if (!window.KimiDOMUtils.get('script[src="' + src + '"]')) {
if (!this.isValidPluginPath(src)) {
console.error(`Blocked unsafe script path: ${src}`);
return;
}
const script = document.createElement("script");
script.src = src;
script.type = "text/javascript";
script.onerror = function () {
console.error(`Failed to load plugin script: ${src}`);
};
if (window.CSP_NONCE) {
script.nonce = window.CSP_NONCE;
}
document.body.appendChild(script);
}
}
renderPluginList() {
if (!window.KimiDOMUtils) {
console.error("KimiDOMUtils not available");
return;
}
const container = window.KimiDOMUtils.get("#plugin-list");
if (!container) return;
while (container.firstChild) {
container.removeChild(container.firstChild);
}
for (const plugin of this.plugins) {
const div = document.createElement("div");
div.className = "plugin-card";
// Left: info
const info = document.createElement("div");
info.className = "plugin-info";
const title = document.createElement("div");
title.className = "plugin-title";
title.textContent = plugin.name;
const type = document.createElement("span");
type.className = "plugin-type";
type.textContent = plugin.type;
title.appendChild(type);
const desc = document.createElement("div");
desc.className = "plugin-desc";
desc.textContent = plugin.description;
const author = document.createElement("div");
author.className = "plugin-author";
author.textContent = plugin.author;
info.appendChild(title);
info.appendChild(desc);
info.appendChild(author);
div.appendChild(info);
// Center: badges/swatch
const centerCol = document.createElement("div");
centerCol.className = "plugin-card-center";
const typeBadge = document.createElement("span");
typeBadge.className = "plugin-type-badge";
typeBadge.textContent =
plugin.type === "theme" ? "Theme" : plugin.type.charAt(0).toUpperCase() + plugin.type.slice(1);
centerCol.appendChild(typeBadge);
if (plugin.type === "theme") {
const swatch = document.createElement("div");
swatch.className = "plugin-theme-swatch";
// Create color spans safely
const colors = ["#3b82f6", "#a5b4fc", "#6366f1"];
colors.forEach(color => {
const span = document.createElement("span");
span.style.background = color;
swatch.appendChild(span);
});
centerCol.appendChild(swatch);
if (plugin.enabled) {
const activeBadge = document.createElement("span");
activeBadge.className = "plugin-active-badge";
activeBadge.textContent = "Active Theme";
centerCol.appendChild(activeBadge);
}
}
div.appendChild(centerCol);
// Right: switch
const rightCol = document.createElement("div");
rightCol.className = "plugin-card-switch";
const switchLabel = document.createElement("label");
switchLabel.className = "toggle-switch";
const input = document.createElement("input");
input.type = "checkbox";
input.checked = !!plugin.enabled;
input.style.display = "none";
input.addEventListener("change", () => {
plugin.enabled = input.checked;
this.savePluginState(plugin._dir, plugin.enabled);
this.loadPlugins();
if (input.checked) {
switchLabel.classList.add("active");
} else {
switchLabel.classList.remove("active");
}
});
const slider = document.createElement("span");
slider.className = "slider";
switchLabel.appendChild(input);
switchLabel.appendChild(slider);
if (input.checked) switchLabel.classList.add("active");
rightCol.appendChild(switchLabel);
div.appendChild(rightCol);
container.appendChild(div);
}
}
savePluginState(dir, enabled) {
const key = "kimi-plugin-enabled-" + dir;
localStorage.setItem(key, enabled ? "1" : "0");
}
isPluginEnabled(dir, defaultValue) {
const key = "kimi-plugin-enabled-" + dir;
const val = localStorage.getItem(key);
if (val === null) return defaultValue;
return val === "1";
}
}
window.KimiPluginManager = new KimiPluginManager();
document.addEventListener("DOMContentLoaded", () => {
if (window.KimiPluginManager) window.KimiPluginManager.loadPlugins();
const refreshBtn = document.getElementById("refresh-plugins");
if (refreshBtn) {
refreshBtn.onclick = async () => {
const originalText = refreshBtn.innerHTML;
refreshBtn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> Refreshing...';
refreshBtn.disabled = true;
try {
await window.KimiPluginManager.loadPlugins();
refreshBtn.innerHTML = '<i class="fas fa-check"></i> Refreshed!';
setTimeout(() => {
refreshBtn.innerHTML = originalText;
refreshBtn.disabled = false;
}, 1500);
} catch (error) {
console.error("Error refreshing plugins:", error);
refreshBtn.innerHTML = '<i class="fas fa-exclamation-triangle"></i> Error';
setTimeout(() => {
refreshBtn.innerHTML = originalText;
refreshBtn.disabled = false;
}, 2000);
}
};
}
});
|