diff --git a/package.json b/package.json index 39a2781..be1c74e 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,6 @@ { "dependencies": { "vue": "^3.3.4", - "vue-i18n": "^9.4.0", "vue-router": "^4.2.2" }, "devDependencies": { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 7a89281..b61af53 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -8,9 +8,6 @@ dependencies: vue: specifier: ^3.3.4 version: 3.3.4 - vue-i18n: - specifier: ^9.4.0 - version: 9.4.0(vue@3.3.4) vue-router: specifier: ^4.2.2 version: 4.2.2(vue@3.3.4) @@ -450,27 +447,6 @@ packages: - supports-color dev: true - /@intlify/core-base@9.4.0: - resolution: {integrity: sha512-bIvtuEjLES35l39R0OccYbmr/fatSr4VSRThhouvFc05mnoAZyJGgj3st1iLaHGlFTmvd507uww1427o56r36Q==} - engines: {node: '>= 16'} - dependencies: - '@intlify/message-compiler': 9.4.0 - '@intlify/shared': 9.4.0 - dev: false - - /@intlify/message-compiler@9.4.0: - resolution: {integrity: sha512-EdjqOH3bQqEjZcUOwo90wuW4ZuFr41FDxVni6WNKS0V0myKgwz9EuvLDcnEbjJMIX8vKAWPjWfnSTt5fMwKHLA==} - engines: {node: '>= 16'} - dependencies: - '@intlify/shared': 9.4.0 - source-map-js: 1.0.2 - dev: false - - /@intlify/shared@9.4.0: - resolution: {integrity: sha512-AFqymip2kToqA0B6KZPg5jSrdcVHoli9t/VhGKE2iiMq9utFuMoGdDC/JOCIZgwxo6aXAk86QyU2XtzEoMuZ6A==} - engines: {node: '>= 16'} - dev: false - /@jridgewell/gen-mapping@0.3.3: resolution: {integrity: sha512-HLhSWOLRi875zjjMG/r+Nv0oCW8umGb0BgEhyX3dDX3egwZtB8PqLnjz3yedt8R5StBrzcg4aBpnh8UA9D1BoQ==} engines: {node: '>=6.0.0'} @@ -3039,18 +3015,6 @@ packages: - supports-color dev: true - /vue-i18n@9.4.0(vue@3.3.4): - resolution: {integrity: sha512-701V7wv6m/Fesbu58rRJv9Rd3en/9F7Nxyn/NiwcR7bJkEfwflMJg5wVezkeHy1tIqjXME1e1zbHhK1dNaR8mg==} - engines: {node: '>= 16'} - peerDependencies: - vue: ^3.0.0 - dependencies: - '@intlify/core-base': 9.4.0 - '@intlify/shared': 9.4.0 - '@vue/devtools-api': 6.5.0 - vue: 3.3.4 - dev: false - /vue-router@4.2.2(vue@3.3.4): resolution: {integrity: sha512-cChBPPmAflgBGmy3tBsjeoe3f3VOSG6naKyY5pjtrqLGbNEXdzCigFUHgBvp9e3ysAtFtEx7OLqcSDh/1Cq2TQ==} peerDependencies: diff --git a/src/background/index.ts b/src/background/index.ts index bf67e54..0576f1d 100644 --- a/src/background/index.ts +++ b/src/background/index.ts @@ -6,6 +6,7 @@ type SameSiteStatus = 'no_restriction' | 'lax' | 'strict' | 'None' // 定义接口和类型 interface Account { cookies: chrome.cookies.Cookie[] + localstorage?: any manualSave?: boolean closed?: boolean refresh?: boolean //加载全部cookie后,是否需要手动刷新? @@ -171,6 +172,61 @@ chrome.runtime.onMessage.addListener(async (request: any, _sender: chrome.runtim } }) +//useless +// // 监听来自Content Script或Popup的消息 +// chrome.runtime.onMessage.addListener((request, sender, sendResponse) => { +// if (request.type === 'LOAD_LOCALSTORAGE') { +// // 从某个存储(例如IndexedDB、chrome.storage等)中获取数据 +// const data = /* 对应账户的localStorage数据 */ loadLocalStorage(sender.tab.id, data) +// sendResponse({ status: 'Data loaded' }) +// } else if (request.type === 'SAVE_AND_CLEAR_LOCALSTORAGE') { +// saveAndClearLocalStorage(sender.tab.id) +// sendResponse({ status: 'Data saved and cleared' }) +// } +// }) + +async function loadLocalStorage(tabId: number, data: Record) { + try { + console.log('data in loadLocalStorage:', data) + if (!data) { + return + } + chrome.scripting.executeScript({ + target: { tabId }, + func: (data) => { + Object.assign(localStorage, data) + }, + args: [data], + }) + console.log('localStorage loaded successfully!') + }catch(error){ + console.log('An error occurred in loadLocalStorage:', error) + } +} + +async function saveAndClearLocalStorage(tabId: number) { + try { + const result = await chrome.scripting.executeScript({ + target: { tabId }, + func: () => { + // 保存当前页面的localStorage数据 + const savedData = { ...localStorage } + // 清除当前页面的localStorage + localStorage.clear() + return savedData + }, + }) + + // 输出成功消息 + console.log('Successfully saved and cleared localStorage:') + + return result + } catch (error) { + console.log('An error occurred in saveAndClearLocalStorage:', error) + } +} + + async function reloadActiveTab() { const tabs = await chrome.tabs.query({ active: true, currentWindow: true }) if (tabs[0] && tabs[0].id) { @@ -200,8 +256,9 @@ async function handleLoadAllCookies(loadSavedSelectedAccounts: boolean) { } else { savedAccounts = (await getStorageData('savedAccounts')) as Record } - const newAccounts: Record = {} + // const newAccounts: Record = {} console.log('savedAccounts:', savedAccounts) + const accounts = await getStorageData>('accounts') // 9.13 for (const [key, account] of Object.entries(savedAccounts)) { if (!account.cookies || !account.cookies[0]) { console.log('No cookies for this account, account key:', key) @@ -217,12 +274,13 @@ async function handleLoadAllCookies(loadSavedSelectedAccounts: boolean) { } const newKey = `${rootDomain}-${tab.id}` tabIdDataMap[tab.id] = { key: newKey, account } // 存储 tabId 和相应数据的映射 - - newAccounts[newKey] = account + accounts[newKey] = account + // newAccounts[newKey] = account } await chrome.storage.local.set({ tabIdDataMap: tabIdDataMap }) //update the accounts in this runtime, so we can use it in this extension 8.28 - await chrome.storage.local.set({ accounts: newAccounts }) + // await chrome.storage.local.set({ accounts: newAccounts }) + await chrome.storage.local.set({ accounts: accounts }) console.log('Updated accounts saved successfully!') } catch (error) { @@ -236,6 +294,7 @@ chrome.tabs.onUpdated.addListener(async function (tabId, changeInfo, tab) { // 可能同时刷新多个标签页,要对符合条件的都执行脚本 9.12 const modifyLinkEnabled = await getStorageData('modifyLinkEnabled', false) //9.12 if (modifyLinkEnabled) { + await processDomain(tab) //用于判断当前网页是否在追踪域名之内 9.13 modifyLinksInTab(tabId) } // const tabIdDataMap: Record = await getStorageData('tabIdDataMap') @@ -387,7 +446,17 @@ async function handleTabChange(tabId: number) { const [rootDomain, key] = (await processDomain(tab)) as [string, string] // chrome.tabs.sendMessage(tabId, { action: 'showMask' }) // To show the mask in the content script, corresponding with onUpdated 8.26 - await saveCurrentCookies(rootDomain, key) + await saveCurrentCookies(rootDomain, key)//todo 这里可以将里面的保存提到外面来执行 9.13 + + const result = await saveAndClearLocalStorage(currentTabId) // save and clear localstorage 9.13 + if (result && result[0] && result[0].result) { + accounts[key].localstorage = result[0].result + await chrome.storage.local.set({ accounts: accounts }) + } + else { + console.log("can't get result in saveAndClearLocalStorage in handleTabChange.") + } + console.log('handleTabChange中saveCurrentCookies已触发') //用户可以选择是否清除 @@ -431,6 +500,8 @@ async function handleTabChange(tabId: number) { await loadCookies(rootDomain, accounts[key].cookies) + await loadLocalStorage(tabId, accounts[key].localstorage) + // chrome.tabs.sendMessage(tabId, { action: 'hideMask' }) // To hide the mask in the content script, corresponding with onUpdated 8.26 //The code to remove a mask 8.29 diff --git a/src/popup/index.ts b/src/popup/index.ts index 1deecf4..8ce665a 100644 --- a/src/popup/index.ts +++ b/src/popup/index.ts @@ -4,27 +4,27 @@ import routes from '~pages' import '../assets/base.scss' import App from './app.vue' import './index.scss' -import { createI18n } from 'vue-i18n' +// import { createI18n } from 'vue-i18n' -const messages = { - en: { - title: 'Cookie Clerk', - accountManagement: 'Account Management', - add: 'Add', - saveCookies: 'Save Cookies To Account', - }, - zh_CN: { - title: 'Cookie 管理员', - accountManagement: '账户管理', - add: '添加', - saveCookies: '保存 Cookies 到账户', - }, -} +// const messages = { +// en: { +// title: 'Cookie Clerk', +// accountManagement: 'Account Management', +// add: 'Add', +// saveCookies: 'Save Cookies To Account', +// }, +// zh_CN: { +// title: 'Cookie 管理员', +// accountManagement: '账户管理', +// add: '添加', +// saveCookies: '保存 Cookies 到账户', +// }, +// } -const i18n = createI18n({ - locale: 'en', // 设置默认语言 - messages, -}) +// const i18n = createI18n({ +// locale: 'en', // 设置默认语言 +// messages, +// }) const router = createRouter({ history: createWebHashHistory(import.meta.env.BASE_URL), @@ -35,4 +35,4 @@ router.beforeEach((to) => { if (to.path === '/') return '/popup' }) -createApp(App).use(router).use(i18n).mount('#app') +createApp(App).use(router).mount('#app')