Skip to content

Commit b861869

Browse files
committed
🐛 修复某些时候超时才打开权限确认窗口的问题
1 parent 854dd26 commit b861869

File tree

4 files changed

+65
-49
lines changed

4 files changed

+65
-49
lines changed

src/apps/grant/background.ts

Lines changed: 49 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import { ScriptManager } from '../script/manager';
1616
import { Grant, Api, IPostMessage, IGrantListener, ConfirmParam, PermissionParam, FreedCallback } from './interface';
1717
import { v4 as uuidv4 } from 'uuid'
1818
import { ValueModel } from '@App/model/value';
19-
import { LOGGER_LEVEL_INFO } from '@App/model/do/logger';
19+
import { LOGGER_LEVEL, LOGGER_LEVEL_INFO } from '@App/model/do/logger';
2020
import { Permission } from '@App/model/do/permission';
2121
import { Script } from '@App/model/do/script';
2222
import { Value } from '@App/model/do/value';
@@ -344,10 +344,12 @@ export class BackgroundGrant {
344344
// 处理下一个
345345
const next = () => {
346346
// 一个打开确定,一群不打开只监听消息
347-
const confirm = list.pop();
348-
if (confirm) {
349-
void App.Cache.set('confirm:info:' + confirm.uuid, confirm);
350-
chrome.tabs.create({ url: chrome.runtime.getURL('confirm.html?uuid=' + confirm.uuid) });
347+
const item = list.pop();
348+
if (item) {
349+
void App.Cache.set('confirm:info:' + item.uuid, [item, list.length]);
350+
chrome.tabs.create({ url: chrome.runtime.getURL('confirm.html?uuid=' + item.uuid) });
351+
} else {
352+
void App.Cache.del('confirm:window:' + confirm.permission + ':list:' + script.id.toString());
351353
}
352354
}
353355
const listener = async (param: IPermissionConfirm) => {
@@ -919,31 +921,31 @@ export class BackgroundGrant {
919921
silent: details.silent,
920922
progress: details.progress
921923
};
922-
chrome.notifications.update(id, options);
924+
chrome.notifications.update(<string>id, options);
923925
return resolve(undefined);
924926
});
925927
}
926928

927929
@BackgroundGrant.GMFunction({ default: true, background: true })
928930
protected CAT_setLastRuntime(grant: Grant, post: IPostMessage): Promise<any> {
929931
return new Promise(resolve => {
930-
this.scriptMgr.setLastRuntime(grant.id, grant.params[0]);
932+
void this.scriptMgr.setLastRuntime(grant.id, <number>grant.params[0]);
931933
return resolve(undefined);
932934
});
933935
}
934936

935937
@BackgroundGrant.GMFunction({ default: true, background: true })
936938
protected CAT_setRunError(grant: Grant, post: IPostMessage): Promise<any> {
937939
return new Promise(resolve => {
938-
this.scriptMgr.setRunError(grant.id, grant.params[0], grant.params[1]);
940+
void this.scriptMgr.setRunError(grant.id, <string>grant.params[0], <number>grant.params[1]);
939941
return resolve(undefined);
940942
});
941943
}
942944

943945
@BackgroundGrant.GMFunction({ default: true, background: true })
944946
protected CAT_runComplete(grant: Grant, post: IPostMessage): Promise<any> {
945947
return new Promise(resolve => {
946-
this.scriptMgr.setRunComplete(grant.id);
948+
void this.scriptMgr.setRunComplete(grant.id);
947949
return resolve(undefined);
948950
});
949951
}
@@ -954,7 +956,7 @@ export class BackgroundGrant {
954956
if (grant.params.length == 0) {
955957
return reject('param is null');
956958
}
957-
App.Log.Logger(grant.params[1] ?? LOGGER_LEVEL_INFO, 'GM_log', grant.params[0], grant.name, grant.id);
959+
App.Log.Logger(<LOGGER_LEVEL>grant.params[1] ?? LOGGER_LEVEL_INFO, 'GM_log', <string>grant.params[0], grant.name, grant.id);
958960
AppEvent.trigger(ListenGmLog, {
959961
level: grant.params[1] ?? LOGGER_LEVEL_INFO,
960962
scriptId: grant.id,
@@ -967,36 +969,39 @@ export class BackgroundGrant {
967969
@BackgroundGrant.GMFunction()
968970
protected GM_setValue(grant: Grant, post: IPostMessage, script?: Script): Promise<any> {
969971
//getValue直接从缓存中返回了,无需编写
970-
return new Promise(async resolve => {
971-
const [key, value] = grant.params;
972-
let model: Value | undefined;
973-
if (script?.metadata['storagename']) {
974-
model = await this.valueModel.findOne({ storageName: script.metadata['storagename'][0], key: key });
975-
} else {
976-
model = await this.valueModel.findOne({ scriptId: script?.id, key: key });
977-
}
978-
if (!model) {
979-
model = {
980-
id: 0,
981-
scriptId: script?.id || 0,
982-
storageName: (script?.metadata['storagename'] && script?.metadata['storagename'][0]) || '',
983-
key: key,
984-
value: value,
985-
createtime: new Date().getTime()
972+
return new Promise(resolve => {
973+
const hanlder = async () => {
974+
const [key, value] = grant.params;
975+
let model: Value | undefined;
976+
if (script?.metadata['storagename']) {
977+
model = await this.valueModel.findOne({ storageName: script.metadata['storagename'][0], key: key });
978+
} else {
979+
model = await this.valueModel.findOne({ scriptId: script?.id, key: key });
980+
}
981+
if (!model) {
982+
model = {
983+
id: 0,
984+
scriptId: script?.id || 0,
985+
storageName: (script?.metadata['storagename'] && script?.metadata['storagename'][0]) || '',
986+
key: key,
987+
value: value,
988+
createtime: new Date().getTime()
989+
}
990+
} else {
991+
model.value = value;
992+
}
993+
994+
if (value === undefined) {
995+
void this.valueModel.delete(model.id);
996+
AppEvent.trigger(ScriptValueChange, { model, tabid: grant.tabId });
997+
return resolve(undefined);
986998
}
987-
} else {
988-
model.value = value;
989-
}
990999

991-
if (value === undefined) {
992-
this.valueModel.delete(model.id);
1000+
void this.valueModel.save(model);
9931001
AppEvent.trigger(ScriptValueChange, { model, tabid: grant.tabId });
994-
return resolve(undefined);
1002+
resolve(undefined);
9951003
}
996-
997-
this.valueModel.save(model);
998-
AppEvent.trigger(ScriptValueChange, { model, tabid: grant.tabId });
999-
resolve(undefined);
1004+
void hanlder();
10001005
})
10011006
}
10021007

@@ -1021,7 +1026,8 @@ export class BackgroundGrant {
10211026
regex = regex.replace('*', '(?:^|.*?)')
10221027
}
10231028
regex = regex.replace(/\//g, '\\/');
1024-
ret += `if(/${regex}/.test(url)){return "${val.proxyServer.scheme?.toUpperCase() || 'HTTP'} ${val.proxyServer.host}` + (val.proxyServer.port ? ':' + val.proxyServer.port : '') + '"}\n';
1029+
ret += `if(/${regex}/.test(url)){return "${val.proxyServer.scheme?.toUpperCase() || 'HTTP'} ${val.proxyServer.host}` +
1030+
(val.proxyServer.port ? ':' + val.proxyServer.port.toString() : '') + '"}\n';
10251031
});
10261032
});
10271033
}
@@ -1052,7 +1058,7 @@ export class BackgroundGrant {
10521058
})
10531059
protected CAT_setProxy(grant: Grant, post: IPostMessage): Promise<any> {
10541060
return new Promise(resolve => {
1055-
BackgroundGrant.proxyRule.set(grant.id, grant.params[0]);
1061+
BackgroundGrant.proxyRule.set(grant.id, <CAT_Types.ProxyRule[]>grant.params[0]);
10561062
App.Log.Debug('background', 'enable proxy', grant.name);
10571063
chrome.proxy.settings.set({
10581064
value: {
@@ -1102,6 +1108,7 @@ export class BackgroundGrant {
11021108
button: 'left',
11031109
clickCount: 1
11041110
}, (result) => {
1111+
console.log(result);
11051112
});
11061113
});
11071114
} else {
@@ -1120,6 +1127,7 @@ export class BackgroundGrant {
11201127
button: 'left',
11211128
clickCount: 1
11221129
}, (result) => {
1130+
console.log(result);
11231131
});
11241132
});
11251133
});
@@ -1130,18 +1138,18 @@ export class BackgroundGrant {
11301138
}
11311139

11321140
protected static textarea: HTMLElement = document.createElement('textarea');
1133-
public static clipboardData: any;
1141+
public static clipboardData: { type?: string, data: string } | undefined;
11341142

11351143
@BackgroundGrant.GMFunction({
11361144
listener: () => {
11371145
document.body.appendChild(BackgroundGrant.textarea);
11381146
document.addEventListener('copy', (e: ClipboardEvent) => {
1139-
if (!BackgroundGrant.clipboardData) {
1147+
if (!BackgroundGrant.clipboardData || !e.clipboardData) {
11401148
return;
11411149
}
11421150
e.preventDefault();
11431151
const { type, data } = BackgroundGrant.clipboardData;
1144-
(<any>e).clipboardData.setData(type || 'text/plain', data);
1152+
e.clipboardData.setData(type || 'text/plain', data);
11451153
BackgroundGrant.clipboardData = undefined;
11461154
})
11471155
}

src/apps/script/controller.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,10 @@ export class ScriptController {
189189
});
190190
}
191191

192-
public getConfirmInfo(uuid: string): Promise<ConfirmParam> {
192+
public getConfirmInfo(uuid: string): Promise<[ConfirmParam, number]> {
193193
return new Promise(resolve => {
194194
MsgCenter.sendMessage(RequestConfirmInfo, uuid, resp => {
195-
resolve(<ConfirmParam>resp);
195+
resolve(<[ConfirmParam, number]>resp);
196196
});
197197
});
198198
}

src/views/pages/Confirm/index.vue

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,12 @@
3030
<v-btn @click="allow(true, 5)" color="primary">
3131
总是允许此{{ param.permissionContent }}
3232
</v-btn>
33-
<v-btn v-if="param.wildcard" @click="allow(true, 4)" color="warning">
34-
总是允许全部{{ param.permissionContent }}
33+
<v-btn
34+
v-if="param.wildcard && num > 2"
35+
@click="allow(true, 4)"
36+
color="warning"
37+
>
38+
总是允许剩下{{ num }}个{{ param.permissionContent }}
3539
</v-btn>
3640
</div>
3741

@@ -46,8 +50,11 @@
4650
<v-btn @click="allow(false, 5)" color="error">
4751
总是拒绝此{{ param.permissionContent }}
4852
</v-btn>
49-
<v-btn v-if="param.wildcard" @click="allow(false, 4)" color="error"
50-
>总是拒绝全部{{ param.permissionContent }}</v-btn
53+
<v-btn
54+
v-if="param.wildcard && num > 2"
55+
@click="allow(false, 4)"
56+
color="error"
57+
>总是拒绝剩下{{ num }}个{{ param.permissionContent }}</v-btn
5158
>
5259
</div>
5360
</v-main>
@@ -65,6 +72,7 @@ export default class Confirm extends Vue {
6572
scriptConrtoller: ScriptController = new ScriptController();
6673
6774
protected param: ConfirmParam = <ConfirmParam>{};
75+
protected num = 0;
6876
protected timeout = 30;
6977
protected uuid = '';
7078
protected select = false;
@@ -75,7 +83,7 @@ export default class Confirm extends Vue {
7583
return;
7684
}
7785
this.uuid = uuid;
78-
this.param = await this.scriptConrtoller.getConfirmInfo(uuid);
86+
[this.param, this.num] = await this.scriptConrtoller.getConfirmInfo(uuid);
7987
let i = setInterval(() => {
8088
this.timeout--;
8189
if (!this.timeout) {

src/views/pages/Option/tabs/ScriptList.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1143,7 +1143,7 @@ export default class ScriptList extends Vue {
11431143
BackgroundGrant.clipboardData = {
11441144
data: msg,
11451145
};
1146-
document.execCommand('copy', false, <any>null);
1146+
document.execCommand('copy', false, undefined);
11471147
scriptModule.showSnackbar('复制成功');
11481148
}
11491149

0 commit comments

Comments
 (0)