mirror of
https://github.com/tidys/cc-inspector-chrome
synced 2025-04-20 08:58:41 +00:00
打通流程
This commit is contained in:
parent
c4fb53646f
commit
9ec28b510b
@ -1,4 +1,4 @@
|
|||||||
import {PluginEvent, Page, Msg} from "@/core/types";
|
import {Page, PluginEvent} from "@/core/types";
|
||||||
|
|
||||||
let Devtools: chrome.runtime.Port | null = null;
|
let Devtools: chrome.runtime.Port | null = null;
|
||||||
let Content: chrome.runtime.Port | null = null;
|
let Content: chrome.runtime.Port | null = null;
|
||||||
@ -11,8 +11,8 @@ chrome.runtime.onConnect.addListener((port: chrome.runtime.Port) => {
|
|||||||
onPortConnect(port,
|
onPortConnect(port,
|
||||||
(data: PluginEvent) => {
|
(data: PluginEvent) => {
|
||||||
// 从devtools过来的消息统一派发到Content中
|
// 从devtools过来的消息统一派发到Content中
|
||||||
if (data.target === Page.Background) {
|
if (PluginEvent.check(data, Page.Devtools, Page.Background)) {
|
||||||
data.target = Page.Content;
|
PluginEvent.reset(data, Page.Background, Page.Content);
|
||||||
Content && Content.postMessage(data)
|
Content && Content.postMessage(data)
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@ -54,8 +54,8 @@ function onPortConnect(port: chrome.runtime.Port, onMsg: Function, onDisconnect:
|
|||||||
// background.js 更像是一个主进程,负责整个插件的调度,生命周期和chrome保持一致
|
// background.js 更像是一个主进程,负责整个插件的调度,生命周期和chrome保持一致
|
||||||
// 监听来自content.js发来的事件,将消息转发到devtools
|
// 监听来自content.js发来的事件,将消息转发到devtools
|
||||||
chrome.runtime.onMessage.addListener((request: PluginEvent, sender: any, sendResponse: any) => {
|
chrome.runtime.onMessage.addListener((request: PluginEvent, sender: any, sendResponse: any) => {
|
||||||
if (request.target === Page.Background) {
|
if (PluginEvent.check(request, Page.Content, Page.Background)) {
|
||||||
request.target = Page.Devtools;
|
PluginEvent.reset(request, Page.Background, Page.Devtools)
|
||||||
console.log(`%c[Message]url:${sender.url}]\n${JSON.stringify(request)}`, 'color:green')
|
console.log(`%c[Message]url:${sender.url}]\n${JSON.stringify(request)}`, 'color:green')
|
||||||
sendResponse && sendResponse(request);
|
sendResponse && sendResponse(request);
|
||||||
Devtools && Devtools.postMessage(request);
|
Devtools && Devtools.postMessage(request);
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
// content.js 和原始界面共享DOM,具有操作dom的能力
|
// content.js 和原始界面共享DOM,具有操作dom的能力
|
||||||
// 但是不共享js,要想访问页面js,只能通过注入的方式
|
// 但是不共享js,要想访问页面js,只能通过注入的方式
|
||||||
import {injectScript} from "@/core/util";
|
import {injectScript} from "@/core/util";
|
||||||
import {PluginEvent, Page, Msg} from "@/core/types";
|
import {Msg, Page, PluginEvent} from "@/core/types";
|
||||||
|
|
||||||
injectScript("js/inject.js");
|
injectScript("js/inject.js");
|
||||||
|
|
||||||
@ -9,9 +9,9 @@ injectScript("js/inject.js");
|
|||||||
let conn = chrome.runtime.connect({name: Page.Content})
|
let conn = chrome.runtime.connect({name: Page.Content})
|
||||||
conn.onMessage.addListener((data: PluginEvent, sender) => {
|
conn.onMessage.addListener((data: PluginEvent, sender) => {
|
||||||
// 将background.js的消息返回到injection.js
|
// 将background.js的消息返回到injection.js
|
||||||
console.log(`%c[Connect-Message] ${JSON.stringify(data)}`, "color:green;")
|
if (PluginEvent.check(data, Page.Background, Page.Content)) {
|
||||||
if (data.target === Page.Content) {
|
console.log(`%c[Connect-Message] ${JSON.stringify(data)}`, "color:green;")
|
||||||
data.target = Page.Inject;
|
PluginEvent.reset(data, Page.Content, Page.Inject)
|
||||||
window.postMessage(data, "*");
|
window.postMessage(data, "*");
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@ -19,9 +19,9 @@ conn.onMessage.addListener((data: PluginEvent, sender) => {
|
|||||||
// 接受来自inject.js的消息数据,然后中转到background.js
|
// 接受来自inject.js的消息数据,然后中转到background.js
|
||||||
window.addEventListener('message', function (event) {
|
window.addEventListener('message', function (event) {
|
||||||
let data: PluginEvent = event.data;
|
let data: PluginEvent = event.data;
|
||||||
console.log(`%c[Window-Message] ${JSON.stringify(data)}`, "color:green;");
|
if (PluginEvent.check(data, Page.Inject, Page.Content)) {
|
||||||
if (data.target === Page.Inject) {
|
console.log(`%c[Window-Message] ${JSON.stringify(data)}`, "color:green;");
|
||||||
data.target = Page.Background;
|
PluginEvent.reset(data, Page.Content, Page.Background)
|
||||||
chrome.runtime.sendMessage(data);
|
chrome.runtime.sendMessage(data);
|
||||||
}
|
}
|
||||||
}, false);
|
}, false);
|
||||||
@ -29,7 +29,7 @@ window.addEventListener('message', function (event) {
|
|||||||
|
|
||||||
let gameCanvas = document.querySelector("#GameCanvas");
|
let gameCanvas = document.querySelector("#GameCanvas");
|
||||||
if (!gameCanvas) {
|
if (!gameCanvas) {
|
||||||
let sendData = new PluginEvent(Page.Background, Msg.Support, {
|
let sendData = new PluginEvent(Page.Content, Page.Background, Msg.Support, {
|
||||||
support: false,
|
support: false,
|
||||||
msg: "未发现GameCanvas,不支持调试游戏!"
|
msg: "未发现GameCanvas,不支持调试游戏!"
|
||||||
})
|
})
|
||||||
|
@ -20,10 +20,28 @@ export enum Msg {
|
|||||||
export class PluginEvent {
|
export class PluginEvent {
|
||||||
msg: Msg | null = null;
|
msg: Msg | null = null;
|
||||||
data: any = null;
|
data: any = null;
|
||||||
|
|
||||||
|
source: Page | null = null; // 事件发送的源头
|
||||||
target: Page | null = null; // 事件要发送的目标
|
target: Page | null = null; // 事件要发送的目标
|
||||||
|
|
||||||
constructor(target: Page, msg: Msg, data?: any) {
|
static check(event: PluginEvent, source: Page, target: Page) {
|
||||||
|
return event && source && target && event.source === source && event.target === target;
|
||||||
|
}
|
||||||
|
|
||||||
|
static reset(event: PluginEvent, source: Page | null, target: Page | null) {
|
||||||
|
if (event && source && target) {
|
||||||
|
event.source = source;
|
||||||
|
event.target = target;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static finish(event: PluginEvent) {
|
||||||
|
event.source = event.target = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
constructor(source: Page, target: Page, msg: Msg, data?: any) {
|
||||||
if (PageInclude(target)) {
|
if (PageInclude(target)) {
|
||||||
|
this.source = source;
|
||||||
this.target = target;
|
this.target = target;
|
||||||
this.msg = msg;
|
this.msg = msg;
|
||||||
this.data = data || null;
|
this.data = data || null;
|
||||||
|
@ -16,7 +16,6 @@ class ConnectBackground {
|
|||||||
onBackgroundMessage(cb: Function) {
|
onBackgroundMessage(cb: Function) {
|
||||||
if (this.connect) {
|
if (this.connect) {
|
||||||
this.connect.onMessage.addListener((event, sender) => {
|
this.connect.onMessage.addListener((event, sender) => {
|
||||||
console.log(`[Message] ${JSON.stringify(event)}`);
|
|
||||||
cb && cb(event, sender)
|
cb && cb(event, sender)
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -45,8 +45,8 @@
|
|||||||
import Vue from "vue";
|
import Vue from "vue";
|
||||||
import {Component} from "vue-property-decorator";
|
import {Component} from "vue-property-decorator";
|
||||||
import properties from "./propertys.vue";
|
import properties from "./propertys.vue";
|
||||||
import {DataSupport, NodeData} from "@/devtools/type";
|
import { NodeData} from "@/devtools/type";
|
||||||
import {PluginEvent, Page, Msg} from '@/core/types'
|
import {Msg, Page, PluginEvent} from '@/core/types'
|
||||||
import {connectBackground} from "@/devtools/connectBackground";
|
import {connectBackground} from "@/devtools/connectBackground";
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
@ -80,7 +80,6 @@ export default class Index extends Vue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
window.addEventListener("message", function (event) {
|
window.addEventListener("message", function (event) {
|
||||||
console.log("on vue:" + JSON.stringify(event.data));
|
|
||||||
console.log("on vue:" + JSON.stringify(event));
|
console.log("on vue:" + JSON.stringify(event));
|
||||||
}, false);
|
}, false);
|
||||||
}
|
}
|
||||||
@ -112,9 +111,9 @@ export default class Index extends Vue {
|
|||||||
this.memory = eventData;
|
this.memory = eventData;
|
||||||
}
|
}
|
||||||
|
|
||||||
_onMsgSupport(data: DataSupport) {
|
_onMsgSupport(data:boolean) {
|
||||||
this.isShowDebug = data.support;
|
this.isShowDebug = data;
|
||||||
if (data.support) {
|
if (data) {
|
||||||
// 如果节点树为空,就刷新一次
|
// 如果节点树为空,就刷新一次
|
||||||
if (this.treeData.length === 0) {
|
if (this.treeData.length === 0) {
|
||||||
// this.onBtnClickUpdateTree();
|
// this.onBtnClickUpdateTree();
|
||||||
@ -135,9 +134,10 @@ export default class Index extends Vue {
|
|||||||
if (!data) {
|
if (!data) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (data.target === Page.Background) {
|
if (PluginEvent.check(data, Page.Background, Page.Devtools)) {
|
||||||
|
console.log(`[Devtools] ${JSON.stringify(data)}`);
|
||||||
|
PluginEvent.finish(data);
|
||||||
let eventData: any = data.data;
|
let eventData: any = data.data;
|
||||||
console.log(data)
|
|
||||||
switch (data.msg) {
|
switch (data.msg) {
|
||||||
case Msg.UrlChange: {
|
case Msg.UrlChange: {
|
||||||
break;
|
break;
|
||||||
@ -147,7 +147,7 @@ export default class Index extends Vue {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Msg.Support: {
|
case Msg.Support: {
|
||||||
this._onMsgSupport(eventData as DataSupport)
|
this._onMsgSupport(!!eventData)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Msg.NodeInfo: {
|
case Msg.NodeInfo: {
|
||||||
@ -194,8 +194,7 @@ export default class Index extends Vue {
|
|||||||
console.log("环境异常,无法执行函数");
|
console.log("环境异常,无法执行函数");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
debugger
|
let sendData = new PluginEvent(Page.Devtools, Page.Background, msg, data)
|
||||||
let sendData = new PluginEvent(Page.Background, msg, data)
|
|
||||||
connectBackground.postMessageToBackground(sendData);
|
connectBackground.postMessageToBackground(sendData);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -32,21 +32,25 @@ class CCInspector {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
let pluginEvent: PluginEvent = event.data;
|
let pluginEvent: PluginEvent = event.data;
|
||||||
if (pluginEvent.target === Page.Inject) {
|
if (PluginEvent.check(pluginEvent, Page.Content, Page.Inject)) {
|
||||||
|
console.log(`[Inject] ${JSON.stringify(pluginEvent)}`, 'color:green;');
|
||||||
|
PluginEvent.finish(pluginEvent)
|
||||||
switch (pluginEvent.msg) {
|
switch (pluginEvent.msg) {
|
||||||
case Msg.UrlChange:
|
case Msg.UrlChange:
|
||||||
case Msg.Support: {
|
case Msg.Support: {
|
||||||
let isCocosGame = this._isCocosGame();
|
let isCocosGame = this._isCocosGame();
|
||||||
this.sendMsgToContent(Msg.Support, {support: isCocosGame});
|
this.notifySupportGame(isCocosGame)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Msg.TreeInfo: {
|
case Msg.TreeInfo: {
|
||||||
|
|
||||||
debugger
|
debugger
|
||||||
|
this.updateTreeInfo();
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Msg.NodeInfo: {
|
case Msg.NodeInfo: {
|
||||||
debugger
|
debugger
|
||||||
|
let nodeUUID = pluginEvent.data;
|
||||||
|
this.getNodeInfo(nodeUUID);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Msg.SetProperty: {
|
case Msg.SetProperty: {
|
||||||
@ -60,31 +64,11 @@ class CCInspector {
|
|||||||
|
|
||||||
sendMsgToContent(msg: Msg, data: any) {
|
sendMsgToContent(msg: Msg, data: any) {
|
||||||
// 发送给content.js处理,也会导致发送给了自身,死循环
|
// 发送给content.js处理,也会导致发送给了自身,死循环
|
||||||
window.postMessage(new PluginEvent(Page.Inject, msg, data), "*");
|
window.postMessage(new PluginEvent(Page.Inject, Page.Content, msg, data), "*");
|
||||||
}
|
}
|
||||||
|
|
||||||
devPageCallEntry(str: string) {
|
notifySupportGame(b: boolean) {
|
||||||
let para: PluginEvent = JSON.parse(str);
|
this.sendMsgToContent(Msg.Support, b);
|
||||||
debugger
|
|
||||||
if (this._isCocosGame()) {
|
|
||||||
switch (para.msg) {
|
|
||||||
case Msg.TreeInfo:
|
|
||||||
this.updateTreeInfo();
|
|
||||||
break;
|
|
||||||
case Msg.Support:
|
|
||||||
|
|
||||||
break;
|
|
||||||
case Msg.MemoryInfo:
|
|
||||||
break;
|
|
||||||
case Msg.SetProperty:
|
|
||||||
break;
|
|
||||||
case Msg.NodeInfo:
|
|
||||||
this.getNodeInfo(para.data as string);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
this.sendMsgToContent(Msg.Support, {support: false});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
updateTreeInfo() {
|
updateTreeInfo() {
|
||||||
@ -108,6 +92,8 @@ class CCInspector {
|
|||||||
} else {
|
} else {
|
||||||
this.sendMsgToContent(Msg.Support, {support: false, msg: "未发现游戏场景,不支持调试游戏!"});
|
this.sendMsgToContent(Msg.Support, {support: false, msg: "未发现游戏场景,不支持调试游戏!"});
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
this.notifySupportGame(false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -4,10 +4,7 @@ export class NodeData {
|
|||||||
children: Array<NodeData> = []
|
children: Array<NodeData> = []
|
||||||
}
|
}
|
||||||
|
|
||||||
export class DataSupport {
|
|
||||||
support: boolean = false;
|
|
||||||
msg?: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user