Skip to content

Commit d9b9af0

Browse files
committed
🎨 修改eslint代码
1 parent 9eb92cc commit d9b9af0

8 files changed

Lines changed: 305 additions & 333 deletions

File tree

.eslintrc.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ module.exports = {
8787

8888
"@typescript-eslint/no-explicit-any": ["off"],
8989
"@typescript-eslint/no-unsafe-assignment": ["off"],
90+
"@typescript-eslint/no-implied-eval": ["off"],
91+
9092
// '@typescript-eslint/no-unsafe-return': ['off'],
9193
// '@typescript-eslint/no-unsafe-call': ['off'],
9294
// '@typescript-eslint/no-empty-function': ['off'],

src/apps/grant/frontend.ts

Lines changed: 129 additions & 122 deletions
Large diffs are not rendered by default.

src/apps/msg-center/browser.ts

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
11

22
// 前端用通信
33

4-
import { randomString } from "@App/pkg/utils/utils";
5-
64
export type ListenMsg = (msg: any) => void;
75

86
// 浏览器页面之间的通信,主要在content和injected页面之间
9-
export class BrowserMsg {
7+
8+
export interface BrowserMsg {
9+
send(topic: string, msg: any): void;
10+
11+
listen(topic: string, callback: ListenMsg): void;
12+
}
13+
14+
export class FrontendMsg implements BrowserMsg {
1015

1116
public id: string;
1217

@@ -17,29 +22,29 @@ export class BrowserMsg {
1722
constructor(id: string, content: boolean) {
1823
this.id = id;
1924
this.content = content;
20-
document.addEventListener(this.id + (content ? 'ct' : 'fd'), (event: any) => {
21-
let detail = event.detail;
22-
let topic = detail.topic;
23-
let listen = this.listenMap.get(topic);
25+
document.addEventListener(this.id + (content ? 'ct' : 'fd'), (event: unknown) => {
26+
const detail = (<{ detail: { msg: any, topic: string } }>event).detail;
27+
const topic = detail.topic;
28+
const listen = this.listenMap.get(topic);
2429
if (listen) {
2530
listen(detail.msg);
2631
}
2732
});
2833
}
2934

3035
public send(topic: string, msg: any) {
31-
let detail = Object.assign({}, {
36+
let detail = <{ topic: string, msg: any }>Object.assign({}, {
3237
topic: topic,
3338
msg: msg,
3439
});
35-
if ((<any>global).cloneInto) {
40+
if ((<{ cloneInto?: (detail: any, view: any) => { topic: string, msg: any } }><unknown>global).cloneInto) {
3641
try {
37-
detail = (<any>global).cloneInto(detail, document.defaultView);
42+
detail = (<{ cloneInto: (detail: any, view: any) => { topic: string, msg: any } }><unknown>global).cloneInto(detail, document.defaultView);
3843
} catch (e) {
3944
console.log(e);
4045
}
4146
}
42-
let ev = new CustomEvent(this.id + (this.content ? 'fd' : 'ct'), {
47+
const ev = new CustomEvent(this.id + (this.content ? 'fd' : 'ct'), {
4348
detail: detail,
4449
});
4550
document.dispatchEvent(ev);

src/content.ts

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,41 @@
1-
import { ExternalWhitelist } from "./apps/config";
2-
import { Grant } from "./apps/grant/interface";
3-
import { BrowserMsg } from "./apps/msg-center/browser";
4-
import { ExternalMessage, ScriptExec, ScriptGrant, ScriptValueChange } from "./apps/msg-center/event";
5-
import { MsgCenter } from "./apps/msg-center/msg-center";
6-
import { ScriptCache } from "./model/do/script";
1+
import { Grant } from './apps/grant/interface';
2+
import { FrontendMsg } from './apps/msg-center/browser';
3+
import { ExternalMessage, ScriptExec, ScriptGrant, ScriptValueChange } from './apps/msg-center/event';
4+
import { MsgCenter } from './apps/msg-center/msg-center';
5+
import { ScriptCache } from './model/do/script';
76

8-
chrome.runtime.sendMessage("runScript", (event: any) => {
9-
let scripts = <ScriptCache[]>event.scripts;
10-
let flag = event.flag;
11-
let browserMsg = new BrowserMsg(flag, true);
7+
chrome.runtime.sendMessage('runScript', (event: unknown) => {
8+
const { flag, scripts } = (<{ scripts: ScriptCache[], flag: string }>event);
9+
const browserMsg = new FrontendMsg(flag, true);
1210

1311
browserMsg.send('scripts', scripts);
14-
browserMsg.listen('grant', async msg => {
15-
switch (msg.value) {
16-
case 'CAT_fetchBlob':
17-
let resp = await (await fetch(msg.params[0])).blob();
18-
msg.data = (<any>global).cloneInto ? (<any>global).cloneInto(resp, document.defaultView) : resp;
19-
browserMsg.send(msg.flag!, msg);
20-
break;
21-
default:
22-
// NOTE: 好像没处理释放问题
23-
MsgCenter.connect(ScriptGrant, msg).addListener((msg: Grant, port: chrome.runtime.Port) => {
24-
browserMsg.send(msg.flag!, msg);
25-
});
12+
browserMsg.listen('grant', (msg: { value: string, params: any[], flag: string, data: any }) => {
13+
const handler = async () => {
14+
switch (msg.value) {
15+
case 'CAT_fetchBlob':
16+
const resp = await (await fetch(<RequestInfo>msg.params[0])).blob();
17+
msg.data = (<{ cloneInto?: (detail: any, view: any) => any }><unknown>global).cloneInto ?
18+
(<{ cloneInto: (detail: any, view: any) => any }><unknown>global).cloneInto(resp, document.defaultView) : resp;
19+
browserMsg.send(msg.flag, msg);
20+
break;
21+
default:
22+
// NOTE: 好像没处理释放问题
23+
MsgCenter.connect(ScriptGrant, msg).addListener((msg: Grant) => {
24+
browserMsg.send(msg.flag || '', msg);
25+
});
26+
}
2627
}
28+
void handler();
2729
});
2830
MsgCenter.connect(ScriptValueChange, 'init').addListener((msg: any) => {
2931
browserMsg.send(ScriptValueChange, msg);
3032
});
3133
browserMsg.listen(ExternalMessage, msg => {
32-
MsgCenter.connect(ExternalMessage, msg).addListener((msg, port) => {
34+
MsgCenter.connect(ExternalMessage, msg).addListener((msg) => {
3335
browserMsg.send(ExternalMessage, msg);
3436
});
3537
});
36-
chrome.runtime.onMessage.addListener((event) => {
38+
chrome.runtime.onMessage.addListener((event: { action: string, uuid: string }) => {
3739
switch (event.action) {
3840
case ScriptExec:
3941
browserMsg.send(ScriptExec, event.uuid);
@@ -42,9 +44,12 @@ chrome.runtime.sendMessage("runScript", (event: any) => {
4244
});
4345

4446
// 处理blob
45-
browserMsg.listen("fetchBlob", async msg => {
46-
let ret = await fetch(msg.url);
47-
browserMsg.send("fetchBlob", { url: msg.url, id: msg.id, ret });
47+
browserMsg.listen('fetchBlob', (msg: { url: string, id: string }) => {
48+
const handler = async () => {
49+
const ret = await fetch(msg.url);
50+
browserMsg.send('fetchBlob', { url: msg.url, id: msg.id, ret });
51+
}
52+
void handler();
4853
})
4954
});
5055

src/injected.ts

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
// splitChunks对injected可能会有问题
22

3-
import { ExternalWhitelist } from "./apps/config";
4-
import { FrontendGrant, ScriptContext } from "./apps/grant/frontend";
5-
import { BrowserMsg } from "./apps/msg-center/browser";
6-
import { ExternalMessage, ScriptExec, ScriptValueChange } from "./apps/msg-center/event";
7-
import { ScriptCache } from "./model/do/script";
8-
import { Value } from "./model/do/value";
9-
import { addStyle } from "./pkg/frontend";
10-
import { createContext } from "./pkg/sandbox/compile";
11-
import { buildThis } from "./pkg/sandbox/sandbox";
3+
import { ExternalWhitelist } from './apps/config';
4+
import { FrontendGrant, ScriptContext } from './apps/grant/frontend';
5+
import { FrontendMsg } from './apps/msg-center/browser';
6+
import { ExternalMessage, ScriptExec, ScriptValueChange } from './apps/msg-center/event';
7+
import { ScriptCache } from './model/do/script';
8+
import { Value } from './model/do/value';
9+
import { addStyle } from './pkg/frontend';
10+
import { createContext } from './pkg/sandbox/compile';
11+
import { buildThis } from './pkg/sandbox/sandbox';
1212

1313
// 参考了tm的实现
1414
function waitBody(callback: () => void) {
1515
if (document.body) {
1616
return callback();
1717
}
18-
let listen = function () {
18+
const listen = function () {
1919
document.removeEventListener('load', listen, false);
2020
document.removeEventListener('DOMNodeInserted', listen, false);
2121
document.removeEventListener('DOMContentLoaded', listen, false);
@@ -26,9 +26,9 @@ function waitBody(callback: () => void) {
2626
document.addEventListener('DOMContentLoaded', listen, false);
2727
};
2828

29-
let browserMsg = new BrowserMsg(ScriptFlag, false);
30-
browserMsg.listen("scripts", (msg) => {
31-
let scripts: ScriptCache[] = msg;
29+
const browserMsg = new FrontendMsg(ScriptFlag, false);
30+
browserMsg.listen('scripts', (msg) => {
31+
const scripts: ScriptCache[] = msg;
3232
browserMsg.listen(ScriptValueChange, (msg: Value) => {
3333
scripts.forEach(val => {
3434
if (!val.value) {
@@ -42,15 +42,15 @@ browserMsg.listen("scripts", (msg) => {
4242
browserMsg.listen(ScriptExec, (msg) => {
4343
for (let i = 0; i < scripts.length; i++) {
4444
if (scripts[i].uuid == msg) {
45-
(<any>window)[scripts[i].flag!].apply(scripts[i].context, [scripts[i].context]);
45+
(<{ [key: string]: (context: ScriptContext) => void }><unknown>window)[scripts[i].flag].apply(scripts[i].context, [scripts[i].context]);
4646
break;
4747
}
4848
}
4949
});
5050
scripts.forEach(script => {
5151
// 构建沙盒
5252
let context: ScriptContext;
53-
if (script.grantMap!['none']) {
53+
if (script.grantMap['none']) {
5454
context = <any>window;
5555
} else {
5656
context = new FrontendGrant(script, browserMsg);
@@ -62,18 +62,18 @@ browserMsg.listen("scripts", (msg) => {
6262
if (script.metadata['run-at'] && (script.metadata['run-at'][0] === 'document-menu' || script.metadata['run-at'][0] === 'document-body')) {
6363
if (script.metadata['run-at'][0] === 'document-body') {
6464
waitBody(() => {
65-
if ((<any>window)[script.flag!]) {
66-
(<any>window)[script.flag!].apply(context, [context]);
65+
if ((<{ [key: string]: () => void }><unknown>window)[script.flag]) {
66+
(<{ [key: string]: (context: ScriptContext) => void }><unknown>window)[script.flag].apply(context, [context]);
6767
}
68-
Object.defineProperty(window, script.flag!, {
68+
Object.defineProperty(window, script.flag, {
6969
get: () => { return undefined; },
70-
set: (val) => {
70+
set: (val: (context: ScriptContext) => void) => {
7171
val.apply(context, [context]);
7272
}
7373
});
7474
// 注入css
7575
script.metadata['require-css']?.forEach(val => {
76-
let res = script.resource![val];
76+
const res = script.resource[val];
7777
if (res) {
7878
addStyle(res.content);
7979
}
@@ -82,18 +82,18 @@ browserMsg.listen("scripts", (msg) => {
8282
}
8383
return;
8484
}
85-
if ((<any>window)[script.flag!]) {
86-
(<any>window)[script.flag!].apply(context, [context]);
85+
if ((<{ [key: string]: () => void }><unknown>window)[script.flag]) {
86+
(<{ [key: string]: (context: ScriptContext) => void }><unknown>window)[script.flag].apply(context, [context]);
8787
}
88-
Object.defineProperty(window, script.flag!, {
88+
Object.defineProperty(window, script.flag, {
8989
get: () => { return undefined; },
90-
set: (val) => {
90+
set: (val: (context: ScriptContext) => void) => {
9191
val.apply(context, [context]);
9292
}
9393
});
9494
// 注入css
9595
script.metadata['require-css']?.forEach(val => {
96-
let res = script.resource![val];
96+
const res = script.resource[val];
9797
if (res) {
9898
addStyle(res.content);
9999
}
@@ -107,22 +107,23 @@ browserMsg.listen("scripts", (msg) => {
107107
for (let i = 0; i < ExternalWhitelist.length; i++) {
108108
if (window.location.host.endsWith(ExternalWhitelist[i])) {
109109
// 注入
110-
let isInstalledCallback: any;
111-
(<any>window).external = window.external || {};
112-
browserMsg.listen(ExternalMessage, (msg) => {
113-
switch (msg.action) {
114-
case "isInstalled":
115-
isInstalledCallback(msg.data);
110+
let isInstalledCallback: (data: any) => void;
111+
(<{ external: any }><unknown>window).external = window.external || {};
112+
browserMsg.listen(ExternalMessage, (msg: any) => {
113+
const m = <{ action: string, data: any }>msg;
114+
switch (m.action) {
115+
case 'isInstalled':
116+
isInstalledCallback(m.data);
116117
break;
117118
}
118119
});
119-
(<any>window.external).Scriptcat = {
120+
((<{ external: { Scriptcat: { isInstalled: (name: string, namespace: string, callback: any) => void } } }><unknown>window).external).Scriptcat = {
120121
isInstalled(name: string, namespace: string, callback: any) {
121122
isInstalledCallback = callback;
122123
browserMsg.send(ExternalMessage, { 'action': 'isInstalled', 'params': { name, namespace } });
123124
}
124125
};
125-
(<any>window.external).Tampermonkey = (<any>window.external).Scriptcat;
126+
((<{ external: { Tampermonkey: any } }><unknown>window).external).Tampermonkey = ((<{ external: { Scriptcat: any } }><unknown>window).external).Scriptcat;
126127
break;
127128
}
128129

src/model/do/script.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,9 @@ export type UserConfig = { [key: string]: { [key: string]: Config } };
4141
export interface ScriptCache extends Script {
4242
grantMap: { [key: string]: string }
4343
value: { [key: string]: Value }
44-
flag?: string
44+
flag: string
4545
resource: { [key: string]: Resource }
46-
context?: ScriptContext
46+
context: ScriptContext
4747
}
4848

4949
export interface Script {

src/types/main.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ interface ICodeChange {
6060
scriptId: number;
6161
}
6262

63-
declare const ScriptFlag;
63+
declare const ScriptFlag: string;
6464

6565
declare namespace chrome {
6666
declare namespace clipboard {
@@ -109,7 +109,7 @@ declare namespace GMSend {
109109
}
110110

111111
interface XHRFormData {
112-
type?: 'file'
112+
type?: 'file' | 'text'
113113
key: string
114114
val: string
115115
filename?: string

0 commit comments

Comments
 (0)