Skip to content

Commit

Permalink
🎨 修改GM_menu注册同名菜单只使第一个生效
Browse files Browse the repository at this point in the history
  • Loading branch information
CodFrm committed Jan 3, 2022
1 parent 3f5085f commit bd16121
Show file tree
Hide file tree
Showing 9 changed files with 285 additions and 219 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.7.7",
"version": "0.8.0",
"description": "脚本猫,一个可以执行用户脚本的浏览器扩展,万物皆可脚本化,让你的浏览器可以做更多的事情!",
"scripts": {
"lint": "eslint --ext .js,.ts,.tsx tests/ src/",
Expand Down
43 changes: 27 additions & 16 deletions src/apps/grant/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -365,6 +365,18 @@ export class BackgroundGrant {
}

public listenScriptGrant() {
chrome.tabs.onRemoved.addListener((tabId) => {
BackgroundGrant.freedCallback.forEach(v => {
v(0, tabId, true)
});
});
chrome.tabs.onUpdated.addListener((tabId, info) => {
if (info.status == 'loading' && !info.url) {
BackgroundGrant.freedCallback.forEach(v => {
v(0, tabId, false)
});
}
});
this.listener.listen((msg, postMessage) => {
return new Promise(resolve => {
const grant = <Grant>msg;
Expand All @@ -379,7 +391,7 @@ export class BackgroundGrant {
if (grant.value == 'CAT_runComplete' || (grant.value == 'CAT_setRunError' && grant.params[0])) {
//执行完毕,释放资源
BackgroundGrant.freedCallback.forEach(v => {
v(grant);
v(grant.id, grant.tabId);
});
}
resolve(result);
Expand Down Expand Up @@ -638,8 +650,7 @@ export class BackgroundGrant {
data.push({ storeId: val.id });
}
});
grant.data = { type: 'done', data: data };
post.postMessage(grant);
resolve({ type: 'done', data: data });
});
return;
}
Expand All @@ -664,8 +675,7 @@ export class BackgroundGrant {
url: detail.url,
storeId: detail.storeId,
}, (cookies) => {
grant.data = { type: 'done', data: cookies };
post.postMessage(grant);
resolve({ type: 'done', data: cookies });
});
break;
}
Expand All @@ -678,8 +688,7 @@ export class BackgroundGrant {
url: detail.url,
storeId: detail.storeId,
}, () => {
grant.data = { type: 'done', data: [] };
post.postMessage(grant);
resolve({ type: 'done', data: [] });
});
break;
}
Expand All @@ -698,8 +707,7 @@ export class BackgroundGrant {
secure: detail.secure,
storeId: detail.storeId,
}, () => {
grant.data = { type: 'done', data: [] };
post.postMessage(grant);
resolve({ type: 'done', data: [] });
});
break;
}
Expand Down Expand Up @@ -973,8 +981,8 @@ export class BackgroundGrant {

@BackgroundGrant.GMFunction({
background: true,
freed: (grant: Grant) => {
BackgroundGrant.freedProxy(grant.id);
freed: (id: number) => {
BackgroundGrant.freedProxy(id);
}
})
protected CAT_setProxy(grant: Grant, post: IPostMessage): Promise<any> {
Expand Down Expand Up @@ -1087,6 +1095,9 @@ export class BackgroundGrant {
menu.post.postMessage(menu.grant);
}
});
},
freed: () => {
console.log();
}
})
public GM_registerMenuCommand(grant: Grant, post: IPostMessage): Promise<any> {
Expand Down Expand Up @@ -1124,7 +1135,7 @@ export class BackgroundGrant {


@BackgroundGrant.GMFunction({})
public GM_unregisterMenuCommand(grant: Grant, post: IPostMessage): Promise<any> {
public GM_unregisterMenuCommand(grant: Grant): Promise<any> {
return new Promise(resolve => {
grant.params[0].scriptId = grant.id;
if (grant.tabId) {
Expand All @@ -1145,13 +1156,13 @@ export class BackgroundGrant {
public static tabDatas = new Map<number, Map<number, any>>();

@BackgroundGrant.GMFunction({
freed: (grant: Grant) => {
const datas = BackgroundGrant.tabDatas.get(grant.id);
freed: (id, tabId, windowClose) => {
const datas = BackgroundGrant.tabDatas.get(id);
if (!datas) {
return
}
if (grant.tabId) {
datas.delete(grant.tabId);
if (tabId && windowClose) {
datas.delete(tabId);
} else {
datas.delete(0);
}
Expand Down
9 changes: 6 additions & 3 deletions src/apps/grant/frontend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,11 @@ export class FrontendGrant implements ScriptContext {
@FrontendGrant.GMFunction()
public GM_registerMenuCommand(name: string, listener: () => any, accessKey?: string): number {
const id = randomInt(1, 100000);
this.postRequest('GM_registerMenuCommand', [{ name: name, accessKey: accessKey, id: id }], () => {
listener();
this.postRequest('GM_registerMenuCommand', [{ name: name, accessKey: accessKey, id: id }], (grant: Grant) => {
const data = <{ action: string }>grant.data;
if (data && data.action == 'click') {
listener();
}
});
return id;
}
Expand Down Expand Up @@ -531,7 +534,7 @@ export class FrontendGrant implements ScriptContext {
}

@FrontendGrant.GMFunction()
protected GM_getTabs(callback: (objs: { [key: number]: object }) => any): void {
protected GM_getTabs(callback: (objs: { [key: number]: object }) => any): void {
this.postRequest('GM_getTabs', [], (grant: Grant) => {
if (grant.error) {
throw new Error(grant.errorMsg);
Expand Down
4 changes: 3 additions & 1 deletion src/apps/grant/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ export interface IPostMessage {

export type Api = (grant: Grant, postMessage: IPostMessage, script?: Script) => Promise<any>;

export type FreedCallback = (grant: Grant) => void;
// 资源释放,后台脚本只有脚本id,当窗口是关闭而不是刷新时windowClose为true
// TODO: 实现脚本id
export type FreedCallback = (id: number, tabId?: number, windowClose?: boolean) => void;

export interface IGrantListener {
listen(callback: (msg: any, postMessage: IPostMessage) => Promise<any>): void
Expand Down

0 comments on commit bd16121

Please sign in to comment.