mirror of
https://github.com/sinanyuntu/trade-message-center.git
synced 2026-09-17 13:22:11 +08:00
37 lines
1.0 KiB
TypeScript
37 lines
1.0 KiB
TypeScript
// 读取并展示当前浏览器标签页信息
|
|
|
|
interface ChromeTab {
|
|
title?: string;
|
|
url?: string;
|
|
}
|
|
|
|
interface ChromeApi {
|
|
tabs: {
|
|
query(queryInfo: { active: true; currentWindow: true }): Promise<ChromeTab[]>;
|
|
};
|
|
}
|
|
|
|
const chromeApi = (globalThis as unknown as { chrome: ChromeApi }).chrome;
|
|
const titleElement = document.querySelector<HTMLElement>("#title");
|
|
const urlElement = document.querySelector<HTMLElement>("#url");
|
|
|
|
if (!titleElement || !urlElement) throw new Error("Popup elements are missing");
|
|
const title = titleElement;
|
|
const url = urlElement;
|
|
|
|
/** 查询当前活动标签页并更新弹窗文本。 */
|
|
async function renderCurrentTab(): Promise<void> {
|
|
try {
|
|
const [tab] = await chromeApi.tabs.query({ active: true, currentWindow: true });
|
|
title.textContent = tab?.title || "无法读取当前页面";
|
|
url.textContent = tab?.url || "";
|
|
} catch {
|
|
title.textContent = "无法读取当前页面";
|
|
url.textContent = "";
|
|
}
|
|
}
|
|
|
|
void renderCurrentTab();
|
|
|
|
export {};
|