Compare commits
8 Commits
v0.1.0
...
8482611011
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8482611011 | ||
|
|
7403a03246 | ||
|
|
856f4c01c9 | ||
|
|
5d872f7bf1 | ||
|
|
ce67ae56bd | ||
|
|
b849b08386 | ||
|
|
660fb811a2 | ||
|
|
f549059e47 |
4
.env.example
Normal file
4
.env.example
Normal file
@@ -0,0 +1,4 @@
|
|||||||
|
NOCODB_TOKEN=
|
||||||
|
NOCODB_LIST_URL=https://nocodb.example.com/api/v2/tables/xxx/records
|
||||||
|
NOCODB_FIELD_ID=账号ID
|
||||||
|
NOCODB_FIELD_ALIAS=_最终输出
|
||||||
1
.gitignore
vendored
1
.gitignore
vendored
@@ -79,6 +79,7 @@ web_modules/
|
|||||||
.env.test.local
|
.env.test.local
|
||||||
.env.production.local
|
.env.production.local
|
||||||
.env.local
|
.env.local
|
||||||
|
env.js
|
||||||
|
|
||||||
# parcel-bundler cache (https://parceljs.org/)
|
# parcel-bundler cache (https://parceljs.org/)
|
||||||
.cache
|
.cache
|
||||||
|
|||||||
@@ -1,3 +1,7 @@
|
|||||||
# Teams 别名插件
|
# Teams 别名插件
|
||||||
|
|
||||||
给 Microsoft Teams 好友的名称添加别名。
|
给 Microsoft Teams 好友的名称添加别名。
|
||||||
|
|
||||||
|
使用 NocoDB 数据库获取数据。修改 .env 之后别忘了修改 manifest.json 中的 host_permissions
|
||||||
|
|
||||||
|
v0.3.0 之后支持给聊天分类。
|
||||||
|
|||||||
1135
content.js
1135
content.js
File diff suppressed because it is too large
Load Diff
@@ -1,20 +1,23 @@
|
|||||||
{
|
{
|
||||||
"manifest_version": 3,
|
"manifest_version": 3,
|
||||||
"name": "Teams 别名管理",
|
"name": "Teams 别名管理",
|
||||||
"version": "0.1.0",
|
"version": "0.3.0",
|
||||||
"icons": {
|
"icons": {
|
||||||
"16": "icons/teams-alias-16.png",
|
"16": "icons/teams-alias-16.png",
|
||||||
"32": "icons/teams-alias-32.png",
|
"32": "icons/teams-alias-32.png",
|
||||||
"48": "icons/teams-alias-48.png",
|
"48": "icons/teams-alias-48.png",
|
||||||
"128": "icons/teams-alias-128.png"
|
"128": "icons/teams-alias-128.png"
|
||||||
},
|
},
|
||||||
"description": "给 Teams 好友设置别名",
|
"description": "给 Teams 好友设置别名以及聊天分类",
|
||||||
"permissions": ["storage", "scripting"],
|
"permissions": ["storage", "scripting"],
|
||||||
"host_permissions": ["https://teams.live.com/v2*"],
|
"host_permissions": [
|
||||||
|
"https://teams.live.com/v2*",
|
||||||
|
"https://nocodb.example.com/*"
|
||||||
|
],
|
||||||
"content_scripts": [
|
"content_scripts": [
|
||||||
{
|
{
|
||||||
"matches": ["https://teams.live.com/v2*"],
|
"matches": ["https://teams.live.com/v2*"],
|
||||||
"js": ["content.js"]
|
"js": ["env.js", "utils.js", "content.js"]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"action": {
|
"action": {
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
{
|
{
|
||||||
"name": "teams-alias",
|
"name": "teams-alias",
|
||||||
"version": "0.1.0",
|
"version": "0.3.0",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
"build": "node scripts/generate-env.js",
|
||||||
"test": "echo \"Error: no test specified\" && exit 1"
|
"test": "echo \"Error: no test specified\" && exit 1"
|
||||||
},
|
},
|
||||||
"author": "",
|
"author": "",
|
||||||
|
|||||||
@@ -55,10 +55,10 @@
|
|||||||
</head>
|
</head>
|
||||||
<body>
|
<body>
|
||||||
<h3>Teams 别名管理</h3>
|
<h3>Teams 别名管理</h3>
|
||||||
|
<button id="sync">立即同步</button>
|
||||||
<button id="export">导出</button>
|
<button id="export">导出</button>
|
||||||
<button id="import">覆盖导入</button>
|
<script src="env.js"></script>
|
||||||
<button id="update-import">合并导入</button>
|
<script src="utils.js"></script>
|
||||||
<input type="file" id="fileInput" accept="application/json" />
|
|
||||||
<script src="popup.js"></script>
|
<script src="popup.js"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
||||||
|
|||||||
64
popup.js
64
popup.js
@@ -17,48 +17,26 @@ document.getElementById('export').addEventListener('click', async () => {
|
|||||||
URL.revokeObjectURL(url);
|
URL.revokeObjectURL(url);
|
||||||
});
|
});
|
||||||
|
|
||||||
// 通用导入处理函数
|
// 手动同步
|
||||||
function handleFileImport(mode = 'overwrite') {
|
document.getElementById('sync').addEventListener('click', async () => {
|
||||||
const fileInput = document.getElementById('fileInput');
|
const btn = document.getElementById('sync');
|
||||||
|
const originalText = btn.textContent;
|
||||||
|
btn.textContent = '同步中...';
|
||||||
|
btn.disabled = true;
|
||||||
|
|
||||||
const handler = async (e) => {
|
try {
|
||||||
const file = e.target.files[0];
|
if (typeof fetchAliasesFromDB !== 'function') {
|
||||||
if (!file) return;
|
throw new Error("utils.js not loaded properly");
|
||||||
|
}
|
||||||
|
|
||||||
const reader = new FileReader();
|
const aliases = await fetchAliasesFromDB();
|
||||||
reader.onload = async function (event) {
|
await chrome.storage.local.set({ aliases, lastSync: Date.now() });
|
||||||
try {
|
alert(`同步成功!共获取 ${Object.keys(aliases).length} 条数据。`);
|
||||||
const importedData = JSON.parse(event.target.result);
|
} catch (error) {
|
||||||
if (typeof importedData !== 'object' || importedData === null) {
|
alert('同步失败,请检查网络或控制台日志。');
|
||||||
alert('文件格式错误');
|
console.error(error);
|
||||||
return;
|
} finally {
|
||||||
}
|
btn.textContent = originalText;
|
||||||
|
btn.disabled = false;
|
||||||
const { aliases: existingAliases = {} } = await chrome.storage.local.get('aliases');
|
}
|
||||||
|
});
|
||||||
const updatedAliases = (mode === 'merge')
|
|
||||||
? { ...importedData, ...existingAliases } // 保留现有的不被导入的覆盖
|
|
||||||
: importedData;
|
|
||||||
|
|
||||||
await chrome.storage.local.set({ aliases: updatedAliases });
|
|
||||||
|
|
||||||
alert(mode === 'merge' ? '导入并合并成功' : '导入成功');
|
|
||||||
} catch (err) {
|
|
||||||
alert('无法解析 JSON 文件');
|
|
||||||
} finally {
|
|
||||||
fileInput.removeEventListener('change', handler);
|
|
||||||
fileInput.value = ''; // 重置 input 以便后续重复导入同一文件
|
|
||||||
}
|
|
||||||
};
|
|
||||||
reader.readAsText(file);
|
|
||||||
};
|
|
||||||
|
|
||||||
fileInput.addEventListener('change', handler, { once: true });
|
|
||||||
fileInput.click();
|
|
||||||
}
|
|
||||||
|
|
||||||
// 导入(覆盖)
|
|
||||||
document.getElementById('import').addEventListener('click', () => handleFileImport('overwrite'));
|
|
||||||
|
|
||||||
// 更新导入(合并)
|
|
||||||
document.getElementById('update-import').addEventListener('click', () => handleFileImport('merge'));
|
|
||||||
37
scripts/generate-env.js
Normal file
37
scripts/generate-env.js
Normal file
@@ -0,0 +1,37 @@
|
|||||||
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
|
const envPath = path.join(__dirname, '..', '.env');
|
||||||
|
const outputPath = path.join(__dirname, '..', 'env.js');
|
||||||
|
|
||||||
|
try {
|
||||||
|
let envContent = '';
|
||||||
|
if (fs.existsSync(envPath)) {
|
||||||
|
envContent = fs.readFileSync(envPath, 'utf8');
|
||||||
|
}
|
||||||
|
|
||||||
|
const envVars = {};
|
||||||
|
envContent.split('\n').forEach(line => {
|
||||||
|
const match = line.match(/^\s*([\w_]+)\s*=\s*(.*)?\s*$/);
|
||||||
|
if (match) {
|
||||||
|
const key = match[1];
|
||||||
|
let value = match[2] || '';
|
||||||
|
// Remove quotes if present
|
||||||
|
if ((value.startsWith('"') && value.endsWith('"')) || (value.startsWith("'" ) && value.endsWith("'" ))) {
|
||||||
|
value = value.slice(1, -1);
|
||||||
|
}
|
||||||
|
envVars[key] = value;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
const fileContent = `// Auto-generated by scripts/generate-env.js
|
||||||
|
const ENV_SECRETS = ${JSON.stringify(envVars, null, 4)};
|
||||||
|
`;
|
||||||
|
|
||||||
|
fs.writeFileSync(outputPath, fileContent);
|
||||||
|
console.log('Successfully generated env.js');
|
||||||
|
|
||||||
|
} catch (err) {
|
||||||
|
console.error('Error generating env.js:', err);
|
||||||
|
process.exit(1);
|
||||||
|
}
|
||||||
73
utils.js
Normal file
73
utils.js
Normal file
@@ -0,0 +1,73 @@
|
|||||||
|
const NOCODB_CONFIG = {
|
||||||
|
// Token and URL are loaded from env.js (ENV_SECRETS global variable)
|
||||||
|
TOKEN: (typeof ENV_SECRETS !== 'undefined' && ENV_SECRETS.NOCODB_TOKEN) ? ENV_SECRETS.NOCODB_TOKEN : "",
|
||||||
|
LIST_URL: (typeof ENV_SECRETS !== 'undefined' && ENV_SECRETS.NOCODB_LIST_URL) ? ENV_SECRETS.NOCODB_LIST_URL : "",
|
||||||
|
FIELD_ID: (typeof ENV_SECRETS !== 'undefined' && ENV_SECRETS.NOCODB_FIELD_ID) ? ENV_SECRETS.NOCODB_FIELD_ID : "账号ID",
|
||||||
|
FIELD_ALIAS: (typeof ENV_SECRETS !== 'undefined' && ENV_SECRETS.NOCODB_FIELD_ALIAS) ? ENV_SECRETS.NOCODB_FIELD_ALIAS : "_最终输出"
|
||||||
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 从 NocoDB 获取所有别名记录并整理为键值对
|
||||||
|
* @returns {Promise<Object>} { "账号ID": "别名", ... }
|
||||||
|
*/
|
||||||
|
async function fetchAliasesFromDB() {
|
||||||
|
const listUrl = NOCODB_CONFIG.LIST_URL;
|
||||||
|
const headers = {
|
||||||
|
"xc-token": NOCODB_CONFIG.TOKEN,
|
||||||
|
"Content-Type": "application/json"
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!listUrl || !NOCODB_CONFIG.TOKEN) {
|
||||||
|
console.error("Teams Alias: 缺少配置 (URL 或 Token),请检查 .env 文件。");
|
||||||
|
throw new Error("Missing configuration");
|
||||||
|
}
|
||||||
|
|
||||||
|
let allAliases = {};
|
||||||
|
let offset = 0;
|
||||||
|
const limit = 1000;
|
||||||
|
|
||||||
|
try {
|
||||||
|
while (true) {
|
||||||
|
// 构建带参数的 URL
|
||||||
|
const url = new URL(listUrl);
|
||||||
|
url.searchParams.append("limit", limit);
|
||||||
|
url.searchParams.append("fields", `${NOCODB_CONFIG.FIELD_ID},${NOCODB_CONFIG.FIELD_ALIAS}`);
|
||||||
|
url.searchParams.append("offset", offset);
|
||||||
|
|
||||||
|
const response = await fetch(url.toString(), { headers });
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
console.error(`获取数据失败: ${response.status} ${response.statusText}`);
|
||||||
|
throw new Error("网络请求失败");
|
||||||
|
}
|
||||||
|
|
||||||
|
const data = await response.json();
|
||||||
|
const rows = data.list || [];
|
||||||
|
|
||||||
|
if (rows.length === 0) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
// 整理数据
|
||||||
|
rows.forEach(row => {
|
||||||
|
if (row[NOCODB_CONFIG.FIELD_ID] && row[NOCODB_CONFIG.FIELD_ALIAS]) {
|
||||||
|
// key = 账号ID, value = _最终输出
|
||||||
|
allAliases[row[NOCODB_CONFIG.FIELD_ID]] = row[NOCODB_CONFIG.FIELD_ALIAS];
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
if (rows.length < limit) {
|
||||||
|
break; // 已获取所有数据
|
||||||
|
}
|
||||||
|
|
||||||
|
offset += limit;
|
||||||
|
}
|
||||||
|
|
||||||
|
console.log(`同步完成,共获取 ${Object.keys(allAliases).length} 条别名记录。`);
|
||||||
|
return allAliases;
|
||||||
|
|
||||||
|
} catch (error) {
|
||||||
|
console.error("同步别名数据时出错:", error);
|
||||||
|
throw error; // 向抛出以便调用者处理
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user