Files
testing-tool/src/utils/chromeTabs.ts
T
LingandRX 945780def8 Develop (#55)
feat: optimize dashboard/search UX and simplify extension architecture

Redesign Dashboard with compact tool grid and recently used tools
Improve TopBar search UX with Cmd/Ctrl+K shortcut and better history navigation
Reorganize project structure into src/
Migrate i18n from react-i18next to chrome.i18n
Remove runtime language switch and settings page
Remove HTML/Markdown conversion tools
Clean up unused code, dead animations, redundant comments, and imports
Improve component consistency with shadcn/ui patterns
Replace hardcoded strings/colors with i18n tokens and theme tokens
Add comprehensive project documentation and coding standards
Fix CI artifact upload workflow and multiple TypeScript/test issues

Includes various refactors, UI polish, i18n cleanup, CI improvements,
and maintenance updates across the codebase.
2026-05-28 22:39:25 +08:00

78 lines
1.8 KiB
TypeScript
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.
/**
* Chrome 标签页相关工具函数
*/
/**
* 获取当前活动的标签页
*/
export async function getActiveTab(): Promise<chrome.tabs.Tab | null> {
try {
const [tab] = await chrome.tabs.query({ active: true, currentWindow: true });
return tab || null;
} catch (error) {
console.error('获取活动标签页失败:', error);
return null;
}
}
/**
* 获取当前活动的标签页域名
*/
export async function getActiveTabDomain(): Promise<string> {
const tab = await getActiveTab();
if (tab?.url) {
try {
const url = new URL(tab.url);
return url.hostname;
} catch (e) {
console.error('解析域名失败:', e);
}
}
return '';
}
/**
* 在新标签页中打开扩展页面
* @param page - 扩展页面路径(如 'popup.html'
* @param params - 可选的查询参数
*/
export async function openExtensionPage(
page: string,
params?: Record<string, string>,
): Promise<void> {
try {
const url = new URL(chrome.runtime.getURL(page));
if (params) {
Object.entries(params).forEach(([key, value]) => url.searchParams.set(key, value));
}
await chrome.tabs.create({ url: url.toString() });
} catch (error) {
console.error('打开扩展页面失败:', error);
}
}
/**
* 确保内容脚本已注入
*/
export async function ensureContentScriptInjected(): Promise<boolean> {
try {
const tab = await getActiveTab();
if (!tab?.id) return false;
try {
return true;
} catch (e) {
console.log('内容脚本未注入,尝试注入...');
console.error('注入内容脚本失败:', e);
await chrome.scripting.executeScript({
target: { tabId: tab.id },
files: ['/content-scripts/content.js'],
});
return true;
}
} catch (error) {
console.error('注入内容脚本失败:', error);
return false;
}
}