Skip to content

Commit

Permalink
🐛 修复GM_xhr返回值和GM_delValue删除错误
Browse files Browse the repository at this point in the history
  • Loading branch information
CodFrm committed Jan 20, 2022
1 parent 82ceea9 commit 45fb304
Show file tree
Hide file tree
Showing 7 changed files with 204 additions and 157 deletions.
2 changes: 1 addition & 1 deletion build/scriptcat/manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"manifest_version": 2,
"name": "ScriptCat",
"version": "0.8.1",
"version": "0.8.2",
"description": "脚本猫,一个用户脚本的框架,可编写脚本每天帮你自动处理事务.",
"background": {
"page": "background.html"
Expand Down
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.8.1",
"version": "0.8.2",
"description": "脚本猫,一个可以执行用户脚本的浏览器扩展,万物皆可脚本化,让你的浏览器可以做更多的事情!",
"scripts": {
"lint": "eslint --ext .js,.ts,.tsx tests/ src/",
Expand Down
62 changes: 42 additions & 20 deletions src/apps/grant/background.ts
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,24 @@ export class BackgroundGrant {
return respond;
}

request = new Map<number, XMLHttpRequest>();
requestId = 0;

@BackgroundGrant.GMFunction({
alias: ['GM_xmlhttpRequest']
})
protected CAT_abortXhr(grant: Grant): Promise<void> {
return new Promise(resolve => {
const id = <number>grant.params[0];
const xhr = this.request.get(id);
if (xhr) {
xhr.abort();
this.request.delete(id);
}
resolve();
});
}

//TODO:按照tampermonkey文档实现
@BackgroundGrant.GMFunction({
confirm: (grant: Grant, script: Script) => {
Expand Down Expand Up @@ -550,6 +568,11 @@ export class BackgroundGrant {
const config = <GMSend.XHRDetails>grant.params[0];

const xhr = new XMLHttpRequest();

this.request.set(++this.requestId, xhr);
grant.data = { type: 'requestId', data: this.requestId };
post.postMessage(grant);

xhr.open(config.method || 'GET', config.url, true, config.user || '', config.password || '');
config.overrideMimeType && xhr.overrideMimeType(config.overrideMimeType);
if (config.responseType != 'json') {
Expand All @@ -564,7 +587,11 @@ export class BackgroundGrant {
}
}
grant.data = { type: event, data: respond };
post.postMessage(grant);
try {
post.postMessage(grant);
} catch (e) {
xhr.abort();
}
}
xhr.onload = () => {
deal('onload');
Expand Down Expand Up @@ -682,7 +709,7 @@ export class BackgroundGrant {
});
}
})
protected GM_cookie(grant: Grant, post: IPostMessage): Promise<any> {
protected GM_cookie(grant: Grant): Promise<any> {
return new Promise((resolve, reject) => {
const param = grant.params;
if (param.length != 2) {
Expand Down Expand Up @@ -795,14 +822,14 @@ export class BackgroundGrant {
active: param.active || false,
}, tab => {
resolve({ type: 'tabid', tabId: tab.id });
BackgroundGrant.tabMap.set(tab.id!, [grant, post]);
BackgroundGrant.tabMap.set(<number>tab.id, [grant, post]);
});
});
}

// 隐藏函数
@BackgroundGrant.GMFunction({ default: true })
protected GM_closeInTab(grant: Grant, post: IPostMessage): Promise<any> {
protected GM_closeInTab(grant: Grant): Promise<any> {
return new Promise(resolve => {
chrome.tabs.remove(<number>grant.params[0]);
resolve(undefined);
Expand Down Expand Up @@ -927,31 +954,31 @@ export class BackgroundGrant {
}

@BackgroundGrant.GMFunction({ default: true, background: true })
protected CAT_setLastRuntime(grant: Grant, post: IPostMessage): Promise<any> {
protected CAT_setLastRuntime(grant: Grant): Promise<any> {
return new Promise(resolve => {
void this.scriptMgr.setLastRuntime(grant.id, <number>grant.params[0]);
return resolve(undefined);
});
}

@BackgroundGrant.GMFunction({ default: true, background: true })
protected CAT_setRunError(grant: Grant, post: IPostMessage): Promise<any> {
protected CAT_setRunError(grant: Grant): Promise<any> {
return new Promise(resolve => {
void this.scriptMgr.setRunError(grant.id, <string>grant.params[0], <number>grant.params[1]);
return resolve(undefined);
});
}

@BackgroundGrant.GMFunction({ default: true, background: true })
protected CAT_runComplete(grant: Grant, post: IPostMessage): Promise<any> {
protected CAT_runComplete(grant: Grant): Promise<any> {
return new Promise(resolve => {
void this.scriptMgr.setRunComplete(grant.id);
return resolve(undefined);
});
}

@BackgroundGrant.GMFunction({ default: true })
protected GM_log(grant: Grant, post: IPostMessage): Promise<any> {
protected GM_log(grant: Grant): Promise<any> {
return new Promise((resolve, reject) => {
if (grant.params.length == 0) {
return reject('param is null');
Expand Down Expand Up @@ -990,13 +1017,12 @@ export class BackgroundGrant {
} else {
model.value = value;
}

if (value === undefined) {
if (value === undefined || value === null) {
model.value = undefined;
void this.valueModel.delete(model.id);
AppEvent.trigger(ScriptValueChange, { model, tabid: grant.tabId });
return resolve(undefined);
}

void this.valueModel.save(model);
AppEvent.trigger(ScriptValueChange, { model, tabid: grant.tabId });
resolve(undefined);
Expand Down Expand Up @@ -1056,7 +1082,7 @@ export class BackgroundGrant {
BackgroundGrant.freedProxy(id);
}
})
protected CAT_setProxy(grant: Grant, post: IPostMessage): Promise<any> {
protected CAT_setProxy(grant: Grant): Promise<any> {
return new Promise(resolve => {
BackgroundGrant.proxyRule.set(grant.id, <CAT_Types.ProxyRule[]>grant.params[0]);
App.Log.Debug('background', 'enable proxy', grant.name);
Expand All @@ -1073,7 +1099,7 @@ export class BackgroundGrant {
}

@BackgroundGrant.GMFunction({ background: true })
protected CAT_clearProxy(grant: Grant, post: IPostMessage): Promise<any> {
protected CAT_clearProxy(grant: Grant): Promise<any> {
return new Promise(resolve => {
BackgroundGrant.freedProxy(grant.id);
resolve(undefined);
Expand All @@ -1100,15 +1126,13 @@ export class BackgroundGrant {
y: param[1],
button: 'left',
clickCount: 1
}, (result) => {
}, () => {
chrome.debugger.sendCommand(target, 'Input.dispatchMouseEvent', {
type: 'mouseReleased',
x: param[0],
y: param[1],
button: 'left',
clickCount: 1
}, (result) => {
console.log(result);
});
});
} else {
Expand All @@ -1119,15 +1143,13 @@ export class BackgroundGrant {
y: param[1],
button: 'left',
clickCount: 1
}, (result) => {
}, () => {
chrome.debugger.sendCommand(target, 'Input.dispatchMouseEvent', {
type: 'mouseReleased',
x: param[0],
y: param[1],
button: 'left',
clickCount: 1
}, (result) => {
console.log(result);
});
});
});
Expand All @@ -1154,7 +1176,7 @@ export class BackgroundGrant {
})
}
})
public GM_setClipboard(grant: Grant, post: IPostMessage): Promise<any> {
public GM_setClipboard(grant: Grant): Promise<any> {
return new Promise(resolve => {
BackgroundGrant.clipboardData = {
type: grant.params[1],
Expand Down
Loading

0 comments on commit 45fb304

Please sign in to comment.