+ btn = document.createElement('li');
+ btn.className = 'nav-item';
+ btn.id = 'prof-settings-btn';
+ btn.setAttribute('data-prof-plugin', 'settings');
+ btn.style.cssText = 'cursor:pointer;user-select:none;';
+ btnInner = document.createElement('span');
+ btnInner.className = 'nav-link';
+ btnInner.style.cssText = 'font-size:16px;padding-top:6px;';
+ btnInner.textContent = '⚙';
+ btn.appendChild(btnInner);
+ } else {
+ // pprof UI header: inject as a .menu-item div
+ btn = document.createElement('div');
+ btn.className = 'menu-item';
+ btn.id = 'prof-settings-btn';
+ btn.setAttribute('data-prof-plugin', 'settings');
+ btn.style.cssText = 'cursor:pointer;padding:0 8px;user-select:none;';
+ btn.innerHTML = '';
+ }
+
+ // ── Settings panel (hidden by default) ───────────────────────────────────
+ var panel = document.createElement('div');
+ panel.id = 'prof-settings-panel';
+ panel.setAttribute('data-prof-plugin', 'settings');
+ panel.style.cssText = [
+ 'display:none',
+ 'position:fixed',
+ 'top:48px',
+ 'right:12px',
+ 'background:#fff',
+ 'border:1px solid #ddd',
+ 'border-radius:4px',
+ 'box-shadow:0 2px 8px rgba(0,0,0,.12)',
+ 'padding:12px 16px',
+ 'min-width:220px',
+ 'z-index:9999',
+ 'font-size:13px',
+ 'color:#333',
+ 'line-height:1.4',
+ ].join(';');
+
+ fetchAndRender(panel);
+
+ btn.addEventListener('click', function (e) {
+ e.stopPropagation();
+ panel.style.display = panel.style.display === 'none' ? 'block' : 'none';
+ });
+
+ document.addEventListener('click', function () {
+ panel.style.display = 'none';
+ });
+
+ container.appendChild(btn);
+ document.body.appendChild(panel);
+ }
+
+ // ── Fetch /api/plugins and render toggle list ────────────────────────────
+ function fetchAndRender(panel) {
+ var xhr = new XMLHttpRequest();
+ xhr.open('GET', '/api/plugins');
+ xhr.onload = function () {
+ if (xhr.status !== 200) return;
+ try {
+ var data = JSON.parse(xhr.responseText);
+ renderPanel(panel, data.plugins || []);
+ } catch (e) {
+ console.warn('[settings] failed to parse /api/plugins:', e);
+ }
+ };
+ xhr.onerror = function () {
+ console.warn('[settings] /api/plugins request failed');
+ };
+ xhr.send();
+ }
+
+ function renderPanel(panel, plugins) {
+ panel.innerHTML = '' + st('settings.title', 'Prof 插件') + '
';
+
+ if (plugins.length === 0) {
+ var empty = document.createElement('div');
+ empty.style.cssText = 'color:#999;font-size:12px;';
+ empty.textContent = st('settings.noPlugins', '暂无插件');
+ panel.appendChild(empty);
+ return;
+ }
+
+ plugins.forEach(function (p) {
+ var lsKey = 'prof-plugin-' + p.name;
+ var lsVal = localStorage.getItem(lsKey);
+ var isOn = lsVal !== null ? lsVal === 'true' : p.enabled;
+
+ var row = document.createElement('div');
+ row.style.cssText = 'display:flex;justify-content:space-between;align-items:center;margin:6px 0;gap:12px;';
+
+ var label = document.createElement('span');
+ label.textContent = st('plugin.' + p.name, p.description || p.name);
+ label.style.cssText = 'flex:1;';
+
+ var toggle = document.createElement('input');
+ toggle.type = 'checkbox';
+ toggle.checked = isOn;
+ toggle.style.cssText = 'cursor:pointer;width:16px;height:16px;flex-shrink:0;';
+
+ row.appendChild(label);
+ row.appendChild(toggle);
+ panel.appendChild(row);
+
+ // 子配置区域
+ if (p.name === 'source-fold') {
+ var subConfig = renderSourceFoldConfig();
+ subConfig.style.display = isOn ? 'block' : 'none';
+ panel.appendChild(subConfig);
+
+ toggle.addEventListener('change', function () {
+ localStorage.setItem(lsKey, toggle.checked ? 'true' : 'false');
+ subConfig.style.display = toggle.checked ? 'block' : 'none';
+ showReloadHint(panel);
+ });
+ } else if (p.name === 'peek-fold') {
+ var peekSubConfig = renderPeekFoldConfig();
+ peekSubConfig.style.display = isOn ? 'block' : 'none';
+ panel.appendChild(peekSubConfig);
+
+ toggle.addEventListener('change', function () {
+ localStorage.setItem(lsKey, toggle.checked ? 'true' : 'false');
+ peekSubConfig.style.display = toggle.checked ? 'block' : 'none';
+ showReloadHint(panel);
+ });
+ } else {
+ toggle.addEventListener('change', function () {
+ localStorage.setItem(lsKey, toggle.checked ? 'true' : 'false');
+ showReloadHint(panel);
+ });
+ }
+ });
+ }
+
+ // source-fold 子配置:标准库 / 第三方 / 全部
+ function renderSourceFoldConfig() {
+ var configKey = 'prof-source-fold-config';
+ var cfg = { collapseStdlib: true, collapseThirdParty: false, collapseAll: false };
+ try {
+ var raw = localStorage.getItem(configKey);
+ if (raw) cfg = Object.assign({ collapseStdlib: true, collapseThirdParty: false, collapseAll: false }, JSON.parse(raw));
+ } catch (e) {}
+
+ var container = document.createElement('div');
+ container.style.cssText = [
+ 'margin:2px 0 6px 12px',
+ 'padding:6px 10px',
+ 'background:#f9f9f9',
+ 'border-left:3px solid #ddd',
+ 'font-size:12px',
+ ].join(';');
+
+ function makeSubRow(labelText, checked, onChange) {
+ var row = document.createElement('label');
+ row.style.cssText = 'display:flex;align-items:center;gap:6px;margin:3px 0;cursor:pointer;';
+ var cb = document.createElement('input');
+ cb.type = 'checkbox';
+ cb.checked = checked;
+ cb.style.cssText = 'cursor:pointer;';
+ cb.addEventListener('change', function () { onChange(cb.checked); });
+ var txt = document.createElement('span');
+ txt.textContent = labelText;
+ row.appendChild(cb);
+ row.appendChild(txt);
+ return row;
+ }
+
+ function saveConfig() {
+ localStorage.setItem(configKey, JSON.stringify(cfg));
+ }
+
+ container.appendChild(makeSubRow(st('source-fold.collapseStdlib', '折叠 std(标准库)'), cfg.collapseStdlib, function (v) {
+ cfg.collapseStdlib = v;
+ saveConfig();
+ }));
+ container.appendChild(makeSubRow(st('source-fold.collapseThirdParty', '折叠 mod(第三方)'), cfg.collapseThirdParty, function (v) {
+ cfg.collapseThirdParty = v;
+ saveConfig();
+ }));
+ container.appendChild(makeSubRow(st('source-fold.collapseAll', '折叠全部'), cfg.collapseAll, function (v) {
+ cfg.collapseAll = v;
+ saveConfig();
+ }));
+
+ return container;
+ }
+
+ // peek-fold 子配置:折叠 std/mod/全部 + 路径缩写
+ function renderPeekFoldConfig() {
+ var configKey = 'prof-peek-fold-config';
+ var cfg = { collapseStd: true, collapseMod: false, collapseAll: false, shortenPaths: true };
+ try {
+ var raw = localStorage.getItem(configKey);
+ if (raw) cfg = Object.assign({ collapseStd: true, collapseMod: false, collapseAll: false, shortenPaths: true }, JSON.parse(raw));
+ } catch (e) {}
+
+ var container = document.createElement('div');
+ container.style.cssText = [
+ 'margin:2px 0 6px 12px',
+ 'padding:6px 10px',
+ 'background:#f9f9f9',
+ 'border-left:3px solid #ddd',
+ 'font-size:12px',
+ ].join(';');
+
+ function makeSubRow(labelText, checked, onChange) {
+ var row = document.createElement('label');
+ row.style.cssText = 'display:flex;align-items:center;gap:6px;margin:3px 0;cursor:pointer;';
+ var cb = document.createElement('input');
+ cb.type = 'checkbox';
+ cb.checked = checked;
+ cb.style.cssText = 'cursor:pointer;';
+ cb.addEventListener('change', function () { onChange(cb.checked); });
+ var txt = document.createElement('span');
+ txt.textContent = labelText;
+ row.appendChild(cb);
+ row.appendChild(txt);
+ return row;
+ }
+
+ function saveConfig() {
+ localStorage.setItem(configKey, JSON.stringify(cfg));
+ }
+
+ container.appendChild(makeSubRow(st('peek-fold.collapseStd', '折叠 std(标准库)'), cfg.collapseStd, function (v) {
+ cfg.collapseStd = v;
+ saveConfig();
+ }));
+ container.appendChild(makeSubRow(st('peek-fold.collapseMod', '折叠 mod(第三方)'), cfg.collapseMod, function (v) {
+ cfg.collapseMod = v;
+ saveConfig();
+ }));
+ container.appendChild(makeSubRow(st('peek-fold.collapseAll', '折叠全部'), cfg.collapseAll, function (v) {
+ cfg.collapseAll = v;
+ saveConfig();
+ }));
+ container.appendChild(makeSubRow(st('peek-fold.shortenPaths', '缩短路径显示(std/ mod/)'), cfg.shortenPaths, function (v) {
+ cfg.shortenPaths = v;
+ saveConfig();
+ }));
+
+ return container;
+ }
+
+ function showReloadHint(panel) {
+ if (panel.querySelector('#prof-reload-hint')) return;
+ var hint = document.createElement('div');
+ hint.id = 'prof-reload-hint';
+ hint.style.cssText = [
+ 'margin-top:10px',
+ 'padding-top:8px',
+ 'border-top:1px solid #eee',
+ 'color:#1565c0',
+ 'font-size:12px',
+ 'cursor:pointer',
+ 'text-align:center',
+ ].join(';');
+ hint.textContent = st('settings.reload', '点击刷新生效');
+ hint.addEventListener('click', function () { location.reload(); });
+ panel.appendChild(hint);
+ }
+
+ if (document.readyState === 'loading') {
+ document.addEventListener('DOMContentLoaded', init);
+ } else {
+ init();
+ }
+})();
diff --git a/web/static/plugins/source-fold.js b/web/static/plugins/source-fold.js
new file mode 100644
index 0000000..ce06115
--- /dev/null
+++ b/web/static/plugins/source-fold.js
@@ -0,0 +1,195 @@
+(function (P) {
+ 'use strict';
+
+ // 从 localStorage 读取默认折叠配置
+ function loadConfig() {
+ var defaults = { collapseStdlib: true, collapseThirdParty: false, collapseAll: false };
+ try {
+ var raw = localStorage.getItem('prof-source-fold-config');
+ if (raw) return Object.assign(defaults, JSON.parse(raw));
+ } catch (e) {}
+ return defaults;
+ }
+
+ // 扫描所有 发现根目录
+ function discoverSourceRoots(content) {
+ var roots = { std: null, mod: null };
+ var ps = content.querySelectorAll('p.filename');
+ for (var i = 0; i < ps.length; i++) {
+ var filePath = ps[i].textContent.trim();
+ var h2 = ps[i].previousElementSibling;
+ var funcName = h2 ? h2.textContent.trim() : '';
+
+ var modIdx = filePath.indexOf('/pkg/mod/');
+ if (modIdx !== -1 && !roots.mod) {
+ roots.mod = filePath.slice(0, modIdx + '/pkg/mod/'.length);
+ }
+ if (!roots.std && modIdx === -1) {
+ var pkg = funcName.replace(/\(\*?[^)]+\)\.[^.]+$/, '').replace(/\.[^./]+$/, '');
+ var firstPart = pkg.split('/')[0];
+ if (pkg && firstPart.indexOf('.') === -1) {
+ var pkgIdx = filePath.indexOf('/' + firstPart + '/');
+ if (pkgIdx !== -1) roots.std = filePath.slice(0, pkgIdx + 1);
+ }
+ }
+ if (roots.std && roots.mod) break;
+ }
+ return roots;
+ }
+
+ // 根据路径分类:stdlib / thirdparty / local / unknown
+ function classifyPath(filePath, roots) {
+ if (!filePath) return 'unknown';
+ if (roots.mod && filePath.startsWith(roots.mod)) return 'thirdparty';
+ if (roots.std && filePath.startsWith(roots.std)) return 'stdlib';
+ return filePath.indexOf('/pkg/mod/') !== -1 ? 'thirdparty' : 'local';
+ }
+
+ // 找 h2 后面的 p.filename + pre(折叠体,连续兄弟节点)
+ function getFoldBody(h2) {
+ var nodes = [];
+ var el = h2.nextElementSibling;
+ while (el && (el.tagName === 'P' || el.tagName === 'PRE')) {
+ nodes.push(el);
+ el = el.nextElementSibling;
+ }
+ return nodes;
+ }
+
+ // 判断是否默认折叠
+ function shouldDefaultFold(name, filePath, roots, config) {
+ if (config.collapseAll) return true;
+ var category = classifyPath(filePath, roots);
+ if (config.collapseStdlib && category === 'stdlib') return true;
+ if (config.collapseThirdParty && category === 'thirdparty') return true;
+ return false;
+ }
+
+ function isFolded(h2) {
+ return h2.getAttribute('data-folded') === 'true';
+ }
+
+ // 切换折叠状态(不 wrap DOM,用 display 控制)
+ function setFolded(h2, folded) {
+ var body = getFoldBody(h2);
+ body.forEach(function (el) {
+ el.style.display = folded ? 'none' : '';
+ });
+ h2.setAttribute('data-folded', folded ? 'true' : 'false');
+ var indicator = h2.querySelector('.prof-fold-indicator');
+ if (indicator) indicator.textContent = folded ? '▶' : '▼';
+ }
+
+ // 给 h2 加折叠指示器和点击事件
+ function decorateH2(h2, folded) {
+ h2.setAttribute('data-prof-plugin', 'source-fold');
+ h2.setAttribute('data-folded', folded ? 'true' : 'false');
+ h2.style.cursor = 'pointer';
+
+ var indicator = document.createElement('span');
+ indicator.className = 'prof-fold-indicator';
+ indicator.setAttribute('data-prof-plugin', 'source-fold');
+ indicator.style.cssText = 'margin-right:8px;font-size:12px;color:#888;user-select:none;';
+ indicator.textContent = folded ? '▶' : '▼';
+ h2.insertBefore(indicator, h2.firstChild);
+
+ h2.addEventListener('click', function () {
+ setFolded(h2, !isFolded(h2));
+ });
+
+ // 应用初始折叠状态
+ if (folded) {
+ getFoldBody(h2).forEach(function (el) {
+ el.style.display = 'none';
+ });
+ }
+ }
+
+ // 注入「全部折叠」「全部展开」工具栏
+ function injectToolbar(content, h2List) {
+ var toolbar = document.createElement('div');
+ toolbar.setAttribute('data-prof-plugin', 'source-fold');
+ toolbar.style.cssText = 'padding:6px 0 10px 0;display:flex;gap:8px;';
+
+ function makeBtn(text, action) {
+ var btn = document.createElement('button');
+ btn.textContent = text;
+ btn.setAttribute('data-prof-plugin', 'source-fold');
+ btn.style.cssText = [
+ 'padding:2px 10px',
+ 'font-size:12px',
+ 'cursor:pointer',
+ 'border:1px solid #ccc',
+ 'border-radius:3px',
+ 'background:#f5f5f5',
+ ].join(';');
+ btn.addEventListener('click', action);
+ return btn;
+ }
+
+ toolbar.appendChild(makeBtn('全部折叠', function () {
+ h2List.forEach(function (h2) { setFolded(h2, true); });
+ }));
+ toolbar.appendChild(makeBtn('全部展开', function () {
+ h2List.forEach(function (h2) { setFolded(h2, false); });
+ }));
+
+ content.insertBefore(toolbar, content.firstChild);
+ }
+
+ // 防重复:清理上次注入的元素
+ function cleanup(content) {
+ var old = content.querySelectorAll('[data-prof-plugin="source-fold"]');
+ old.forEach(function (el) {
+ // toolbar div 整体移除,指示器 span 移除,h2 属性还原
+ if (el.tagName === 'DIV' || el.tagName === 'SPAN') {
+ el.parentNode && el.parentNode.removeChild(el);
+ } else if (el.tagName === 'H2') {
+ el.removeAttribute('data-prof-plugin');
+ el.removeAttribute('data-folded');
+ el.style.cursor = '';
+ }
+ });
+ // 恢复隐藏的 p/pre
+ var bodies = content.querySelectorAll('p[data-prof-fold-body], pre[data-prof-fold-body]');
+ bodies.forEach(function (el) {
+ el.style.display = '';
+ el.removeAttribute('data-prof-fold-body');
+ });
+ }
+
+ function run() {
+ var content = document.getElementById('content');
+ if (!content || !content.classList.contains('source')) return;
+
+ var h2List = Array.prototype.slice.call(content.querySelectorAll('h2'));
+ if (h2List.length === 0) return;
+
+ cleanup(content);
+
+ var config = loadConfig();
+ var roots = discoverSourceRoots(content);
+
+ h2List.forEach(function (h2) {
+ var p = h2.nextElementSibling;
+ var filePath = (p && p.tagName === 'P') ? p.textContent.trim() : '';
+ var name = h2.textContent.trim();
+ var folded = shouldDefaultFold(name, filePath, roots, config);
+ decorateH2(h2, folded);
+ });
+
+ injectToolbar(content, h2List);
+ }
+
+ P.register({
+ name: 'source-fold',
+ version: '1.1.0',
+ views: ['source'],
+
+ init: function () { run(); },
+ onViewChange: function (view) {
+ if (view === 'source') run();
+ },
+ });
+
+})(window.ProfPlugin);
diff --git a/web/static/prof-i18n.js b/web/static/prof-i18n.js
new file mode 100644
index 0000000..e8ad91d
--- /dev/null
+++ b/web/static/prof-i18n.js
@@ -0,0 +1,79 @@
+(function () {
+ 'use strict';
+
+ var DEFAULT_LANG = 'zh';
+ var LANG_KEY = 'prof-lang';
+
+ var lang = localStorage.getItem(LANG_KEY) || DEFAULT_LANG;
+ var translations = {};
+
+ function loadTranslations(l) {
+ var xhr = new XMLHttpRequest();
+ xhr.open('GET', '/static/i18n/' + l + '.json', false); // async=false intentional: local tool
+ try {
+ xhr.send();
+ if (xhr.status === 200) return JSON.parse(xhr.responseText);
+ } catch (e) {}
+ return {};
+ }
+
+ translations = loadTranslations(lang);
+
+ window.t = function (key) {
+ return translations[key] !== undefined ? translations[key] : key;
+ };
+
+ window.ProfI18n = {
+ lang: lang,
+ t: window.t,
+ setLang: function (newLang) {
+ localStorage.setItem(LANG_KEY, newLang);
+ location.reload();
+ },
+ getLang: function () { return lang; },
+ };
+
+ function applyTranslations() {
+ document.querySelectorAll('[data-i18n]').forEach(function (el) {
+ var key = el.getAttribute('data-i18n');
+ if (translations[key] !== undefined) el.textContent = translations[key];
+ });
+ document.querySelectorAll('[data-i18n-placeholder]').forEach(function (el) {
+ var key = el.getAttribute('data-i18n-placeholder');
+ if (translations[key] !== undefined) el.placeholder = translations[key];
+ });
+ }
+
+ function injectSwitcher() {
+ var nav = document.querySelector('.navbar-nav');
+ if (!nav) return;
+ if (document.getElementById('prof-lang-switcher')) return;
+
+ var li = document.createElement('li');
+ li.className = 'nav-item';
+ li.id = 'prof-lang-switcher';
+ li.style.cssText = 'cursor:pointer;';
+
+ var a = document.createElement('a');
+ a.className = 'nav-link';
+ a.style.cssText = 'user-select:none;';
+ a.textContent = lang === 'zh' ? 'EN' : '中';
+ a.title = lang === 'zh' ? 'Switch to English' : '切换为中文';
+ a.addEventListener('click', function () {
+ ProfI18n.setLang(lang === 'zh' ? 'en' : 'zh');
+ });
+
+ li.appendChild(a);
+ nav.appendChild(li);
+ }
+
+ if (document.readyState === 'loading') {
+ document.addEventListener('DOMContentLoaded', function () {
+ applyTranslations();
+ injectSwitcher();
+ });
+ } else {
+ applyTranslations();
+ injectSwitcher();
+ }
+})();
diff --git a/web/template/_keys.html b/web/template/_keys.html
new file mode 100644
index 0000000..a7c380c
--- /dev/null
+++ b/web/template/_keys.html
@@ -0,0 +1,6 @@
+{{define "storageKeys"}}
+
+{{end}}
diff --git a/web/template/index.html b/web/template/index.html
index 7ef7101..f361a39 100644
--- a/web/template/index.html
+++ b/web/template/index.html
@@ -5,6 +5,7 @@
Prof
+