Skip to content

Commit

Permalink
fix after browser is closed, all accounts become one(domain--1)
Browse files Browse the repository at this point in the history
  • Loading branch information
14790897 committed Sep 20, 2023
1 parent 3d3b423 commit 083bffb
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 21 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,5 +57,5 @@
"preview": "vite preview"
},
"type": "module",
"version": "1.0.3"
"version": "1.0.4"
}
27 changes: 21 additions & 6 deletions src/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,31 @@ async function checkTabsAndCleanAccounts() {
// 创建一个新的对象来存储修改后的账户
const updatedAccounts: Record<string, Account> = {}
Object.keys(accounts).forEach((accountKey) => {
const modifiedKey = modifyTabIdFromKey(accountKey, true)
let modifiedKey = modifyTabIdFromKey(accountKey, true)

// 检查键是否已经存在
while (updatedAccounts.hasOwnProperty(modifiedKey)) {
// 如果键已经存在,则减少键名中的数字
modifiedKey = decrementNumberInKey(modifiedKey)
}

// 将修改后的键及其对应的值放回新的对象
updatedAccounts[modifiedKey] = accounts[accountKey]
})
console.log('已经修改不存在的选项卡cookie in checkTabsAndCleanAccounts')
// 将更新后的账户列表保存回插件的存储
chrome.storage.local.set({ accounts: updatedAccounts })
}

// 更新全局变量
// accounts = updatedAccounts
function decrementNumberInKey(key: string): string {
const firstDashIndex = key.indexOf('-')
const part1 = key.substring(0, firstDashIndex) // "somekey"
const part2 = key.substring(firstDashIndex + 1) // "1-2-3"
const keyParts = [part1, part2]
const lastPart = keyParts[1]
const newLastPart = (parseInt(lastPart, 10) - 1).toString()
keyParts[1] = newLastPart
return keyParts.join('-')
}

// 在浏览器启动时调用此函数
Expand Down Expand Up @@ -144,7 +159,7 @@ chrome.runtime.onMessage.addListener(async (request: any, _sender: chrome.runtim
// Get the URL for the account
const url = getURLFromAccountKey(request.account)
// Load cookies for this account
const rootDomain = getRootDomain(url) // todo Is this code necessary? 8.29
const rootDomain = getRootDomain(url) // TODO: Is this code necessary? 8.29
if (rootDomain) {
await loadCookies(rootDomain, accounts[request.account].cookies)
//加载好cookies之后要刷新页面 8.25
Expand Down Expand Up @@ -378,7 +393,7 @@ async function removeClosedAccounts() {

// 遍历现有的账户
for (const [key, account] of Object.entries(accounts)) {
// 如果账户没有被标记为删除,则添加到新的对象中
// 如果账户没有被标记为关闭,则添加到新的对象中
if (!account.closed || account.alias) {
//如果有alias,说明是用户手动添加的,不应该删除
//小心这里操作空对象,会报错,目前已修复9.9
Expand All @@ -400,7 +415,7 @@ async function removeClosedAccounts() {
function extractTabIdFromKey(key: string, modify = false) {
const keyParts = key.split('-')
if (modify) {
return keyParts[0] + '-' + '-1'
return keyParts[0] + '-' + '0'
}
return keyParts[keyParts.length - 1] // tabID存储在键的最后一部分
}
Expand Down
1 change: 1 addition & 0 deletions src/popup/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
<style>
html {
font-size: 16px;
font-family: '黑体', 'Arial', sans-serif;
}
</style>

Expand Down
32 changes: 18 additions & 14 deletions src/popup/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,41 +14,41 @@
</option>
</select>
<div class="flex ">
<button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-1 px-2 rounded mr-1 " @click="addAccount"
<button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-1 px-2 rounded mr-1 shadow-lg" @click="addAccount"
data-i18n="addAccount">Add</button>
<!-- Rename Button 9.13-->
<button class="bg-green-500 hover:bg-green-700 text-white font-bold py-1 px-2 rounded" @click="renameAccount"
<button class="bg-green-500 hover:bg-green-700 text-white font-bold py-1 px-2 rounded shadow-lg" @click="renameAccount"
data-i18n="renameAccount">Rename</button>
</div>
</div>

<div class="justify-center items-center ">
<button class="bg-green-500 hover:bg-green-700 text-white font-bold rounded mr-2" @click="saveCookies"> <img
<button class="bg-green-500 hover:bg-green-700 text-white font-bold rounded mr-2 shadow-lg" @click="saveCookies"> <img
src="../../assets/SaveCookies.png" alt="Save Cookies Icon" title="Save Cookies To Account"
:class="imageClass" data-i18n-title="saveCookiesToAccount">
</button>
<button class="bg-yellow-500 hover:bg-yellow-700 text-white font-bold rounded mr-2" @click="loadCookies"><img
<button class="bg-yellow-500 hover:bg-yellow-700 text-white font-bold rounded mr-2 shadow-lg" @click="loadCookies"><img
src="../../assets/LoadCookies.png" alt="Load Cookies Icon" title="Load Cookies From Account"
:class="imageClass" data-i18n-title="loadCookiesFromAccount"></button>
<button class="bg-red-500 hover:bg-red-700 text-white font-bold rounded mr-2" @click="deleteAccount"><img
<button class="bg-red-500 hover:bg-red-700 text-white font-bold rounded mr-2 shadow-lg" @click="deleteAccount"><img
src="../../assets/DeleteAccount.png" alt="Delete Account Icon" title="Delete Account" :class="imageClass"
data-i18n-title="deleteAccount">
</button>
<button class="bg-red-500 hover:bg-red-700 text-white font-bold rounded mr-2" @click="deleteAllAccounts"><img
<button class="bg-red-500 hover:bg-red-700 text-white font-bold rounded mr-2 shadow-lg" @click="deleteAllAccounts"><img
src="../../assets/DeleteAllAccount.png" alt="Delete All Account Icon" title="Delete All Account"
:class="imageClass" data-i18n-title="deleteAllAccount"></button>
<button class="bg-red-500 hover:bg-red-700 text-white font-bold rounded mr-2"
<button class="bg-red-500 hover:bg-red-700 text-white font-bold rounded mr-2 shadow-lg"
@click="clearAllClosedCookies"><img src="../../assets/ClearAllClosedCookies.png"
alt="Clear All Closed Cookies Icon" title="Clear All Closed Cookies" :class="imageClass"
data-i18n-title="clearAllClosedCookies"></button>
<button class="bg-blue-500 hover:bg-blue-700 text-white font-bold rounded mr-2" @click="clearCookies">
<button class="bg-blue-500 hover:bg-blue-700 text-white font-bold rounded mr-2 shadow-lg" @click="clearCookies">
<img src="../../assets/ClearCookies.png" alt="Clear Cookies Icon" title="Clear Cookies" :class="imageClass"
data-i18n-title="clearCookies">
</button>
<button class="bg-blue-500 hover:bg-blue-700 text-white font-bold rounded mr-2" @click="saveAllCookies">
<button class="bg-blue-500 hover:bg-blue-700 text-white font-bold rounded mr-2 shadow-lg" @click="saveAllCookies">
<img src="../../assets/SaveAllCookies.png" alt="Save All Cookies Icon" title="Save All Cookies"
:class="imageClass" data-i18n-title="saveAllCookies"></button>
<button class="bg-blue-500 hover:bg-blue-700 text-white font-bold rounded mr-2" @click="loadAllCookies">
<button class="bg-blue-500 hover:bg-blue-700 text-white font-bold rounded mr-2 shadow-lg" @click="loadAllCookies">
<img src="../../assets/LoadAllCookies.png" alt="Load All Cookies Icon" title="Load All Cookies"
:class="imageClass" data-i18n-title="loadAllCookies"></button>
<!-- <button class="bg-blue-500 hover:bg-blue-700 text-white font-bold rounded mr-2 p-2" @click="openOptions"
Expand All @@ -64,7 +64,7 @@
<div class="flex items-center mb-2">
<input v-model="newDomain" type="text" placeholder="Add new domain" required class="form-input w-48 mt-1 mr-2"
data-i18n-placeholder="domainExample">
<button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded mr-2 " @click="addDomain"
<button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded mr-2 shadow-lg" @click="addDomain"
data-i18n="addNewDomain">Add</button>
</div>
<label class="flex items-center space-x-2">
Expand All @@ -90,10 +90,10 @@
</div>
</div>

<!-- <div class="text-center m-4">
<RouterLink to="/about">About</RouterLink>
</div> -->
</template>
<!-- <div class="text-center m-4">
<RouterLink to="/about">About</RouterLink>
</div> -->

<script setup lang="ts">

Expand Down Expand Up @@ -188,6 +188,10 @@ chrome.storage.local.get('modifyLinkEnabled', (data) => {
// }
});

chrome.storage.local.get('onStartupClearEnabled', (data) => {
onStartupClearEnabled.value = data.onStartupClearEnabled || false;
});

chrome.storage.local.get('accounts', (data) => {
console.log('data.accounts in popup', data.accounts);
accounts.value = data.accounts || {};
Expand Down

0 comments on commit 083bffb

Please sign in to comment.