Files
mama-box/js/ugc.js

60 lines
3.9 KiB
JavaScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* 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,先按它渲染、接口回来再校正;接口不通就沿用上一次的值(从没拿到过 = 关)。
11-05 首页「孕N周专属福利」开关 giftEnabled(后台同一处,默认开):false → <html>.gift-off 藏 .shelf.gift2,缓存 mmbox.gift.v1。
预览站也跟这个开关(预览不跟的只是 UGC);正式版 main 是并进 UGC 那次请求里的,合并时以哪边为准都行、两边行为一致。
本文件在 <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}' +
'html.gift-off .shelf.gift2{display:none!important}';
(document.head || de).appendChild(st);
try { de.classList.toggle('gift-off', localStorage.getItem('mmbox.gift.v1') === '0'); } catch (e) {}
fetch('/api/mp/public/mama-box/config', { cache: 'no-store' })
.then(function (r) { return r.json(); })
.then(function (j) {
var g = !(j && j.result && j.result.giftEnabled === false);
de.classList.toggle('gift-off', !g);
try { localStorage.setItem('mmbox.gift.v1', g ? '1' : '0'); } catch (e) {}
}).catch(function () {});
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'; /* 还不知道开没开:先别露内容,免得关着时闪一下 */
/* 11-04 用户:「回到改预览页面」→ 预览站(/mama-box-preview/)不跟后台开关,一直显示,方便看广场改动;?ugc=0 看关闭效果。正式站照开关走。 */
if (/\/mama-box-preview\//.test(location.pathname)) {
var pv = !/[?&]ugc=0/.test(location.search);
apply(pv); if (guard) { if (!pv) goHome(); else de.style.visibility = ''; }
window.__mbUgcReady = Promise.resolve(pv);
return;
}
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;
});
})();