Skip to content

Commit

Permalink
🐛 处理*开头的特殊情况 #123
Browse files Browse the repository at this point in the history
  • Loading branch information
CodFrm committed Dec 12, 2022
1 parent 1073de3 commit 9569736
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 23 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "scriptcat",
"version": "0.11.0-beta.2",
"version": "0.11.0-beta.3",
"description": "脚本猫,一个可以执行用户脚本的浏览器扩展,万物皆可脚本化,让你的浏览器可以做更多的事情!",
"author": "CodFrm",
"license": "GPLv3",
Expand Down
27 changes: 16 additions & 11 deletions pkg/filesystem/auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,23 @@ export async function AuthVerify(netDiskType: NetDiskType, reapply?: boolean) {
}
if (Date.now() > token.createtime + 3600000) {
// 大于一小时刷新token
const resp = await RefreshToken(netDiskType, token.refreshToken);
if (resp.code !== 0) {
// 刷新失败删除token
localStorage.removeItem(`netdisk:token:${netDiskType}`);
return Promise.reject(new Error(resp.msg));
try {
const resp = await RefreshToken(netDiskType, token.refreshToken);
if (resp.code !== 0) {
// 刷新失败删除token
localStorage.removeItem(`netdisk:token:${netDiskType}`);
return Promise.reject(new Error(resp.msg));
}
token = {
accessToken: resp.data.token.access_token,
refreshToken: resp.data.token.refresh_token,
createtime: Date.now(),
};
localStorage[`netdisk:token:${netDiskType}`] = JSON.stringify(token);
} catch (e) {
// 报错返回原token
return Promise.resolve(token.accessToken);
}
token = {
accessToken: resp.data.token.access_token,
refreshToken: resp.data.token.refresh_token,
createtime: Date.now(),
};
localStorage[`netdisk:token:${netDiskType}`] = JSON.stringify(token);
} else {
return Promise.resolve(token.accessToken);
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/const.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const ExtVersion = "0.11.0-beta";
export const ExtVersion = "0.11.0-beta.3";

export const ExtServer = "https://ext.scriptcat.org/";

Expand Down
2 changes: 1 addition & 1 deletion src/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "ScriptCat",
"version": "0.11.0.1010",
"version": "0.11.0.1040",
"author": "CodFrm",
"description": "脚本猫,一个用户脚本管理器,支持后台脚本、定时脚本、页面脚本,可编写脚本每天帮你自动处理事务.",
"options_ui": {
Expand Down
14 changes: 14 additions & 0 deletions src/pkg/utils/match.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,20 @@ describe("UrlMatch-port2", () => {
});
});

describe("特殊情况", () => {
it("**", () => {
const url = new UrlMatch<string>();
url.add("*://**/*", "ok1");
expect(url.match("http://www.example.com/")).toEqual(["ok1"]);
});

it("prefix *",()=>{
const url=new UrlMatch<string>();
url.add("*https://www.baidu.com*","ok1");
expect(url.match("https://www.baidu.com")).toEqual(["ok1"]);
})
});

// --- include
describe("UrlInclude-1", () => {
const url = new UrlInclude<string>();
Expand Down
32 changes: 23 additions & 9 deletions src/pkg/utils/match.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
// eslint-disable-next-line max-classes-per-file
/* eslint-disable max-classes-per-file */

import Logger from "@App/app/logger/logger";

export default class Match<T> {
protected cache = new Map<string, T[]>();

protected rule = new Map<string, T[]>();

// eslint-disable-next-line class-methods-use-this
protected parseURL(url: string): Url | undefined {
if (url.indexOf("*http") === 0) {
url = url.substring(1);
}
const match = /^(.+?):\/\/(.*?)((\/.*?)(\?.*?|)|)$/.exec(url);
if (match) {
return {
Expand Down Expand Up @@ -44,7 +49,7 @@ export default class Match<T> {
default:
}
let pos = u.host.indexOf("*");
if (u.host === "*") {
if (u.host === "*" || u.host === "**") {
pos = -1;
} else if (u.host.endsWith("*")) {
// 处理*结尾
Expand Down Expand Up @@ -104,12 +109,21 @@ export default class Match<T> {
return ret;
}
ret = [];
this.rule.forEach((val, key) => {
const re = new RegExp(key);
if (re.test(url) && ret) {
ret.push(...val);
}
});
try {
this.rule.forEach((val, key) => {
const re = new RegExp(key);
if (re.test(url) && ret) {
ret.push(...val);
}
});
} catch (e) {
// eslint-disable-next-line no-console
console.warn("bad match rule", Logger.E(e));
// LoggerCore.getLogger({ component: "match" }).warn(
// "bad match rule",
// Logger.E(e)
// );
}
this.cache.set(url, ret);
return ret;
}
Expand Down

0 comments on commit 9569736

Please sign in to comment.