Files
mama-box/js/ugc.js

50 lines
3.2 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 同一个接口顺带读 giftEnabled(用户:「首页底部孕 N 周专属福利加个开关,可以关闭隐藏」):
false → <html> 带 .gift-off,藏首页 .shelf.gift2 整块;默认/从没拿到 = 显示,缓存 mmbox.gift.v1。
本文件在 <root>/js/ugc.js,必须同步 <script> 引入(要靠 document.currentScript 算首页地址)。 */
(function () {
var K = 'mmbox.ugc.v1', KG = 'mmbox.gift.v1', de = document.documentElement, me = document.currentScript, on = false;
try { on = localStorage.getItem(K) === '1'; de.classList.toggle('gift-off', localStorage.getItem(KG) === '0'); } 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);
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);
var g = !(j && j.result && j.result.giftEnabled === false);
de.classList.toggle('gift-off', !g);
try { localStorage.setItem(KG, g ? '1' : '0'); } catch (e) {}
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;
});
})();