Bạn đang mở trang trong trình duyệt của một ứng dụng (Facebook, TikTok, Zalo…). Google thường chặn đăng nhập ở đây. Hãy mở trang bằng Chrome hoặc Safari, hoặc đăng nhập bằng email ở trên.
◆ ◆ ◆
PHỤNG LINH CÁC
Hoàn tất hồ sơ để bắt đầu
Thông tin sinh dùng để lập lá số và bát tự. Chỉ bạn xem được; người khác không thấy ngày giờ sinh của bạn.
PHỤNG LINH CÁC
{ try { f.contentWindow.postMessage(ctxMessage(), location.origin); } catch (e){} });
frames[m.id] = f; pane.appendChild(f);
}
$('#panes').appendChild(pane);
return pane;
}
const tabUrl = (id) => location.pathname + (id === 'home' ? '' : '#' + id);
function routeNow(){
const m = location.pathname.match(/\/([A-Za-z]+)\/?$/);
const seg = m ? m[1] : '';
return MODULES.some((x) => x.id === seg && x.id !== 'home') ? seg : (location.hash || '').replace('#', '');
}
function openTab(id, mode){ // mode: 'replace' (lần đầu), 'pop' (do nút Back/Forward), mặc định: thêm một mục lịch sử
const m = MODULES.find((x) => x.id === id) || MODULES[0];
const prev = current;
current = m.id;
const pane = ensurePane(m);
document.querySelectorAll('#panes .pane').forEach((p) => { p.hidden = p !== pane; });
if (m.id === 'hoso') renderProfile();
renderNav(); updateTitle();
$('#capTools').hidden = !m.cap;
try {
if (mode === 'pop') { /* trình duyệt đã tự đặt đúng mục lịch sử */ }
else if (mode === 'replace' || prev === m.id) history.replaceState({ tab: m.id }, '', tabUrl(m.id));
else history.pushState({ tab: m.id }, '', tabUrl(m.id));
} catch (e){}
}
/* ----- Sao chép / tải ảnh lá số, ảnh quẻ -----
Khung chính nhờ trang đang mở vẽ ảnh PNG (plc:capture), nhận lại qua plc:captured. */
function layAnh(){
const m = MODULES.find((x) => x.id === current), f = m && frames[m.id];
if (!m || !m.cap || !f) return Promise.reject(new Error('Mục này chưa có ảnh để lưu.'));
return new Promise((ok, hong) => {
const id = 'cap' + Date.now() + Math.random().toString(36).slice(2, 6);
const tt = setTimeout(() => { window.removeEventListener('message', on); hong(new Error('Tạo ảnh quá lâu, thử lại sau.')); }, 25000);
function on(e){
if (e.origin !== location.origin || e.source !== f.contentWindow) return;
const d = e.data; if (!d || d.type !== 'plc:captured' || d.id !== id) return;
clearTimeout(tt); window.removeEventListener('message', on);
if (d.blob) ok({ blob: d.blob, name: d.name || m.cap }); else hong(new Error(d.error || 'Không tạo được ảnh.'));
}
window.addEventListener('message', on);
try { f.contentWindow.postMessage({ type: 'plc:capture', id }, location.origin); }
catch (err){ clearTimeout(tt); window.removeEventListener('message', on); hong(new Error('Không gửi được yêu cầu tạo ảnh.')); }
});
}
function tenFileAnh(name){
const d = new Date(), z = (n) => String(n).padStart(2, '0');
return name + '_' + d.getFullYear() + z(d.getMonth() + 1) + z(d.getDate()) + '_' + z(d.getHours()) + z(d.getMinutes()) + '.png';
}
async function xuLyAnh(kieu){
const bs = [$('#capCopy'), $('#capSave')];
bs.forEach((b) => { b.disabled = true; });
try {
if (kieu === 'copy'){
if (!(navigator.clipboard && window.ClipboardItem)) throw new Error('Trình duyệt này không cho sao chép ảnh. Hãy dùng nút Tải ảnh.');
const p = layAnh();
toast('Đang tạo ảnh...');
await navigator.clipboard.write([new ClipboardItem({ 'image/png': p.then((r) => r.blob) })]);
toast('Đã sao chép ảnh. Bạn có thể dán vào Zalo, Facebook hoặc tài liệu.');
} else {
toast('Đang tạo ảnh...');
const r = await layAnh();
const a = document.createElement('a'); a.href = URL.createObjectURL(r.blob); a.download = tenFileAnh(r.name);
document.body.appendChild(a); a.click(); a.remove(); setTimeout(() => URL.revokeObjectURL(a.href), 4000);
toast('Đã tải ảnh về máy.');
}
} catch (e){ toast(e && e.message ? e.message : 'Không tạo được ảnh.'); }
finally { bs.forEach((b) => { b.disabled = false; }); }
}
$('#capCopy').onclick = () => xuLyAnh('copy');
$('#capSave').onclick = () => xuLyAnh('save');
window.addEventListener('popstate', () => {
if (!me || !started) return;
const h = routeNow();
openTab(MODULES.some((m) => m.id === h) ? h : 'home', 'pop');
});
/* ----- Hồ sơ ----- */
function renderProfile(){
const pane = $('#pane-hoso'); pane.replaceChildren();
const box = el('div', 'box');
if (me.guest){
box.appendChild(el('h2', null, me.displayName));
box.appendChild(el('p', 'note', 'Bạn đang dùng thử với tư cách khách: an lá số, bát tự, gieo quẻ và lập bàn Kỳ Môn miễn phí (có giới hạn số lượt mỗi ngày). Đăng nhập hoặc tạo tài khoản để lưu hồ sơ sinh, nhắn tin riêng, tạo nhóm và dùng không giới hạn.'));
const a = el('button', 'btn btn-sm', 'Tạo tài khoản'); a.type = 'button'; a.id = 'guestRegBtn'; a.onclick = () => openAuth('reg');
const b = el('button', 'linkbtn', 'Tôi đã có tài khoản, đăng nhập'); b.type = 'button'; b.id = 'guestLoginBtn2'; b.onclick = () => openAuth('login');
box.append(a, document.createElement('br'), b);
pane.appendChild(box);
return;
}
box.appendChild(el('h2', null, me.displayName));
box.appendChild(el('p', 'note', 'Thông tin sinh chỉ mình bạn xem được.'));
const sum = el('div', 'sum'); sum.id = 'birthSum';
birthSummary(me.birth).forEach(([k, v]) => { const r = el('div'); r.append(el('span', null, k + ': '), document.createTextNode(v)); sum.appendChild(r); });
box.appendChild(sum);
box.appendChild(el('div', 'sect', 'Sửa thông tin sinh'));
const f = birthForm(me.birth, 'ed_');
box.appendChild(f.node);
const err = el('div', 'err'); err.id = 'profileErr'; box.appendChild(err);
const save = el('button', 'btn btn-sm', 'Lưu thay đổi'); save.type = 'button'; save.id = 'profileSave';
save.onclick = async () => {
const b = f.read();
if (b.error){ err.textContent = b.error; return; }
save.disabled = true; err.textContent = '';
try { await saveBirth(me.uid, b.data); }
catch (e){ console.error(e); err.textContent = 'Không lưu được (' + (e.code || e.message) + ').'; save.disabled = false; return; }
me.birth = { ...b.data }; save.disabled = false;
toast('Đã lưu thông tin sinh.');
for (const id in frames){ try { frames[id].contentWindow.postMessage(ctxMessage(), location.origin); } catch (e){} }
renderProfile();
};
box.appendChild(save);
const out = el('button', 'linkbtn', 'Đăng xuất'); out.type = 'button'; out.id = 'logoutBtn'; out.onclick = logout;
box.appendChild(document.createElement('br')); box.appendChild(out);
pane.appendChild(box);
}
/* ----- Thông điệp từ các dịch vụ (iframe cùng tên miền) ----- */
window.addEventListener('message', (e) => {
if (e.origin !== location.origin) return;
if (e.data && e.data.type === 'plc:needToken'){
// Dịch vụ xin ID token Firebase để gọi máy chủ tính toán riêng (Apps Script). Nếu phiên đăng nhập chưa sẵn sàng thì chờ tối đa 8 giây.
const rid = String(e.data.id || '').slice(0, 64), src = e.source, force = !!e.data.force;
(async () => {
let token = null;
try {
for (let i = 0; i < 32 && !(auth && auth.currentUser); i++) await new Promise((r) => setTimeout(r, 250));
if (auth && auth.currentUser) token = await auth.currentUser.getIdToken(force);
} catch (_) {}
try { src.postMessage({ type: 'plc:token', id: rid, token }, location.origin); } catch (_) {}
})();
return;
}
if (!me) return;
const id = Object.keys(frames).find((k) => frames[k].contentWindow === e.source);
if (!id || !e.data) return;
if (e.data.type === 'plc:hello'){ e.source.postMessage(ctxMessage(), location.origin); }
else if (e.data.type === 'plc:go'){ // trang chủ bấm vào một công cụ
const t = String(e.data.tab || '');
if (id === 'home' && MODULES.some((x) => x.id === t && x.src)) openTab(t);
}
else if (e.data.type === 'plc:needLogin'){
// Dịch vụ báo cần tài khoản (hết lượt thử, nhắn riêng, nhóm...). Chỉ khách mới cần mở màn đăng nhập.
toast(String(e.data.msg || 'Bạn đã dùng hết lượt hôm nay, vui lòng quay lại vào ngày mai.').slice(0, 200));
}
});
function updateTitle(){
let n = 0; for (const k in badges) n += badges[k] || 0;
document.title = (n ? '(' + n + ') ' : '') + 'Phụng Linh Các';
}