Skip to content

Commit

Permalink
i18n
Browse files Browse the repository at this point in the history
  • Loading branch information
14790897 committed Sep 12, 2023
1 parent b1b369c commit 8902953
Show file tree
Hide file tree
Showing 9 changed files with 310 additions and 48 deletions.
78 changes: 78 additions & 0 deletions _locales/en/messages.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
{
"title":{
"message": "CookiesClerk",
"description": "Title text"
},
"accountManagement": {
"message": "Account Management",
"description": "Title for the account management section"
},
"addAccount": {
"message": "Add",
"description": "Label for the Add button"
},
"trackedDomainManagement": {
"message": "Tracked Domain Management",
"description": "Title for the tracked domain management section"
},
"domainExample": {
"message": "Example: google.com",
"description": "Placeholder for the new domain input box"
},
"addNewDomain": {
"message": "Add",
"description": "Placeholder for the new domain input box"
},
"clearCookiesEnabled": {
"message": "Enable cookie clearing for new tabs in tracked domains",
"description": "Description for the first checkbox"
},
"modifyLinkEnabled": {
"message": "Restrict tracked domain links to open only in the current tab",
"description": "Description for the second checkbox"
},
"delete": {
"message": "Delete",
"description": "Label for the Delete button"
},
"saveCookiesToAccount": {
"message": "Save Cookies To Account",
"description": "Title for the Save Cookies button"
},
"loadCookiesFromAccount": {
"message": "Load Cookies From Account",
"description": "Title for the Load Cookies button"
},
"deleteAccount": {
"message": "Delete Account",
"description": "Title for the Delete Account button"
},
"deleteAllAccount": {
"message": "Delete All Account",
"description": "Title for the Delete All Account button"
},
"clearAllClosedCookies": {
"message": "Clear All Closed Cookies",
"description": "Title for the Clear All Closed Cookies button"
},
"clearCookies": {
"message": "Clear Cookies",
"description": "Title for the Clear Cookies button"
},
"saveAllCookies": {
"message": "Save All Cookies",
"description": "Title for the Save All Cookies button"
},
"loadAllCookies": {
"message": "Load All Cookies",
"description": "Title for the Load All Cookies button"
},
"greeting": {
"message": "Hello",
"description": "Greeting text"
},
"farewell": {
"message": "Goodbye",
"description": "Farewell text"
}
}
78 changes: 78 additions & 0 deletions _locales/zh_CN/messages.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
{
"title":{
"message": "Cookies店员",
"description": "Title text"
},
"accountManagement": {
"message": "账户管理",
"description": "账户管理部分的标题"
},
"addAccount": {
"message": "添加",
"description": "添加按钮的标签"
},
"trackedDomainManagement": {
"message": "追踪域名管理",
"description": "追踪域名管理部分的标题"
},
"domainExample": {
"message": "例如: google.com",
"description": "域名输入框的占位符"
},
"addNewDomain": {
"message": "添加",
"description": "新域名输入框的占位符"
},
"clearCookiesEnabled": {
"message": "开启对追踪域名的新选项卡清除cookie功能",
"description": "第一个复选框的描述"
},
"modifyLinkEnabled": {
"message": "限制追踪域名网页的链接只能在当前页面打开",
"description": "第二个复选框的描述"
},
"delete": {
"message": "删除",
"description": "删除按钮的标签"
},
"saveCookiesToAccount": {
"message": "保存Cookies到账户",
"description": "保存Cookies按钮的标题"
},
"loadCookiesFromAccount": {
"message": "从账户加载Cookies",
"description": "加载Cookies按钮的标题"
},
"deleteAccount": {
"message": "删除账户",
"description": "删除账户按钮的标题"
},
"deleteAllAccount": {
"message": "删除所有账户",
"description": "删除所有账户按钮的标题"
},
"clearAllClosedCookies": {
"message": "清除所有已关闭的Cookies",
"description": "清除所有已关闭的Cookies按钮的标题"
},
"clearCookies": {
"message": "清除Cookies",
"description": "清除Cookies按钮的标题"
},
"saveAllCookies": {
"message": "保存所有Cookies",
"description": "保存所有Cookies按钮的标题"
},
"loadAllCookies": {
"message": "加载所有Cookies",
"description": "加载所有Cookies按钮的标题"
},
"greeting": {
"message": "Hello",
"description": "Greeting text"
},
"farewell": {
"message": "Goodbye",
"description": "Farewell text"
}
}
1 change: 1 addition & 0 deletions manifest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export default defineManifest(async (env) => ({
// semver is OK in "version_name"
version_name: version,
manifest_version: 3,
default_locale: 'en',
// key: 'ekgmcbpgglflmgczajnglpbcbdccdnje',
action: {
default_popup: 'src/popup/index.html',
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"dependencies": {
"vue": "^3.3.4",
"vue-i18n": "^9.4.0",
"vue-router": "^4.2.2"
},
"devDependencies": {
Expand Down Expand Up @@ -57,5 +58,5 @@
"preview": "vite preview"
},
"type": "module",
"version": "1.0.1"
"version": "1.0.2"
}
36 changes: 36 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

53 changes: 33 additions & 20 deletions src/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,27 +233,41 @@ async function handleLoadAllCookies(loadSavedSelectedAccounts: boolean) {
// 监听标签页更新事件
chrome.tabs.onUpdated.addListener(async function (tabId, changeInfo, tab) {
try {
const tabIdDataMap: Record<number, { key: string; account: Account }> = await getStorageData('tabIdDataMap')
// 检查是否存在映射数据
if (Object.prototype.hasOwnProperty.call(tabIdDataMap, tabId) && changeInfo.status === 'complete') {
const { key, account } = tabIdDataMap[tabId]
console.log(`Tab ${tabId} loaded for account key in onUpdated: ${key}`)
const tab = await chrome.tabs.get(tabId) //At first it is currenttabID, but now I change it to tabID.
const [rootDomain, _] = (await processDomain(tab)) as [string, string]
await chrome.storage.sync.set({ selectedAccount: key })
await loadCookies(rootDomain, account.cookies)
delete tabIdDataMap[tabId] // 从映射中移除数据
await chrome.storage.local.set({ tabIdDataMap }) // 更新存储中的映射数据
await chrome.tabs.reload(tabId)
// 可能同时刷新多个标签页,要对符合条件的都执行脚本 9.12
}
// 可能同时刷新多个标签页,要对符合条件的都执行脚本 9.12
const modifyLinkEnabled = await getStorageData<boolean>('modifyLinkEnabled', false) //9.12
if (modifyLinkEnabled) {
modifyLinksInTab(tabId)
console.log('modifyLinksInTab已经触发')
}
}
catch(error) {
// const tabIdDataMap: Record<number, { key: string; account: Account }> = await getStorageData('tabIdDataMap')
// // 检查是否存在映射数据
// if (Object.prototype.hasOwnProperty.call(tabIdDataMap, tabId) && changeInfo.status === 'complete') {
// const { key, account } = tabIdDataMap[tabId]
// console.log(`Tab ${tabId} loaded for account key in onUpdated: ${key}`)
// const tab = await chrome.tabs.get(tabId) //At first it is currenttabID, but now I change it to tabID.
// const [rootDomain, _] = (await processDomain(tab)) as [string, string]
// await chrome.storage.sync.set({ selectedAccount: key })
// await loadCookies(rootDomain, account.cookies)
// delete tabIdDataMap[tabId] // 从映射中移除数据
// await chrome.storage.local.set({ tabIdDataMap }) // 更新存储中的映射数据
// await chrome.tabs.reload(tabId)
// }
//下面的代码是为了在刷新界面的时候如果目标网址是要追踪的网址那么就加载对应的cookies,主要是为上面的加载所有保存的账户的网页所服务的,因为它会立即切换到第一个网页,但是这时候还没有运行loadcookies的代码,所以需要在这里运行一下 9.12
const accounts = await getStorageData<Record<string, Account>>('accounts')
if (changeInfo.status === 'complete') {
for (const [key, account] of Object.entries(accounts)) {
if (account.refresh) {
if (key.includes(tabId.toString())) {
const [rootDomain, _] = (await processDomain(tab)) as [string, string]
await chrome.storage.sync.set({ selectedAccount: key })
await loadCookies(rootDomain, account.cookies)
await chrome.tabs.reload(tabId)
account.refresh = false
}
}
}
}
await chrome.storage.local.set({ accounts: accounts })
} catch (error) {
console.log('An error occurred in onUpdated:', error)
}
})
Expand Down Expand Up @@ -407,7 +421,7 @@ async function handleTabChange(tabId: number) {
//make new links only open in the current tab 8.26

// modifyLinksInTab(tabId) //It is not necessary to use await 9.9

// Load the appropriate cookies
if (key in accounts && accounts[key]) {
//让popup页面显示当前所在的账户,popup页面的显示是通过sync.get来实现的(由于每次打开会自动获取,所以不需要持续监听) 为什么要写把这个放在if语句里面?因为这说明这是一个新打开的页面,暂时不保存可以方便用户创建自己的账户
Expand Down Expand Up @@ -733,13 +747,12 @@ async function modifyLinksInTab(tabId: number) {
observer.observe(document.body, config)
},
})
console.log('Links and forms modified to open in the same tab.')
console.log('Links and forms are modified to open in the same tab.')
} catch (error) {
console.log('Failed to modify links and forms', error)
}
}


// 清除旧通知
const clearPreviousNotification = (notificationId: string) => {
chrome.notifications.clear(notificationId, (wasCleared) => {
Expand Down
2 changes: 1 addition & 1 deletion src/popup/index.scss
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
body {
width: 480px;
width: 355px;
margin: 0 auto;
}

Expand Down
23 changes: 22 additions & 1 deletion src/popup/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,27 @@ import routes from '~pages'
import '../assets/base.scss'
import App from './app.vue'
import './index.scss'
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 i18n = createI18n({
locale: 'en', // 设置默认语言
messages,
})

const router = createRouter({
history: createWebHashHistory(import.meta.env.BASE_URL),
Expand All @@ -14,4 +35,4 @@ router.beforeEach((to) => {
if (to.path === '/') return '/popup'
})

createApp(App).use(router).mount('#app')
createApp(App).use(router).use(i18n).mount('#app')
Loading

0 comments on commit 8902953

Please sign in to comment.