68 lines
2.4 KiB
JavaScript
68 lines
2.4 KiB
JavaScript
(function (root, factory) {
|
||
if (typeof module === 'object' && module.exports) {
|
||
module.exports = factory(root);
|
||
return;
|
||
}
|
||
|
||
root.GenealogyEntryPages = factory(root);
|
||
if (root.document) {
|
||
root.document.addEventListener('DOMContentLoaded', function () {
|
||
root.GenealogyEntryPages.init();
|
||
});
|
||
}
|
||
})(typeof globalThis !== 'undefined' ? globalThis : window, function (root) {
|
||
'use strict';
|
||
|
||
var TARGET_LABELS = {
|
||
'profile-family-home.html': '家谱主页',
|
||
'profile-feed.html': '家族圈动态',
|
||
'profile-growth.html': '成长记录',
|
||
'profile-relative.html': '亲友往来',
|
||
'profile-memo.html': '备忘录',
|
||
'profile-tree.html': '世系图',
|
||
'profile-generation.html': '字辈谱'
|
||
};
|
||
|
||
function getTargetPage(search) {
|
||
var params = new URLSearchParams(String(search === undefined && root.location ? root.location.search : search || '').replace(/^\?/, ''));
|
||
var target = params.get('next') || '';
|
||
|
||
return /^profile-[a-z-]+\.html$/.test(target) ? target : 'profile-family-home.html';
|
||
}
|
||
|
||
function buildStatus(quota, target) {
|
||
var source = quota || {};
|
||
var details = [];
|
||
var label = TARGET_LABELS[target] || '该家谱业务';
|
||
|
||
if (Number(source.createRemaining) >= 0) details.push('还可创建 ' + source.createRemaining + ' 部');
|
||
if (Number(source.joinRemaining) >= 0) details.push('还可加入 ' + source.joinRemaining + ' 部');
|
||
return '当前 PC 接口尚未提供“我的家谱”列表或家谱详情,不能伪造家谱编号进入“' + label + '”。' +
|
||
(details.length ? ' 已读取额度:' + details.join(',') + '。' : '');
|
||
}
|
||
|
||
async function init() {
|
||
var status = root.document && root.document.querySelector('[data-genealogy-entry-status]');
|
||
var api = root.GenealogyApi && root.GenealogyApi.defaultClient;
|
||
var target = getTargetPage();
|
||
|
||
if (!status) return;
|
||
if (!api || !api.genealogyQuota) {
|
||
status.textContent = '家谱入口接口初始化失败,请刷新后重试。';
|
||
return;
|
||
}
|
||
|
||
try {
|
||
status.textContent = buildStatus(await api.genealogyQuota(), target);
|
||
} catch (error) {
|
||
status.textContent = '无法读取家谱额度,请稍后重试。当前 PC 接口仍未提供家谱列表或详情,不能伪造家谱编号。';
|
||
}
|
||
}
|
||
|
||
return {
|
||
getTargetPage: getTargetPage,
|
||
buildStatus: buildStatus,
|
||
init: init
|
||
};
|
||
});
|