diff --git a/extension/content.js b/extension/content.js index 27af495..6723c81 100644 --- a/extension/content.js +++ b/extension/content.js @@ -21,6 +21,9 @@ function initializeExtension() { document.getElementById('leetcode-helper-get-hint').addEventListener('click', getHint); document.getElementById('leetcode-helper-get-hint-advanced').addEventListener('click', getHintAdvanced); document.getElementById('leetcode-helper-toggle').addEventListener('click', toggleOverlay); + document.getElementById('leetcode-helper-favorite').addEventListener('click', toggleFavorite); + // 检查当前题目是否已收藏 + checkIsFavorite(); } catch (error) { console.error("Error during extension initialization:", error); displayErrorMessage("Error: LeetCode's page structure has changed. The extension may not work correctly."); @@ -84,7 +87,12 @@ function createOverlay() {
Need help with your solution? Click a button below!
@@ -436,4 +444,57 @@ function formatTextWithCodeBlocks(text) { } return text; +} + +// 收藏功能 +function toggleFavorite() { + const problemUrl = window.location.href; + const favoriteBtn = document.getElementById('leetcode-helper-favorite'); + const icon = favoriteBtn.querySelector('i'); + + chrome.storage.local.get('favorites', (result) => { + let favorites = result.favorites || []; + const existingIndex = favorites.findIndex(item => item.url === problemUrl); + + if (existingIndex >= 0) { + // 取消收藏 + favorites.splice(existingIndex, 1); + icon.classList.remove('fa-solid'); + icon.classList.add('fa-regular'); + icon.style.color = ''; + } else { + // 添加收藏 + favorites.push({ + title: problemTitle, + url: problemUrl, + timestamp: Date.now() + }); + icon.classList.remove('fa-regular'); + icon.classList.add('fa-solid'); + icon.style.color = '#f1c40f'; + } + + chrome.storage.local.set({favorites: favorites}); + }); +} + +function checkIsFavorite() { + const problemUrl = window.location.href; + const favoriteBtn = document.getElementById('leetcode-helper-favorite'); + const icon = favoriteBtn.querySelector('i'); + + chrome.storage.local.get('favorites', (result) => { + const favorites = result.favorites || []; + const isFavorite = favorites.some(item => item.url === problemUrl); + + if (isFavorite) { + icon.classList.remove('fa-regular'); + icon.classList.add('fa-solid'); + icon.style.color = '#f1c40f'; + } else { + icon.classList.remove('fa-solid'); + icon.classList.add('fa-regular'); + icon.style.color = ''; + } + }); } \ No newline at end of file diff --git a/extension/popup.html b/extension/popup.html index f95d38c..9cc950b 100644 --- a/extension/popup.html +++ b/extension/popup.html @@ -47,6 +47,47 @@ h2 { font-size: 20px; } + + /* Tab Styles */ + .tabs { + display: flex; + gap: 8px; + margin-bottom: 16px; + background: var(--dark-surface); + padding: 4px; + border-radius: 8px; + } + + .tab-btn { + flex: 1; + padding: 10px 12px; + background: transparent; + border: none; + border-radius: 6px; + color: var(--medium-text); + font-size: 14px; + font-weight: 500; + cursor: pointer; + transition: all 0.2s ease; + font-family: 'Roboto', sans-serif; + } + + .tab-btn.active { + background: var(--primary-color); + color: white; + } + + .tab-btn:hover:not(.active) { + background: var(--dark-card); + } + + .tab-content { + display: none; + } + + .tab-content.active { + display: block; + } h3 { font-size: 16px; diff --git a/extension/popup.js b/extension/popup.js index cef4a9f..241dcb0 100644 --- a/extension/popup.js +++ b/extension/popup.js @@ -47,6 +47,25 @@ function setupEventListeners() { const aboutTab = document.getElementById('about-tab'); const settingsContent = document.getElementById('settings-content'); const aboutContent = document.getElementById('about-content'); + + // 收藏Tab功能 + const tabBtns = document.querySelectorAll('.tab-btn'); + const tabContents = document.querySelectorAll('.tab-content'); + tabBtns.forEach(btn => { + btn.addEventListener('click', () => { + const tab = btn.dataset.tab; + // 切换按钮状态 + tabBtns.forEach(b => b.classList.remove('active')); + btn.classList.add('active'); + // 切换内容 + tabContents.forEach(content => content.classList.remove('active')); + document.getElementById(`${tab}-tab`).classList.add('active'); + // 如果是收藏tab,加载收藏列表 + if (tab === 'favorites') { + renderFavorites(); + } + }); + }); if (settingsTab && aboutTab && settingsContent && aboutContent) { settingsTab.addEventListener('click', function() { @@ -270,4 +289,82 @@ function showErrorMessage(message) { } catch (error) { console.error('Error showing error message:', error); } +} + +// 渲染收藏列表 +function renderFavorites() { + const favoritesTab = document.getElementById('favorites-tab'); + if (!favoritesTab) return; + + chrome.storage.local.get('favorites', (result) => { + const favorites = result.favorites || []; + if (favorites.length === 0) { + favoritesTab.innerHTML = ` +No favorite problems yet
+Star problems on LeetCode pages to save them here
+${favorites.length} saved problems
+