使用外部数据库
This commit is contained in:
55
content.js
55
content.js
@@ -9,19 +9,6 @@ const PEOPLE_PICKER_SEL_PREFIX = "people-picker-selected-user-";
|
||||
let debounceTimer = null;
|
||||
let isMutating = false;
|
||||
|
||||
async function saveAlias(id, alias) {
|
||||
const key = id.replace(PERSON_ID_PREFIX, "");
|
||||
const result = await chrome.storage.local.get('aliases');
|
||||
const aliases = result.aliases || {};
|
||||
// 新的别名为空就删除
|
||||
if (alias) {
|
||||
aliases[key] = alias;
|
||||
} else {
|
||||
delete aliases[key];
|
||||
}
|
||||
await chrome.storage.local.set({ aliases });
|
||||
}
|
||||
|
||||
async function getAlias(id) {
|
||||
const key = id.replace(PERSON_ID_PREFIX, "");
|
||||
const result = await chrome.storage.local.get('aliases');
|
||||
@@ -34,44 +21,37 @@ function applyAliasAndButton(el) {
|
||||
const id = el.id;
|
||||
if (!id || !id.startsWith(PERSON_ID_PREFIX)) return;
|
||||
|
||||
const rawId = id.replace(PERSON_ID_PREFIX, "");
|
||||
|
||||
const existingBtn = document.querySelector(`[data-floating-btn-for="${id}"]`);
|
||||
if (!existingBtn) {
|
||||
const rect = el.getBoundingClientRect();
|
||||
|
||||
const button = document.createElement('button');
|
||||
button.textContent = '设置别名';
|
||||
button.textContent = '显示ID'; // 改为显示ID
|
||||
button.style.position = 'fixed';
|
||||
button.style.left = `${rect.left + window.scrollX}px`;
|
||||
button.style.top = `${rect.bottom + window.scrollY + 20}px`;
|
||||
button.style.zIndex = '99999';
|
||||
button.style.padding = '4px 8px';
|
||||
button.style.fontSize = '12px';
|
||||
button.style.backgroundColor = '#0078d4';
|
||||
button.style.backgroundColor = '#6c757d'; // 灰色,表示只是信息查看
|
||||
button.style.color = '#fff';
|
||||
button.style.border = 'none';
|
||||
button.style.borderRadius = '4px';
|
||||
button.style.cursor = 'pointer';
|
||||
button.setAttribute('data-floating-btn-for', id);
|
||||
|
||||
button.addEventListener('click', async () => {
|
||||
const current = (await getAlias(id.replace(PERSON_ID_PREFIX, ""))) || document.getElementById(id)?.textContent || '';
|
||||
const newAlias = prompt("请输入别名:", current);
|
||||
// 如果是空字符串还是得进入
|
||||
if (newAlias !== null) {
|
||||
const el = document.getElementById(id); // 🔥 重新获取最新的元素
|
||||
if (el && newAlias) el.textContent = newAlias.trim();
|
||||
await saveAlias(id.replace(PERSON_ID_PREFIX, ""), newAlias.trim());
|
||||
}
|
||||
if (newAlias === "") {
|
||||
alert("别名已删除");
|
||||
}
|
||||
button.addEventListener('click', () => {
|
||||
// 弹窗显示 ID,方便复制
|
||||
prompt("该用户的账号ID为 (请复制):", rawId);
|
||||
});
|
||||
|
||||
document.body.appendChild(button);
|
||||
}
|
||||
|
||||
// 应用别名(异步)
|
||||
getAlias(id.replace(PERSON_ID_PREFIX, "")).then(alias => {
|
||||
getAlias(rawId).then(alias => {
|
||||
if (alias && el.textContent !== alias) {
|
||||
el.textContent = alias;
|
||||
}
|
||||
@@ -417,6 +397,25 @@ function init() {
|
||||
subtree: true,
|
||||
});
|
||||
|
||||
// 自动同步检查
|
||||
chrome.storage.local.get('lastSync').then(({ lastSync }) => {
|
||||
const now = Date.now();
|
||||
// 24 小时 = 86400000 ms
|
||||
if (!lastSync || (now - lastSync > 86400000)) {
|
||||
console.log("Teams Alias: 正在进行后台同步...");
|
||||
if (typeof fetchAliasesFromDB === 'function') {
|
||||
fetchAliasesFromDB().then(aliases => {
|
||||
chrome.storage.local.set({ aliases, lastSync: now });
|
||||
console.log("Teams Alias: 自动同步成功");
|
||||
}).catch(err => {
|
||||
console.error("Teams Alias: 自动同步失败", err);
|
||||
});
|
||||
} else {
|
||||
console.warn("Teams Alias: fetchAliasesFromDB 未定义,无法同步。");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
applyToAll(); // 初始执行
|
||||
|
||||
// 兜底:每 2 秒再扫一次(避免漏掉异步更新)
|
||||
|
||||
Reference in New Issue
Block a user