v26110416 UGC 总开关:孕妈广场+测一测+专题评论区按后台 ugcEnabled 显示,关着时广场/测试页回首页(js/ugc.js)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
40
js/ugc.js
Normal file
40
js/ugc.js
Normal file
@@ -0,0 +1,40 @@
|
||||
/* 11-04 UGC 总开关(用户:「正式版先把孕妈广场和测一测整个模块隐藏起来,后台加个开关,打开就出现、关上就隐藏」)
|
||||
开关在后台 /box_bm/plaza 顶部,存 box_admin.mamabox_configs.ugcEnabled,公开读 GET /api/mp/public/mama-box/config(改完不用发版)。
|
||||
关闭时(默认):
|
||||
· <html> 带 .ugc-off,下面这张样式表把首页孕妈广场整块、百宝箱「趣味小测试」、「我的收藏」、专题页评论区藏掉;
|
||||
· 带 data-guard 引入本脚本的页面(广场页 / 测试页 / 我的投稿 / 两个老测试)直接回首页;
|
||||
· 别的脚本读 window.__mbUgc(true/false),等结果用 window.__mbUgcReady(Promise),中途变了会发 mb-ugc 事件。
|
||||
上一次拿到的值存本机 mmbox.ugc.v1,先按它渲染、接口回来再校正;接口不通就沿用上一次的值(从没拿到过 = 关)。
|
||||
本文件在 <root>/js/ugc.js,必须同步 <script> 引入(要靠 document.currentScript 算首页地址)。 */
|
||||
(function () {
|
||||
var K = 'mmbox.ugc.v1', de = document.documentElement, me = document.currentScript, on = false;
|
||||
try { on = localStorage.getItem(K) === '1'; } catch (e) {}
|
||||
var guard = !!(me && me.hasAttribute('data-guard'));
|
||||
var home = me ? new URL('../', me.src).pathname : '/';
|
||||
|
||||
var st = document.createElement('style');
|
||||
st.textContent =
|
||||
'html.ugc-off #gcHome,html.ugc-off #meFavCard,html.ugc-off .kit-e.e-qz,html.ugc-off #tx-cm,html.ugc-off .tc-in{display:none!important}' +
|
||||
'html.ugc-off .kit-g{grid-template-columns:repeat(2,1fr)!important}';
|
||||
(document.head || de).appendChild(st);
|
||||
|
||||
function apply(v) { on = !!v; window.__mbUgc = on; de.classList.toggle('ugc-off', !on); }
|
||||
function goHome() { location.replace(home + (location.hash || '')); }
|
||||
apply(on);
|
||||
if (guard && !on) de.style.visibility = 'hidden'; /* 还不知道开没开:先别露内容,免得关着时闪一下 */
|
||||
|
||||
window.__mbUgcReady = new Promise(function (res) {
|
||||
var t = setTimeout(function () { res(null); }, 4000);
|
||||
fetch('/api/mp/public/mama-box/config', { cache: 'no-store' })
|
||||
.then(function (r) { return r.json(); })
|
||||
.then(function (j) { clearTimeout(t); res(!!(j && j.result && j.result.ugcEnabled)); })
|
||||
.catch(function () { clearTimeout(t); res(null); });
|
||||
}).then(function (v) {
|
||||
if (v === null) v = on; else try { localStorage.setItem(K, v ? '1' : '0'); } catch (e) {}
|
||||
var changed = v !== on;
|
||||
apply(v);
|
||||
if (guard) { if (!v) { goHome(); return v; } de.style.visibility = ''; }
|
||||
if (changed) try { window.dispatchEvent(new Event('mb-ugc')); } catch (e) {}
|
||||
return v;
|
||||
});
|
||||
})();
|
||||
Reference in New Issue
Block a user