Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 兼容partition cookie #233

Merged
merged 1 commit into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "yuque-chrome-extension",
"version": "0.5.4",
"version": "0.5.5",
"description": "语雀浏览器插件",
"private": true,
"releaseNotes": [
"[优化] 增加保存失败的提示",
"[修复] 修复插件导致特殊网页卡住问题",
"[修复] 语雀文档剪藏列表丢失情况"
"[特性] 支持全文剪藏",
"[优化] 优化插件性能",
"[修复] 修复上传图片失败的异常处理"
],
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion src/background/core/httpClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
98 changes: 67 additions & 31 deletions src/background/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ chromeExtension.runtime.onInstalled.addListener(async details => {

createContextMenu();
updateDynamicRules();
updateYuqueCookieRule();

if (details.reason === 'install') {
chromeExtension.tabs.create({
Expand Down Expand Up @@ -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();
});
40 changes: 16 additions & 24 deletions src/core/bridge/background/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,36 +8,28 @@ export function createUserBridge(impl: ICallBridgeImpl) {
user: {
async login(): Promise<IUser | null> {
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<IShortcutMap> {
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({});
}
});
});
},
},
Expand Down
2 changes: 1 addition & 1 deletion src/core/webProxy/mine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export function createMineProxy() {
},
},
res => {
resolve(res.data.data);
resolve(res?.data?.data);
},
);
});
Expand Down
2 changes: 1 addition & 1 deletion src/pages/sidePanel/app.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
Loading