mirror of
https://github.com/tidys/cc-inspector-chrome
synced 2025-10-22 20:35:24 +00:00
逻辑优化
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
import { ChromeConst } from "cc-plugin/src/chrome/const";
|
||||
import { Msg, Page, PluginEvent, RequestSupportData } from "../../core/types";
|
||||
import { debugLog, Msg, Page, PluginEvent, RequestSupportData } from "../../core/types";
|
||||
import { Terminal } from "../terminal";
|
||||
import { PortMan } from "./portMan";
|
||||
import { portMgr } from "./portMgr";
|
||||
const terminal = new Terminal(Page.Background);
|
||||
console.log(...terminal.init());
|
||||
debugLog && console.log(...terminal.init());
|
||||
function isDevtools(port: chrome.runtime.Port) {
|
||||
const devtoolsUrl = `chrome-extension://${port.sender?.id}/${ChromeConst.html.devtools}`;
|
||||
if (port.sender?.url === devtoolsUrl) {
|
||||
|
@@ -1,4 +1,4 @@
|
||||
import { Page, PluginEvent } from "../../core/types";
|
||||
import { debugLog, Page, PluginEvent } from "../../core/types";
|
||||
import { Terminal } from "../terminal";
|
||||
|
||||
export abstract class PortMan {
|
||||
@@ -29,15 +29,15 @@ export abstract class PortMan {
|
||||
this.terminal = new Terminal(`Port-${this.name}`);
|
||||
port.onMessage.addListener((data: any, port: chrome.runtime.Port) => {
|
||||
const event = PluginEvent.create(data);
|
||||
console.log(...this.terminal.chunkMessage(event.toChunk()));
|
||||
debugLog && console.log(...this.terminal.chunkMessage(event.toChunk()));
|
||||
if (event.valid && this.onMessage) {
|
||||
this.onMessage(event);
|
||||
} else {
|
||||
console.log(...this.terminal.log(JSON.stringify(data)));
|
||||
debugLog && console.log(...this.terminal.log(JSON.stringify(data)));
|
||||
}
|
||||
});
|
||||
port.onDisconnect.addListener((port: chrome.runtime.Port) => {
|
||||
console.log(...this.terminal.disconnect(""));
|
||||
debugLog && console.log(...this.terminal.disconnect(""));
|
||||
if (this.onDisconnect) {
|
||||
this.onDisconnect(port);
|
||||
}
|
||||
|
@@ -1,4 +1,4 @@
|
||||
import { Msg, Page, PluginEvent, RequestSupportData, ResponseUpdateFramesData } from "../../core/types";
|
||||
import { debugLog, Msg, Page, PluginEvent, RequestSupportData, ResponseUpdateFramesData } from "../../core/types";
|
||||
import { FrameDetails } from "../../views/devtools/data";
|
||||
import { Terminal } from "../terminal";
|
||||
import { PortContent } from "./portContent";
|
||||
@@ -76,9 +76,9 @@ export class PortMgr {
|
||||
|
||||
if (arr.length) {
|
||||
// console.table(arr)
|
||||
console.log(...this.terminal.log(str.join("\n"), true));
|
||||
debugLog && console.log(...this.terminal.log(str.join("\n"), true));
|
||||
} else {
|
||||
console.log(...this.terminal.log("no port connected"));
|
||||
debugLog && console.log(...this.terminal.log("no port connected"));
|
||||
}
|
||||
}
|
||||
public removePort(item: PortMan) {
|
||||
|
@@ -1,12 +1,12 @@
|
||||
// content.js 和原始界面共享DOM,具有操作dom的能力
|
||||
// 但是不共享js,要想访问页面js,只能通过注入的方式
|
||||
import { ChromeConst } from "cc-plugin/src/chrome/const";
|
||||
import { Msg, Page, PluginEvent, ResponseSupportData } from "../../core/types";
|
||||
import { debugLog, Msg, Page, PluginEvent, ResponseSupportData } from "../../core/types";
|
||||
import { DocumentEvent } from "../const";
|
||||
import { Terminal } from "../terminal";
|
||||
|
||||
const terminal = new Terminal(Page.Content);
|
||||
console.log(...terminal.init());
|
||||
debugLog && console.log(...terminal.init());
|
||||
|
||||
// #region 注入脚本
|
||||
export function injectScript(url: string) {
|
||||
@@ -19,9 +19,9 @@ export function injectScript(url: string) {
|
||||
document.head.removeChild(script);
|
||||
};
|
||||
document.head.appendChild(script);
|
||||
console.log(...terminal.green(`inject script success: ${content}`));
|
||||
debugLog && console.log(...terminal.green(`inject script success: ${content}`));
|
||||
} else {
|
||||
console.log(...terminal.red("inject script failed"));
|
||||
debugLog && console.log(...terminal.red("inject script failed"));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,13 +29,13 @@ export function injectScript(url: string) {
|
||||
document.addEventListener(DocumentEvent.Inject2Content, (event: CustomEvent) => {
|
||||
let data: PluginEvent = PluginEvent.create(event.detail);
|
||||
if (data.valid && data.check(Page.Inject, Page.Content)) {
|
||||
console.log(...terminal.chunkMessage(data.toChunk()));
|
||||
debugLog && console.log(...terminal.chunkMessage(data.toChunk()));
|
||||
data.reset(Page.Content, Page.Devtools);
|
||||
if (connect) {
|
||||
// 接受来自inject.js的消息数据,然后中转到background.js
|
||||
connect.postMessage(data);
|
||||
} else {
|
||||
console.log(...terminal.log(`connect is null`));
|
||||
debugLog && console.log(...terminal.log(`connect is null`));
|
||||
throw new Error("connect is null");
|
||||
}
|
||||
} else {
|
||||
@@ -45,16 +45,16 @@ document.addEventListener(DocumentEvent.Inject2Content, (event: CustomEvent) =>
|
||||
// #region 和background通讯
|
||||
let connect: chrome.runtime.Port = chrome.runtime.connect({ name: Page.Content });
|
||||
connect.onDisconnect.addListener(() => {
|
||||
console.log(...terminal.disconnect(""));
|
||||
debugLog && console.log(...terminal.disconnect(""));
|
||||
connect = null;
|
||||
});
|
||||
connect.onMessage.addListener((data: PluginEvent, sender: chrome.runtime.Port) => {
|
||||
const event = PluginEvent.create(data);
|
||||
if (event.valid && event.check(Page.Background, Page.Content)) {
|
||||
console.log(...terminal.chunkMessage(event.toChunk()));
|
||||
debugLog && console.log(...terminal.chunkMessage(event.toChunk()));
|
||||
event.reset(Page.Content, Page.Inject);
|
||||
const e = new CustomEvent(DocumentEvent.Content2Inject, { detail: event });
|
||||
console.log(...terminal.chunkSend(event.toChunk()));
|
||||
debugLog && console.log(...terminal.chunkSend(event.toChunk()));
|
||||
document.dispatchEvent(e);
|
||||
} else {
|
||||
throw new Error(`invalid data: ${data}`);
|
||||
@@ -70,7 +70,7 @@ function checkGame() {
|
||||
if (connect) {
|
||||
connect.postMessage(sendData);
|
||||
} else {
|
||||
console.log(...terminal.log(`connect is null`));
|
||||
debugLog && console.log(...terminal.log(`connect is null`));
|
||||
throw new Error("connect is null");
|
||||
}
|
||||
}
|
||||
|
@@ -1,4 +1,4 @@
|
||||
import { Msg, Page, PluginEvent } from "../../core/types";
|
||||
import { debugLog, Msg, Page, PluginEvent } from "../../core/types";
|
||||
import { DocumentEvent } from "../const";
|
||||
import { Terminal } from "../terminal";
|
||||
|
||||
@@ -7,14 +7,14 @@ export class InjectEvent {
|
||||
constructor() {
|
||||
document.addEventListener(DocumentEvent.Content2Inject, (event: CustomEvent) => {
|
||||
const pluginEvent: PluginEvent = PluginEvent.create(event.detail);
|
||||
console.log(...this.terminal.chunkMessage(pluginEvent.toChunk()));
|
||||
debugLog && console.log(...this.terminal.chunkMessage(pluginEvent.toChunk()));
|
||||
this.onMessage(pluginEvent);
|
||||
});
|
||||
}
|
||||
onMessage(data: PluginEvent) {}
|
||||
sendMsgToContent(msg: Msg, data: any) {
|
||||
const detail = new PluginEvent(Page.Inject, Page.Content, msg, data);
|
||||
console.log(...this.terminal.chunkSend(detail.toChunk()));
|
||||
debugLog && console.log(...this.terminal.chunkSend(detail.toChunk()));
|
||||
const event = new CustomEvent(DocumentEvent.Inject2Content, { detail });
|
||||
document.dispatchEvent(event);
|
||||
}
|
||||
|
@@ -1,6 +1,6 @@
|
||||
// eval 注入脚本的代码,变量尽量使用var,后来发现在import之后,let会自动变为var
|
||||
import { uniq } from "lodash";
|
||||
import { Msg, PluginEvent, RequestLogData, RequestNodeInfoData, RequestSetPropertyData, ResponseNodeInfoData, ResponseSetPropertyData, ResponseSupportData, ResponseTreeInfoData } from "../../core/types";
|
||||
import { debugLog, Msg, PluginEvent, RequestLogData, RequestNodeInfoData, RequestSetPropertyData, ResponseNodeInfoData, ResponseSetPropertyData, ResponseSupportData, ResponseTreeInfoData } from "../../core/types";
|
||||
import { ArrayData, BoolData, ColorData, DataType, EngineData, Group, ImageData, Info, InvalidData, NodeInfoData, NumberData, ObjectData, Property, StringData, TreeData, Vec2Data, Vec3Data } from "../../views/devtools/data";
|
||||
import { InjectEvent } from "./event";
|
||||
import { getValue, trySetValueWithConfig } from "./setValue";
|
||||
@@ -62,10 +62,26 @@ export class Inspector extends InjectEvent {
|
||||
logFunction(value);
|
||||
break;
|
||||
}
|
||||
case Msg.RequestVisible: {
|
||||
const uuid: string = pluginEvent.data;
|
||||
const node = this.inspectorGameMemoryStorage[uuid];
|
||||
if (node) {
|
||||
node.active = !node.active;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case Msg.RequestDestroy: {
|
||||
const uuid: string = pluginEvent.data;
|
||||
const node = this.inspectorGameMemoryStorage[uuid];
|
||||
if (node && node.isValid && node.destroy) {
|
||||
node.destroy();
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
init() {
|
||||
console.log(...this.terminal.init());
|
||||
debugLog && console.log(...this.terminal.init());
|
||||
this.watchIsCocosGame();
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user