Skip to content

Commit

Permalink
🎨 修改eslint代码
Browse files Browse the repository at this point in the history
  • Loading branch information
CodFrm committed Jan 1, 2022
1 parent 9eb92cc commit d9b9af0
Show file tree
Hide file tree
Showing 8 changed files with 305 additions and 333 deletions.
2 changes: 2 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,8 @@ module.exports = {

"@typescript-eslint/no-explicit-any": ["off"],
"@typescript-eslint/no-unsafe-assignment": ["off"],
"@typescript-eslint/no-implied-eval": ["off"],

// '@typescript-eslint/no-unsafe-return': ['off'],
// '@typescript-eslint/no-unsafe-call': ['off'],
// '@typescript-eslint/no-empty-function': ['off'],
Expand Down
251 changes: 129 additions & 122 deletions src/apps/grant/frontend.ts

Large diffs are not rendered by default.

27 changes: 16 additions & 11 deletions src/apps/msg-center/browser.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@

// 前端用通信

import { randomString } from "@App/pkg/utils/utils";

export type ListenMsg = (msg: any) => void;

// 浏览器页面之间的通信,主要在content和injected页面之间
export class BrowserMsg {

export interface BrowserMsg {
send(topic: string, msg: any): void;

listen(topic: string, callback: ListenMsg): void;
}

export class FrontendMsg implements BrowserMsg {

public id: string;

Expand All @@ -17,29 +22,29 @@ export class BrowserMsg {
constructor(id: string, content: boolean) {
this.id = id;
this.content = content;
document.addEventListener(this.id + (content ? 'ct' : 'fd'), (event: any) => {
let detail = event.detail;
let topic = detail.topic;
let listen = this.listenMap.get(topic);
document.addEventListener(this.id + (content ? 'ct' : 'fd'), (event: unknown) => {
const detail = (<{ detail: { msg: any, topic: string } }>event).detail;
const topic = detail.topic;
const listen = this.listenMap.get(topic);
if (listen) {
listen(detail.msg);
}
});
}

public send(topic: string, msg: any) {
let detail = Object.assign({}, {
let detail = <{ topic: string, msg: any }>Object.assign({}, {
topic: topic,
msg: msg,
});
if ((<any>global).cloneInto) {
if ((<{ cloneInto?: (detail: any, view: any) => { topic: string, msg: any } }><unknown>global).cloneInto) {
try {
detail = (<any>global).cloneInto(detail, document.defaultView);
detail = (<{ cloneInto: (detail: any, view: any) => { topic: string, msg: any } }><unknown>global).cloneInto(detail, document.defaultView);
} catch (e) {
console.log(e);
}
}
let ev = new CustomEvent(this.id + (this.content ? 'fd' : 'ct'), {
const ev = new CustomEvent(this.id + (this.content ? 'fd' : 'ct'), {
detail: detail,
});
document.dispatchEvent(ev);
Expand Down
59 changes: 32 additions & 27 deletions src/content.ts
Original file line number Diff line number Diff line change
@@ -1,39 +1,41 @@
import { ExternalWhitelist } from "./apps/config";
import { Grant } from "./apps/grant/interface";
import { BrowserMsg } from "./apps/msg-center/browser";
import { ExternalMessage, ScriptExec, ScriptGrant, ScriptValueChange } from "./apps/msg-center/event";
import { MsgCenter } from "./apps/msg-center/msg-center";
import { ScriptCache } from "./model/do/script";
import { Grant } from './apps/grant/interface';
import { FrontendMsg } from './apps/msg-center/browser';
import { ExternalMessage, ScriptExec, ScriptGrant, ScriptValueChange } from './apps/msg-center/event';
import { MsgCenter } from './apps/msg-center/msg-center';
import { ScriptCache } from './model/do/script';

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

browserMsg.send('scripts', scripts);
browserMsg.listen('grant', async msg => {
switch (msg.value) {
case 'CAT_fetchBlob':
let resp = await (await fetch(msg.params[0])).blob();
msg.data = (<any>global).cloneInto ? (<any>global).cloneInto(resp, document.defaultView) : resp;
browserMsg.send(msg.flag!, msg);
break;
default:
// NOTE: 好像没处理释放问题
MsgCenter.connect(ScriptGrant, msg).addListener((msg: Grant, port: chrome.runtime.Port) => {
browserMsg.send(msg.flag!, msg);
});
browserMsg.listen('grant', (msg: { value: string, params: any[], flag: string, data: any }) => {
const handler = async () => {
switch (msg.value) {
case 'CAT_fetchBlob':
const resp = await (await fetch(<RequestInfo>msg.params[0])).blob();
msg.data = (<{ cloneInto?: (detail: any, view: any) => any }><unknown>global).cloneInto ?
(<{ cloneInto: (detail: any, view: any) => any }><unknown>global).cloneInto(resp, document.defaultView) : resp;
browserMsg.send(msg.flag, msg);
break;
default:
// NOTE: 好像没处理释放问题
MsgCenter.connect(ScriptGrant, msg).addListener((msg: Grant) => {
browserMsg.send(msg.flag || '', msg);
});
}
}
void handler();
});
MsgCenter.connect(ScriptValueChange, 'init').addListener((msg: any) => {
browserMsg.send(ScriptValueChange, msg);
});
browserMsg.listen(ExternalMessage, msg => {
MsgCenter.connect(ExternalMessage, msg).addListener((msg, port) => {
MsgCenter.connect(ExternalMessage, msg).addListener((msg) => {
browserMsg.send(ExternalMessage, msg);
});
});
chrome.runtime.onMessage.addListener((event) => {
chrome.runtime.onMessage.addListener((event: { action: string, uuid: string }) => {
switch (event.action) {
case ScriptExec:
browserMsg.send(ScriptExec, event.uuid);
Expand All @@ -42,9 +44,12 @@ chrome.runtime.sendMessage("runScript", (event: any) => {
});

// 处理blob
browserMsg.listen("fetchBlob", async msg => {
let ret = await fetch(msg.url);
browserMsg.send("fetchBlob", { url: msg.url, id: msg.id, ret });
browserMsg.listen('fetchBlob', (msg: { url: string, id: string }) => {
const handler = async () => {
const ret = await fetch(msg.url);
browserMsg.send('fetchBlob', { url: msg.url, id: msg.id, ret });
}
void handler();
})
});

Expand Down
67 changes: 34 additions & 33 deletions src/injected.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
// splitChunks对injected可能会有问题

import { ExternalWhitelist } from "./apps/config";
import { FrontendGrant, ScriptContext } from "./apps/grant/frontend";
import { BrowserMsg } from "./apps/msg-center/browser";
import { ExternalMessage, ScriptExec, ScriptValueChange } from "./apps/msg-center/event";
import { ScriptCache } from "./model/do/script";
import { Value } from "./model/do/value";
import { addStyle } from "./pkg/frontend";
import { createContext } from "./pkg/sandbox/compile";
import { buildThis } from "./pkg/sandbox/sandbox";
import { ExternalWhitelist } from './apps/config';
import { FrontendGrant, ScriptContext } from './apps/grant/frontend';
import { FrontendMsg } from './apps/msg-center/browser';
import { ExternalMessage, ScriptExec, ScriptValueChange } from './apps/msg-center/event';
import { ScriptCache } from './model/do/script';
import { Value } from './model/do/value';
import { addStyle } from './pkg/frontend';
import { createContext } from './pkg/sandbox/compile';
import { buildThis } from './pkg/sandbox/sandbox';

// 参考了tm的实现
function waitBody(callback: () => void) {
if (document.body) {
return callback();
}
let listen = function () {
const listen = function () {
document.removeEventListener('load', listen, false);
document.removeEventListener('DOMNodeInserted', listen, false);
document.removeEventListener('DOMContentLoaded', listen, false);
Expand All @@ -26,9 +26,9 @@ function waitBody(callback: () => void) {
document.addEventListener('DOMContentLoaded', listen, false);
};

let browserMsg = new BrowserMsg(ScriptFlag, false);
browserMsg.listen("scripts", (msg) => {
let scripts: ScriptCache[] = msg;
const browserMsg = new FrontendMsg(ScriptFlag, false);
browserMsg.listen('scripts', (msg) => {
const scripts: ScriptCache[] = msg;
browserMsg.listen(ScriptValueChange, (msg: Value) => {
scripts.forEach(val => {
if (!val.value) {
Expand All @@ -42,15 +42,15 @@ browserMsg.listen("scripts", (msg) => {
browserMsg.listen(ScriptExec, (msg) => {
for (let i = 0; i < scripts.length; i++) {
if (scripts[i].uuid == msg) {
(<any>window)[scripts[i].flag!].apply(scripts[i].context, [scripts[i].context]);
(<{ [key: string]: (context: ScriptContext) => void }><unknown>window)[scripts[i].flag].apply(scripts[i].context, [scripts[i].context]);
break;
}
}
});
scripts.forEach(script => {
// 构建沙盒
let context: ScriptContext;
if (script.grantMap!['none']) {
if (script.grantMap['none']) {
context = <any>window;
} else {
context = new FrontendGrant(script, browserMsg);
Expand All @@ -62,18 +62,18 @@ browserMsg.listen("scripts", (msg) => {
if (script.metadata['run-at'] && (script.metadata['run-at'][0] === 'document-menu' || script.metadata['run-at'][0] === 'document-body')) {
if (script.metadata['run-at'][0] === 'document-body') {
waitBody(() => {
if ((<any>window)[script.flag!]) {
(<any>window)[script.flag!].apply(context, [context]);
if ((<{ [key: string]: () => void }><unknown>window)[script.flag]) {
(<{ [key: string]: (context: ScriptContext) => void }><unknown>window)[script.flag].apply(context, [context]);
}
Object.defineProperty(window, script.flag!, {
Object.defineProperty(window, script.flag, {
get: () => { return undefined; },
set: (val) => {
set: (val: (context: ScriptContext) => void) => {
val.apply(context, [context]);
}
});
// 注入css
script.metadata['require-css']?.forEach(val => {
let res = script.resource![val];
const res = script.resource[val];
if (res) {
addStyle(res.content);
}
Expand All @@ -82,18 +82,18 @@ browserMsg.listen("scripts", (msg) => {
}
return;
}
if ((<any>window)[script.flag!]) {
(<any>window)[script.flag!].apply(context, [context]);
if ((<{ [key: string]: () => void }><unknown>window)[script.flag]) {
(<{ [key: string]: (context: ScriptContext) => void }><unknown>window)[script.flag].apply(context, [context]);
}
Object.defineProperty(window, script.flag!, {
Object.defineProperty(window, script.flag, {
get: () => { return undefined; },
set: (val) => {
set: (val: (context: ScriptContext) => void) => {
val.apply(context, [context]);
}
});
// 注入css
script.metadata['require-css']?.forEach(val => {
let res = script.resource![val];
const res = script.resource[val];
if (res) {
addStyle(res.content);
}
Expand All @@ -107,22 +107,23 @@ browserMsg.listen("scripts", (msg) => {
for (let i = 0; i < ExternalWhitelist.length; i++) {
if (window.location.host.endsWith(ExternalWhitelist[i])) {
// 注入
let isInstalledCallback: any;
(<any>window).external = window.external || {};
browserMsg.listen(ExternalMessage, (msg) => {
switch (msg.action) {
case "isInstalled":
isInstalledCallback(msg.data);
let isInstalledCallback: (data: any) => void;
(<{ external: any }><unknown>window).external = window.external || {};
browserMsg.listen(ExternalMessage, (msg: any) => {
const m = <{ action: string, data: any }>msg;
switch (m.action) {
case 'isInstalled':
isInstalledCallback(m.data);
break;
}
});
(<any>window.external).Scriptcat = {
((<{ external: { Scriptcat: { isInstalled: (name: string, namespace: string, callback: any) => void } } }><unknown>window).external).Scriptcat = {
isInstalled(name: string, namespace: string, callback: any) {
isInstalledCallback = callback;
browserMsg.send(ExternalMessage, { 'action': 'isInstalled', 'params': { name, namespace } });
}
};
(<any>window.external).Tampermonkey = (<any>window.external).Scriptcat;
((<{ external: { Tampermonkey: any } }><unknown>window).external).Tampermonkey = ((<{ external: { Scriptcat: any } }><unknown>window).external).Scriptcat;
break;
}

Expand Down
4 changes: 2 additions & 2 deletions src/model/do/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ export type UserConfig = { [key: string]: { [key: string]: Config } };
export interface ScriptCache extends Script {
grantMap: { [key: string]: string }
value: { [key: string]: Value }
flag?: string
flag: string
resource: { [key: string]: Resource }
context?: ScriptContext
context: ScriptContext
}

export interface Script {
Expand Down
4 changes: 2 additions & 2 deletions src/types/main.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ interface ICodeChange {
scriptId: number;
}

declare const ScriptFlag;
declare const ScriptFlag: string;

declare namespace chrome {
declare namespace clipboard {
Expand Down Expand Up @@ -109,7 +109,7 @@ declare namespace GMSend {
}

interface XHRFormData {
type?: 'file'
type?: 'file' | 'text'
key: string
val: string
filename?: string
Expand Down
Loading

0 comments on commit d9b9af0

Please sign in to comment.