From 1c8c8e69fae5dcc285f99d12cdfe3957217abf22 Mon Sep 17 00:00:00 2001 From: moshangqi <2509678669@qq.com> Date: Wed, 3 Jan 2024 20:07:20 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=85=BC=E5=AE=B9partition=20cookie?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- package.json | 8 +-- src/background/core/httpClient.ts | 2 +- src/background/index.ts | 98 ++++++++++++++++++++---------- src/core/bridge/background/user.ts | 40 +++++------- src/core/webProxy/mine.ts | 2 +- src/pages/sidePanel/app.tsx | 2 +- 6 files changed, 90 insertions(+), 62 deletions(-) diff --git a/package.json b/package.json index a10f95c5..e55d0df6 100644 --- a/package.json +++ b/package.json @@ -1,12 +1,12 @@ { "name": "yuque-chrome-extension", - "version": "0.5.4", + "version": "0.5.5", "description": "语雀浏览器插件", "private": true, "releaseNotes": [ - "[优化] 增加保存失败的提示", - "[修复] 修复插件导致特殊网页卡住问题", - "[修复] 语雀文档剪藏列表丢失情况" + "[特性] 支持全文剪藏", + "[优化] 优化插件性能", + "[修复] 修复上传图片失败的异常处理" ], "repository": { "type": "git", diff --git a/src/background/core/httpClient.ts b/src/background/core/httpClient.ts index 514a0e15..a1287a8c 100644 --- a/src/background/core/httpClient.ts +++ b/src/background/core/httpClient.ts @@ -127,11 +127,11 @@ export default class HttpClient { private async handlePrepareRequestParams(url: string, _config: IRequestConfig, _option?: IRequestOptions) { const headers = await this.prepareHeaders(); const config: RequestInit = { + ..._config, headers: { ...headers, ..._config.headers, }, - ..._config, }; let queryString = ''; if (_option?.isFileUpload) { diff --git a/src/background/index.ts b/src/background/index.ts index 75c82e36..6e92c807 100644 --- a/src/background/index.ts +++ b/src/background/index.ts @@ -24,6 +24,7 @@ chromeExtension.runtime.onInstalled.addListener(async details => { createContextMenu(); updateDynamicRules(); + updateYuqueCookieRule(); if (details.reason === 'install') { chromeExtension.tabs.create({ @@ -95,37 +96,72 @@ chromeExtension.storage.local.onChanged.addListener(res => { }); function updateDynamicRules() { - const rules = [ - { - id: 1, - action: { - type: chromeExtension.declarativeNetRequest.RuleActionType.MODIFY_HEADERS, - requestHeaders: [ - { - header: 'Referer', - operation: chromeExtension.declarativeNetRequest.HeaderOperation.SET, - value: YUQUE_DOMAIN, - }, - { - header: 'Origin', - operation: chromeExtension.declarativeNetRequest.HeaderOperation.SET, - value: YUQUE_DOMAIN, - }, - ], - }, - condition: { - domains: [chromeExtension.runtime.id], - urlFilter: `${YUQUE_DOMAIN}/api/upload/attach`, - resourceTypes: [chromeExtension.declarativeNetRequest.ResourceType.XMLHTTPREQUEST], + chromeExtension.declarativeNetRequest.updateDynamicRules({ + removeRuleIds: [1], + addRules: [ + { + id: 1, + action: { + type: chromeExtension.declarativeNetRequest.RuleActionType.MODIFY_HEADERS, + requestHeaders: [ + { + header: 'Referer', + operation: chromeExtension.declarativeNetRequest.HeaderOperation.SET, + value: YUQUE_DOMAIN, + }, + { + header: 'Origin', + operation: chromeExtension.declarativeNetRequest.HeaderOperation.SET, + value: YUQUE_DOMAIN, + }, + ], + }, + condition: { + domains: [chromeExtension.runtime.id], + urlFilter: `${YUQUE_DOMAIN}/api/upload/attach`, + resourceTypes: [chromeExtension.declarativeNetRequest.ResourceType.XMLHTTPREQUEST], + }, }, - }, - ]; - - chromeExtension.declarativeNetRequest.getDynamicRules(previousRules => { - const previousRuleIds = previousRules.map(rule => rule.id); - chromeExtension.declarativeNetRequest.updateDynamicRules({ - removeRuleIds: previousRuleIds, - addRules: rules, - }); + ], }); } + +const updateYuqueCookieRule = async () => { + // 通用 cookie + const normalCookie = await chromeExtension.cookies.getAll({ url: YUQUE_DOMAIN }); + // 分区 cookie + const partitionCookie = await chromeExtension.cookies.getAll({ partitionKey: { topLevelSite: 'https://yuque.com' } }); + const cookieArray = normalCookie.concat(partitionCookie || []).map(item => { + return `${item.name}=${item.value}`; + }); + chromeExtension.declarativeNetRequest.updateDynamicRules({ + removeRuleIds: [2], + addRules: [ + { + id: 2, + action: { + type: chromeExtension.declarativeNetRequest.RuleActionType.MODIFY_HEADERS, + requestHeaders: [ + { + header: 'Cookie', + operation: chromeExtension.declarativeNetRequest.HeaderOperation.SET, + value: cookieArray.join(';'), + }, + ], + }, + condition: { + domains: [chromeExtension.runtime.id], + urlFilter: `${YUQUE_DOMAIN}`, + resourceTypes: [chromeExtension.declarativeNetRequest.ResourceType.XMLHTTPREQUEST], + }, + }, + ], + }); +}; + +chromeExtension.cookies.onChanged.addListener(async res => { + if (!res.cookie.domain.includes('yuque.com') || res.removed) { + return; + } + updateYuqueCookieRule(); +}); diff --git a/src/core/bridge/background/user.ts b/src/core/bridge/background/user.ts index 20f35215..81aa24e9 100644 --- a/src/core/bridge/background/user.ts +++ b/src/core/bridge/background/user.ts @@ -8,36 +8,28 @@ export function createUserBridge(impl: ICallBridgeImpl) { user: { async login(): Promise { return new Promise(resolve => { - impl( - BackgroundEvents.OperateUser, - { type: OperateUserEnum.login }, - res => { - resolve(res || null); - }, - ); + impl(BackgroundEvents.OperateUser, { type: OperateUserEnum.login }, res => { + resolve(res || null); + }); }); }, async getUserShortCut(): Promise { return new Promise(resolve => { - impl( - BackgroundEvents.OperateUser, - { type: OperateUserEnum.getUserShortCut }, - res => { - try { - const map: IShortcutMap = {}; - for (const item of res) { - if (item.name === '_execute_action') { - map.openSidePanel = item.shortcut; - } else { - map[item.name as keyof IShortcutMap] = item.shortcut; - } + impl(BackgroundEvents.OperateUser, { type: OperateUserEnum.getUserShortCut }, res => { + try { + const map: IShortcutMap = {}; + for (const item of res) { + if (item.name === '_execute_action') { + map.openSidePanel = item.shortcut; + } else { + map[item.name as keyof IShortcutMap] = item.shortcut; } - resolve(map); - } catch (error) { - resolve({}); } - }, - ); + resolve(map); + } catch (error) { + resolve({}); + } + }); }); }, }, diff --git a/src/core/webProxy/mine.ts b/src/core/webProxy/mine.ts index 1ef620f7..fc8c3e76 100644 --- a/src/core/webProxy/mine.ts +++ b/src/core/webProxy/mine.ts @@ -76,7 +76,7 @@ export function createMineProxy() { }, }, res => { - resolve(res.data.data); + resolve(res?.data?.data); }, ); }); diff --git a/src/pages/sidePanel/app.tsx b/src/pages/sidePanel/app.tsx index d48e564f..67612c9a 100644 --- a/src/pages/sidePanel/app.tsx +++ b/src/pages/sidePanel/app.tsx @@ -60,7 +60,7 @@ function App() { STORAGE_KEYS.CURRENT_ACCOUNT, ); const user = await webProxy.mine.getUserInfo(); - if (user.id !== info.id) { + if (user?.id !== info?.id) { await storage.remove(STORAGE_KEYS.CURRENT_ACCOUNT); } Tracert.start({