// 監(jiān)聽當(dāng)前活動標(biāo)簽頁的變化
chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
? if (changeInfo.status === 'complete') {
? ? // 當(dāng)標(biāo)簽頁加載完成后,保存標(biāo)簽頁的相關(guān)信息
? ? const tabData = {
? ? ? url: tab.url,
? ? ? title: tab.title,
? ? ? // 可以根據(jù)需要添加其他信息
? ? };
? ? // 將數(shù)據(jù)存儲到 chrome.storage.local 中
? ? chrome.storage.local.set({ [tabId]: tabData }, () => {
? ? ? console.log(`Tab data for tabId ${tabId} has been saved.`);
? ? });
? }
});
// 監(jiān)聽標(biāo)簽頁關(guān)閉事件,清除存儲的數(shù)據(jù)
chrome.tabs.onRemoved.addListener((tabId, removeInfo) => {
? // 標(biāo)簽頁關(guān)閉時(shí)炭菌,刪除該標(biāo)簽頁對應(yīng)的數(shù)據(jù)
? chrome.storage.local.remove(tabId.toString(), () => {
? ? console.log(`Tab data for tabId ${tabId} has been removed.`);
? });
});
// 其他可能的事件監(jiān)聽(例如窗口關(guān)閉或擴(kuò)展被禁用時(shí)清理)
chrome.windows.onRemoved.addListener((windowId) => {
? // 可添加額外邏輯醇王,當(dāng)整個(gè)窗口關(guān)閉時(shí)敌卓,處理特定的數(shù)據(jù)清理
});
// 從 storage 讀取已存儲的標(biāo)簽頁數(shù)據(jù)(如果需要)
chrome.storage.local.get(null, (items) => {
? console.log("Current storage:", items);
});
//////////////
// 自定義彈窗公告邏輯遂黍,帶不同類型的 SVG 圖標(biāo)和顏色
function showCustomPopup(iconType, iconColor) {
? ? const popup = document.createElement('div');
? ? popup.style.position = 'fixed';
? ? popup.style.bottom = '10px';
? ? popup.style.right = '10px';
? ? popup.style.backgroundColor = '#f1c40f';
? ? popup.style.padding = '10px';
? ? popup.style.zIndex = '1000';
? ? popup.style.display = 'flex';
? ? popup.style.alignItems = 'center';
? ? // 動態(tài)生成 SVG 圖標(biāo)
? ? const svgIcon = getSvgIcon(iconType, iconColor);
? ? // 添加圖標(biāo)到彈窗
? ? popup.innerHTML = `
? ? ? ? ${svgIcon}
? ? ? ? <span style="margin-left: 10px;">這是一個(gè)自定義公告</span>
? ? `;
? ? document.body.appendChild(popup);
}
// 動態(tài)生成 SVG 圖標(biāo)俐填,根據(jù)類型和顏色
function getSvgIcon(type, color) {
? ? let svg = '';
? ? switch (type) {
? ? ? ? case 'info':
? ? ? ? ? ? svg = `
? ? ? ? ? ? ? ? <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="${color}" viewBox="0 0 24 24">
? ? ? ? ? ? ? ? ? ? <path d="M12 2a10 10 0 1 0 0 20 10 10 0 0 0 0-20zm0 18a8 8 0 1 1 0-16 8 8 0 0 1 0 16zm1-7h-2v-5h2v5zm0 4h-2v-2h2v2z"/>
? ? ? ? ? ? ? ? </svg>`;
? ? ? ? ? ? break;
? ? ? ? case 'warning':
? ? ? ? ? ? svg = `
? ? ? ? ? ? ? ? <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="${color}" viewBox="0 0 24 24">
? ? ? ? ? ? ? ? ? ? <path d="M1 21h22L12 2 1 21zm12-3h-2v-2h2v2zm0-4h-2v-4h2v4z"/>
? ? ? ? ? ? ? ? </svg>`;
? ? ? ? ? ? break;
? ? ? ? case 'success':
? ? ? ? ? ? svg = `
? ? ? ? ? ? ? ? <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="${color}" viewBox="0 0 24 24">
? ? ? ? ? ? ? ? ? ? <path d="M12 2a10 10 0 1 0 0 20 10 10 0 0 0 0-20zm-2 15l-5-5 1.41-1.41L10 14.17l7.59-7.59L19 8l-9 9z"/>
? ? ? ? ? ? ? ? </svg>`;
? ? ? ? ? ? break;
? ? ? ? default:
? ? ? ? ? ? svg = `
? ? ? ? ? ? ? ? <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="${color}" viewBox="0 0 24 24">
? ? ? ? ? ? ? ? ? ? <circle cx="12" cy="12" r="10" />
? ? ? ? ? ? ? ? </svg>`;
? ? ? ? ? ? break;
? ? }
? ? return svg;
}
// 加載配置后安接,判斷是否顯示彈窗并傳入 SVG 圖標(biāo)類型和顏色
async function init() {
? ? await loadConfig(); // 等待配置加載完成
? ? // 根據(jù) globalConfig 顯示自定義彈窗公告,傳入圖標(biāo)類型和顏色
? ? if (globalConfig.showPopup) {
? ? ? ? showCustomPopup('info', '#007bff');? // 'info' 圖標(biāo), 藍(lán)色
? ? }
}
// 初始化
init();