Skip to content

Commit 57d8e6b

Browse files
committed
优化沙盒功能 & 兼容firefox & 修复首次require失败的问题 & 优化代码结构
1 parent 90e59d2 commit 57d8e6b

36 files changed

+296
-332
lines changed

build/scriptcat/manifest.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"js": [
2626
"src/content.js"
2727
],
28+
"run_at": "document_start",
2829
"all_frames": true
2930
}
3031
],
@@ -33,6 +34,7 @@
3334
"proxy",
3435
"cookies",
3536
"storage",
37+
"debugger",
3638
"webRequest",
3739
"<all_urls>",
3840
"notifications",

docs/api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ declare namespace GM_Types {
8888

8989
#### GM_xmlhttpRequest
9090

91-
> 部分功能缺失
91+
> 部分功能缺失,cookie功能firefox暂不支持
9292
9393
```typescript
9494
declare function GM_xmlhttpRequest(details: GM_Types.XHRDetails): GM_Types.AbortHandle<void>;

docs/cat-api.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
本扩展特有的API将会以 CAT_ 开头进行定义,如有同步类型的API,也会使用 CAT.* 的方式进行定义.对于某些API为了使用方便会提供GM的别名.
66

77

8-
98
### 定义
109

1110

@@ -33,7 +32,6 @@ declare namespace CAT_Types {
3332
```
3433

3534

36-
3735
#### CAT_clearProxy
3836

3937
> 清理代理
@@ -43,10 +41,10 @@ declare function CAT_clearProxy(): void;
4341
```
4442

4543

46-
4744
#### CAT_click
48-
> 模拟点击
45+
> 真实点击
4946
47+
使用了[Input.dispatchMouseEvent](https://chromedevtools.github.io/devtools-protocol/tot/Input/#method-dispatchMouseEvent)实现,请确认元素在可视区域内,且坐标是相对于窗口的位置.
5048
```ts
51-
declare function CAT_click(el: HTMLElement): void;
52-
```
49+
declare function CAT_click(x: number, y: number): void
50+
```

docs/changelog.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* 支持`@require`引用其他脚本
66
* 将GM_set/getValue函数使其实时全局同步
77
* 添加了`CAT_click`API,可进行真实点击
8+
* 支持了`GM_setClipboard`
89

910
## v0.2.0
1011
> 第一个可用的版本

src/apps/grant/background.ts

Lines changed: 53 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
1-
import { LOGGER_LEVEL_INFO } from "@App/model/logger";
2-
import { Permission, PermissionModel } from "@App/model/permission";
3-
import { Script, SCRIPT_TYPE_BACKGROUND, SCRIPT_TYPE_CRONTAB } from "@App/model/script";
1+
import { PermissionModel } from "@App/model/permission";
42
import { isFirefox } from "@App/pkg/utils";
5-
import axios from "axios";
63
import { App } from "../app";
74
import { AppEvent, PermissionConfirm, ScriptGrant, ScriptValueChange } from "../msg-center/event";
85
import { MsgCenter } from "../msg-center/msg-center";
96
import { ScriptManager } from "../script/manager";
107
import { Grant, Api, IPostMessage, IGrantListener, ConfirmParam, PermissionParam, FreedCallback } from "./interface";
118
import { v4 as uuidv4 } from "uuid"
12-
import { Value, ValueModel } from "@App/model/value";
9+
import { ValueModel } from "@App/model/value";
10+
import { LOGGER_LEVEL_INFO } from "@App/model/do/logger";
11+
import { Permission } from "@App/model/do/permission";
12+
import { SCRIPT_TYPE_CRONTAB, SCRIPT_TYPE_BACKGROUND, Script } from "@App/model/do/script";
13+
import { Value } from "@App/model/do/value";
1314

1415
class postMessage implements IPostMessage {
1516

@@ -51,53 +52,56 @@ export class BackgroundGrant {
5152
private constructor(scriptMgr: ScriptManager, listener: IGrantListener) {
5253
this.listener = listener;
5354
this.scriptMgr = scriptMgr;
54-
//处理xhrcookie的问题
55-
chrome.webRequest.onBeforeSendHeaders.addListener((data) => {
56-
let setCookie = '';
57-
let cookie = '';
58-
let anonymous = false;
59-
let cookieIndex = -1;
60-
let requestHeaders: chrome.webRequest.HttpHeader[] = [];
61-
data.requestHeaders?.forEach((val, key) => {
62-
switch (val.name.toLowerCase()) {
63-
case "x-cat-" + this.rand + "-cookie": {
64-
setCookie = val.value || '';
65-
break;
66-
}
67-
case "x-cat-" + this.rand + "-anonymous": {
68-
anonymous = true;
69-
break;
70-
}
71-
case "cookie": {
72-
cookieIndex = key;
73-
cookie = val.value || '';
74-
break;
55+
//处理xhrcookie的问题,firefox不支持
56+
try {
57+
chrome.webRequest.onBeforeSendHeaders.addListener((data) => {
58+
let setCookie = '';
59+
let cookie = '';
60+
let anonymous = false;
61+
let cookieIndex = -1;
62+
let requestHeaders: chrome.webRequest.HttpHeader[] = [];
63+
data.requestHeaders?.forEach((val, key) => {
64+
switch (val.name.toLowerCase()) {
65+
case "x-cat-" + this.rand + "-cookie": {
66+
setCookie = val.value || '';
67+
break;
68+
}
69+
case "x-cat-" + this.rand + "-anonymous": {
70+
anonymous = true;
71+
break;
72+
}
73+
case "cookie": {
74+
cookieIndex = key;
75+
cookie = val.value || '';
76+
break;
77+
}
78+
default: {
79+
requestHeaders.push(val);
80+
}
7581
}
76-
default: {
77-
requestHeaders.push(val);
82+
});
83+
if (anonymous) {
84+
cookie = '';
85+
}
86+
if (setCookie) {
87+
if (!cookie || cookie.endsWith(';')) {
88+
cookie += setCookie;
89+
} else {
90+
cookie += ';' + setCookie;
7891
}
7992
}
80-
});
81-
if (anonymous) {
82-
cookie = '';
83-
}
84-
if (setCookie) {
85-
if (!cookie || cookie.endsWith(';')) {
86-
cookie += setCookie;
87-
} else {
88-
cookie += ';' + setCookie;
93+
cookie && requestHeaders.push({
94+
name: 'cookie',
95+
value: cookie
96+
});
97+
return {
98+
requestHeaders: requestHeaders,
8999
}
90-
}
91-
cookie && requestHeaders.push({
92-
name: 'cookie',
93-
value: cookie
94-
});
95-
return {
96-
requestHeaders: requestHeaders,
97-
}
98-
}, {
99-
urls: ["<all_urls>"],
100-
}, ["blocking", "requestHeaders", "extraHeaders"]);
100+
}, {
101+
urls: ["<all_urls>"],
102+
}, ["blocking", "requestHeaders", "extraHeaders"]);
103+
} catch (e) {
104+
}
101105
}
102106

103107
// 单实例
@@ -329,7 +333,7 @@ export class BackgroundGrant {
329333
resolve(ret);
330334
});
331335
},
332-
alias: ['GMSC_xmlhttpRequest', 'GM.fetch'],
336+
alias: ['GM.fetch'],
333337
})
334338
protected GM_xmlhttpRequest(grant: Grant, post: IPostMessage): Promise<any> {
335339
return new Promise(resolve => {
@@ -736,7 +740,6 @@ export class BackgroundGrant {
736740
document.addEventListener('copy', (e: ClipboardEvent) => {
737741
e.preventDefault();
738742
let { type, data } = BackgroundGrant.clipboardData;
739-
console.log(type, data, BackgroundGrant.clipboardData);
740743
(<any>e).clipboardData.setData(type || 'text/plain', data);
741744
})
742745
}

src/apps/grant/frontend.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { ScriptCache } from "@App/model/script";
2-
import { Value } from "@App/model/value";
1+
import { ScriptCache } from "@App/model/do/script";
2+
import { Value } from "@App/model/do/value";
33
import { randomString } from "@App/pkg/utils";
44
import { BrowserMsg } from "../msg-center/browser";
55
import { AppEvent, ScriptValueChange } from "../msg-center/event";
@@ -131,16 +131,6 @@ export class FrontendGrant implements ScriptContext {
131131
});
132132
}
133133

134-
@FrontendGrant.GMFunction({ depend: ['GM_xmlhttpRequest'] })
135-
public GMSC_xmlhttpRequest(details: GM_Types.XHRDetails): Promise<GM_Types.XHRResponse> {
136-
return new Promise(resolve => {
137-
details.onload = (xhr) => {
138-
resolve(xhr);
139-
}
140-
this.GM_xmlhttpRequest(details);
141-
});
142-
}
143-
144134
public GM_notification(text: string, title: string, image: string, onclick: Function): void
145135

146136
@FrontendGrant.GMFunction()
@@ -261,8 +251,8 @@ export class FrontendGrant implements ScriptContext {
261251
}
262252

263253
@FrontendGrant.GMFunction()
264-
public CAT_click(el: HTMLElement): void {
265-
this.postRequest('CAT_click', [el.offsetLeft, el.offsetTop]);
254+
public CAT_click(x: number, y: number): void {
255+
this.postRequest('CAT_click', [x, y]);
266256
}
267257

268258
@FrontendGrant.GMFunction()

src/apps/grant/interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Script } from "@App/model/script";
1+
import { Script } from "@App/model/do/script";
22

33
export interface Grant {
44
value: string

src/apps/logger/logger.ts

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
1-
import {
2-
Log,
3-
LoggerModel,
4-
LOGGER_LEVEL,
5-
LOGGER_LEVEL_DEBUG,
6-
LOGGER_LEVEL_ERROR,
7-
LOGGER_LEVEL_INFO,
8-
LOGGER_LEVEL_WARN,
9-
LOGGER_TYPE,
10-
LOGGER_TYPE_SYSTEM,
11-
} from "@App/model/logger";
1+
import { LOGGER_LEVEL, Log, LOGGER_LEVEL_INFO, LOGGER_LEVEL_WARN, LOGGER_LEVEL_ERROR, LOGGER_LEVEL_DEBUG } from "@App/model/do/logger";
2+
import { LoggerModel } from "@App/model/logger";
123

134
export interface Logger {
145
// todo 可以改造为可调用实例

src/apps/msg-center/browser.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export class BrowserMsg {
1212
constructor(id: string) {
1313
this.id = id;
1414
document.addEventListener(this.id, event => {
15-
let detail = (<any>event).detail
15+
let detail = JSON.parse((<any>event).detail);
1616
let ret = this.listenMap.get(detail.topic);
1717
if (ret) {
1818
ret(detail.msg);
@@ -21,11 +21,12 @@ export class BrowserMsg {
2121
}
2222

2323
public send(topic: string, msg: any) {
24+
// 兼容火狐的序列化
2425
let ev = new CustomEvent(this.id, {
25-
detail: {
26+
detail: JSON.stringify({
2627
topic: topic,
2728
msg: msg,
28-
},
29+
}),
2930
});
3031
document.dispatchEvent(ev);
3132
}

src/apps/resource.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { hash, Resource, ResourceLinkModel, ResourceModel } from "@App/model/resource";
1+
import { Resource } from "@App/model/do/resource";
2+
import { ResourceLinkModel, ResourceModel } from "@App/model/resource";
23
import { SystemConfig } from "@App/pkg/config";
34
import axios from "axios";
45
import crypto from "crypto-js";
@@ -11,20 +12,19 @@ export class ResourceManager {
1112
public addResource(url: string, scriptId: number): Promise<boolean> {
1213
return new Promise(async resolve => {
1314
let u = this.parseUrl(url);
14-
this.getResource(u.url).then(async result => {
15-
if (!result) {
16-
let resource = await this.loadByUrl(u.url);
17-
if (!resource) {
18-
return;
19-
}
20-
resource.createtime = new Date().getTime();
21-
resource.updatetime = new Date().getTime();
22-
await App.Cache.set('resource:' + u.url, resource);
23-
if (await this.model.save(resource)) {
24-
App.Log.Info("resource", u.url, "add");
25-
}
15+
let result = await this.getResource(u.url);
16+
if (!result) {
17+
let resource = await this.loadByUrl(u.url);
18+
if (!resource) {
19+
return resolve(false);
2620
}
27-
});
21+
resource.createtime = new Date().getTime();
22+
resource.updatetime = new Date().getTime();
23+
await App.Cache.set('resource:' + u.url, resource);
24+
if (await this.model.save(resource)) {
25+
App.Log.Info("resource", u.url, "add");
26+
}
27+
}
2828

2929
let link = await this.linkModel.findOne({ url: u.url, scriptId: scriptId });
3030
if (link) {

src/apps/script/background.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { Script, ScriptCache } from "@App/model/script";
2-
import { Value } from "@App/model/value";
3-
import App from "@App/views/pages/Option";
1+
import { ScriptCache, Script } from "@App/model/do/script";
2+
import { Value } from "@App/model/do/value";
43
import { CronJob } from "cron";
54
import { AppEvent, ScriptValueChange } from "../msg-center/event";
65
import { IScript } from "./interface";

src/apps/script/interface.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Script, ScriptCache } from "@App/model/script";
1+
import { ScriptCache, Script } from "@App/model/do/script";
22

33
export interface IScript {
44
// 用于自动启动

0 commit comments

Comments
 (0)