fix: 加固扩展配置注入并缓存 getConfig

- configJSON 注入转义 </script,避免脚本块被提前终止(潜在注入)
- getConfig 解析结果缓存,避免重复 JSON.parse;支持 getConfig({ clone: true }) 返回副本
This commit is contained in:
rbetree
2025-12-26 15:23:44 +08:00
parent e0e8ef39ee
commit 1475a8a0d3
2 changed files with 44 additions and 7 deletions

View File

@@ -29,14 +29,35 @@ if (window.visualViewport) {
window.visualViewport.addEventListener('resize', menavUpdateAppHeight);
}
// 配置数据缓存:避免浏览器扩展/站点脚本频繁 JSON.parse
let menavConfigCacheReady = false;
let menavConfigCacheRaw = null;
let menavConfigCacheValue = null;
// 全局MeNav对象 - 用于浏览器扩展
window.MeNav = {
version: "1.0.0",
// 获取配置数据
getConfig: function() {
getConfig: function(options) {
const configData = document.getElementById('menav-config-data');
return configData ? JSON.parse(configData.textContent) : null;
if (!configData) return null;
const raw = configData.textContent || '';
if (!menavConfigCacheReady || menavConfigCacheRaw !== raw) {
menavConfigCacheValue = JSON.parse(raw);
menavConfigCacheRaw = raw;
menavConfigCacheReady = true;
}
if (options && options.clone) {
if (typeof structuredClone === 'function') {
return structuredClone(menavConfigCacheValue);
}
return JSON.parse(JSON.stringify(menavConfigCacheValue));
}
return menavConfigCacheValue;
},
// 获取元素的唯一标识符