632 lines
16 KiB
Vue
Raw Normal View History

2019-03-15 10:08:39 +08:00
<template>
2021-04-03 19:31:47 +08:00
<div id="devtools">
2021-11-20 16:10:52 +08:00
<el-drawer
title="settings"
direction="btt"
@close="onCloseSettings"
:visible.sync="showSettings">
<div>
<settings-vue></settings-vue>
</div>
</el-drawer>
2021-11-18 22:41:07 +08:00
<div class="head" v-show="iframes.length>1">
2021-11-17 23:28:40 +08:00
<div class="label">inspect target:</div>
2021-11-20 21:57:54 +08:00
<el-select v-model="frameID" placeholder="please select ..." @change="onChangeFrame" style="flex:1;">
2021-11-17 23:28:40 +08:00
<el-option v-for="item in iframes" :key="item.value" :label="item.label" :value="item.value">
</el-option>
</el-select>
</div>
2021-04-03 19:31:47 +08:00
<div v-show="isShowDebug" class="find">
<div v-if="false">
<el-button type="success" @click="onMemoryTest">内存测试</el-button>
2019-03-18 18:03:07 +08:00
</div>
2021-04-03 19:31:47 +08:00
<div v-if="false">
<span>JS堆栈限制: {{ memory.performance.jsHeapSizeLimit }}</span>
<span>JS堆栈大小: {{ memory.performance.totalJSHeapSize }}</span>
<span>JS堆栈使用: {{ memory.performance.usedJSHeapSize }}</span>
2019-03-18 12:05:07 +08:00
</div>
2021-12-05 17:40:48 +08:00
<div ref="left" class="left">
2021-04-03 19:31:47 +08:00
<div class="tool-btn">
2021-11-20 16:10:52 +08:00
<div style="padding-left: 15px;flex:1;">Node Tree</div>
<el-button v-show="isShowRefreshBtn" type="success" class="el-icon-refresh"
size="mini"
@click="onBtnClickUpdateTree"></el-button>
2021-11-20 21:57:54 +08:00
<el-button @click="onClickSettings" class="el-icon-s-tools"></el-button>
2021-04-03 19:31:47 +08:00
</div>
2021-11-20 21:57:54 +08:00
<el-input placeholder="enter keywords to filter" v-model="filterText">
2021-11-12 22:30:08 +08:00
<template slot="append">
<div class="matchCase ">
<div class="iconfont el-icon-third-font-size" @click.stop="onChangeCase"
2021-11-20 21:57:54 +08:00
title="match case"
2021-11-12 22:30:08 +08:00
:style="{'color':matchCase?'red':''}">
2021-11-12 22:21:49 +08:00
</div>
2021-11-12 22:30:08 +08:00
</div>
</template>
</el-input>
<div class="treeList">
2021-04-03 19:31:47 +08:00
<el-tree :data="treeData"
ref="tree"
2021-04-03 19:31:47 +08:00
style="display: inline-block;"
:props="defaultProps"
:highlight-current="true"
:default-expand-all="false"
:default-expanded-keys="expandedKeys"
2021-11-12 22:21:49 +08:00
:filter-node-method="filterNode"
:expand-on-click-node="false"
node-key="uuid"
@node-expand="onNodeExpand"
@node-collapse="onNodeCollapse"
2021-05-11 22:15:29 +08:00
@node-click="handleNodeClick">
2021-11-04 21:01:33 +08:00
<!-- :render-content="renderContent"-->
2021-05-11 22:15:29 +08:00
<span slot-scope="{node,data}" class="leaf" :class="data.active?'leaf-show':'leaf-hide'">
<span>{{ node.label }}</span>
<!-- <el-button v-if="!!data||true"> 显示</el-button>-->
</span>
</el-tree>
2021-04-03 19:31:47 +08:00
</div>
</div>
2021-12-05 17:40:48 +08:00
<ui-divider ref="divider" @move="onDividerMove"></ui-divider>
<div ref="right" class="right">
2021-11-20 21:57:54 +08:00
<properties v-if="treeItemData" :data="treeItemData"></properties>
2021-04-03 19:31:47 +08:00
</div>
2019-03-15 10:08:39 +08:00
</div>
2021-04-03 19:31:47 +08:00
<div v-show="!isShowDebug" class="no-find">
2021-11-20 21:57:54 +08:00
<span>No games created by cocos creator found!</span>
2021-04-03 19:31:47 +08:00
<el-button type="success" class="el-icon-refresh" @click="onBtnClickUpdatePage">刷新</el-button>
2019-03-15 10:08:39 +08:00
</div>
</div>
</template>
2021-04-03 19:31:47 +08:00
<script lang="ts">
import Vue from "vue";
2021-11-12 22:21:49 +08:00
import {Component, Watch} from "vue-property-decorator";
2021-10-30 16:40:51 +08:00
import properties from "./propertys.vue";
2021-11-04 21:01:33 +08:00
import {Msg, Page, PluginEvent} from "@/core/types"
2021-04-26 22:27:47 +08:00
import {connectBackground} from "@/devtools/connectBackground";
2021-11-20 21:57:54 +08:00
import {
EngineData,
FrameDetails,
Info,
NodeInfoData,
ObjectData,
ObjectItemRequestData,
TreeData
} from "@/devtools/data";
2021-11-04 21:01:33 +08:00
import Bus, {BusMsg} from "@/devtools/bus";
2021-11-20 16:10:52 +08:00
import settingsVue from "./settings.vue"
import {RefreshAuto, RefreshManual, settings} from "@/devtools/settings";
2021-12-05 17:40:48 +08:00
import UiDivider from "@/devtools/ui/ui-divider.vue";
2021-04-03 19:31:47 +08:00
@Component({
components: {
2021-12-05 17:40:48 +08:00
UiDivider,
2021-04-20 11:15:30 +08:00
properties,
2021-11-20 16:10:52 +08:00
settingsVue,
}
})
2021-04-03 19:31:47 +08:00
export default class Index extends Vue {
2021-04-07 17:26:16 +08:00
private isShowDebug: boolean = false;
2021-11-20 21:57:54 +08:00
treeItemData: NodeInfoData | null = null;
2021-05-08 17:52:29 +08:00
treeData: Array<TreeData> = []
expandedKeys: Array<string> = [];
selectedUUID: string | null = null;
2021-04-03 19:31:47 +08:00
2021-11-12 22:21:49 +08:00
filterText: string | null = null;
2021-11-18 22:41:07 +08:00
iframes: Array<{ label: string, value: number }> = []
frameID: number | null = null;
2021-11-17 23:28:40 +08:00
2021-11-12 22:21:49 +08:00
@Watch("filterText")
updateFilterText(val: any) {
(this.$refs?.tree as any)?.filter(val);
}
private matchCase = false;
onChangeCase() {
this.matchCase = !this.matchCase;
this.updateFilterText(this.filterText);
}
2021-11-20 16:10:52 +08:00
private showSettings = false;
onClickSettings() {
this.showSettings = true;
}
2021-12-05 17:40:48 +08:00
onDividerMove(event: MouseEvent) {
const leftDiv: HTMLDivElement = this.$refs.left as HTMLDivElement;
if (leftDiv) {
let width = leftDiv.clientWidth;
width += event.movementX;
if (width >= 300 && width < document.body.clientWidth - 100) {
leftDiv.style.width = `${width}px`;
}
}
}
2021-11-21 21:36:16 +08:00
private syncSettings() {
2021-11-20 16:10:52 +08:00
if (settings.data) {
const {refreshType, refreshTime} = settings.data;
switch (refreshType) {
case RefreshAuto: {
this.isShowRefreshBtn = false;
this.onEnableTreeWatch(true, refreshTime)
break;
}
case RefreshManual: {
this.isShowRefreshBtn = true;
this.onEnableTreeWatch(false)
}
}
}
}
2021-11-21 21:36:16 +08:00
private isShowRefreshBtn = false;
onCloseSettings() {
this.syncSettings();
}
// el-tree的渲染key
2021-04-03 19:31:47 +08:00
defaultProps = {
children: "children",
label: "name"
};
memory = {
performance: {},
console: {},
}
2021-11-20 16:10:52 +08:00
timerID: number | null = null;
2019-03-18 12:05:07 +08:00
2021-11-06 20:19:21 +08:00
private requestList: Array<{ id: string, cb: Function }> = [];
2021-04-03 19:31:47 +08:00
created() {
if (chrome && chrome.runtime) {
this._initChromeRuntimeConnect();
}
2019-03-15 10:08:39 +08:00
2021-04-03 19:31:47 +08:00
window.addEventListener("message", function (event) {
console.log("on vue:" + JSON.stringify(event));
}, false);
2021-06-19 19:51:05 +08:00
Bus.$on(BusMsg.ShowPlace, (data: EngineData) => {
console.log(data)
this._expand(data.engineUUID);
})
2021-11-06 20:19:21 +08:00
Bus.$on(BusMsg.RequestObjectData, (data: ObjectData, cb: Function) => {
2021-11-07 00:09:02 +08:00
if (!data.id || this.requestList.find(el => el.id === data.id)) {
2021-11-06 20:19:21 +08:00
return
}
this.requestList.push({id: data.id, cb});
this.sendMsgToContentScript(Msg.GetObjectItemData, data)
});
2021-12-05 18:21:00 +08:00
Bus.$on(BusMsg.LogData, (data: string[]) => {
this.sendMsgToContentScript(Msg.LogData, data);
})
2021-06-19 19:51:05 +08:00
}
2021-11-12 22:21:49 +08:00
filterNode(value: any, data: any) {
if (!value) {
return true;
} else {
if (this.matchCase) {
// 严格匹配大写
return data?.name?.indexOf(value) !== -1;
} else {
return data?.name?.toLowerCase().indexOf(value.toLowerCase()) !== -1;
}
}
}
2021-11-17 23:28:40 +08:00
onChangeFrame() {
2021-11-18 22:41:07 +08:00
this.sendMsgToContentScript(Msg.UseFrame, this.frameID)
2021-11-17 23:28:40 +08:00
}
2021-06-19 19:51:05 +08:00
_expand(uuid: string) {
let expandKeys: Array<string> = [];
function circle(array: any) {
for (let i = 0; i < array.length; i++) {
let item = array[i];
expandKeys.push(item.uuid);
if (item.uuid === uuid) {
return true
} else {
let find = circle(item.children);
if (find) {
return true;
} else {
expandKeys.pop();
}
}
}
}
circle(this.treeData)
expandKeys.forEach(key => {
if (!this.expandedKeys.find(el => el === key)) {
this.expandedKeys.push(key)
}
})
// 高亮uuid
}
renderContent(h: Function, options: any) {
let {node, data, store} = options;
2021-11-04 21:01:33 +08:00
return h("span", {class: ""}, data.name)
2021-06-19 19:51:05 +08:00
// return(<span>1111</span>)
2021-04-03 19:31:47 +08:00
}
2019-03-18 12:05:07 +08:00
2021-11-18 22:41:07 +08:00
_onMsgTreeInfo(treeData: Array<TreeData>) {
2021-04-22 19:09:35 +08:00
this.isShowDebug = true;
2021-11-18 22:41:07 +08:00
if (!Array.isArray(treeData)) {
treeData = [treeData]
2021-04-22 19:09:35 +08:00
}
2021-11-18 22:41:07 +08:00
this.treeData = treeData;
2021-11-21 18:02:23 +08:00
if (this._checkSelectedUUID()) {
2021-11-21 21:36:16 +08:00
this.updateNodeInfo();
2021-04-22 19:09:35 +08:00
this.$nextTick(() => {
//@ts-ignore
this.$refs.tree.setCurrentKey(this.selectedUUID);
})
}
}
2021-11-21 18:02:23 +08:00
_checkSelectedUUID() {
if (this.selectedUUID) {
const b = this._findUuidInTree(this.treeData, this.selectedUUID)
if (b) {
return true;
}
}
this.selectedUUID = null;
this.treeItemData = null;
return false;
}
_findUuidInTree(data: TreeData[], targetUUID: string) {
function circle(tree: TreeData[]) {
for (let i = 0; i < tree.length; i++) {
let item: TreeData = tree[i];
if (item.uuid === targetUUID) {
return true;
}
if (circle(item.children)) {
return true;
}
}
return false;
}
return circle(data)
}
2021-11-20 21:57:54 +08:00
_onMsgNodeInfo(eventData: NodeInfoData) {
2021-04-22 19:09:35 +08:00
this.isShowDebug = true;
this.treeItemData = eventData;
}
2021-11-18 22:41:07 +08:00
_onMsgMemoryInfo(eventData: any) {
2021-04-22 19:09:35 +08:00
this.memory = eventData;
}
2021-11-21 21:36:16 +08:00
_onMsgSupport(isCocosGame: boolean) {
this.isShowDebug = isCocosGame;
if (isCocosGame) {
this.syncSettings();
this.onBtnClickUpdateTree();
2021-04-22 19:09:35 +08:00
} else {
2021-11-21 21:36:16 +08:00
this._clearTimer();
this.treeData = [];
this.treeItemData = null;
this.selectedUUID = null;
2021-04-22 19:09:35 +08:00
}
}
2021-11-20 16:10:52 +08:00
mounted() {
2021-11-21 21:36:16 +08:00
this.syncSettings();
2021-11-20 16:10:52 +08:00
}
2021-04-03 19:31:47 +08:00
_initChromeRuntimeConnect() {
2021-11-18 22:41:07 +08:00
const msgFunctionMap: Record<string, Function> = {};
msgFunctionMap[Msg.TreeInfo] = this._onMsgTreeInfo;
msgFunctionMap[Msg.Support] = this._onMsgSupport;
msgFunctionMap[Msg.NodeInfo] = this._onMsgNodeInfo;
msgFunctionMap[Msg.MemoryInfo] = this._onMsgMemoryInfo;
msgFunctionMap[Msg.UpdateProperty] = this._onMsgUpdateProperty;
msgFunctionMap[Msg.UpdateFrames] = this._onMsgUpdateFrames;
msgFunctionMap[Msg.GetObjectItemData] = this._onMsgGetObjectItemData;
2021-04-03 19:31:47 +08:00
// 接收来自background.js的消息数据
2021-04-28 13:59:16 +08:00
connectBackground.onBackgroundMessage((data: PluginEvent, sender: any) => {
2021-04-03 19:31:47 +08:00
if (!data) {
return;
2019-03-15 10:08:39 +08:00
}
2021-11-04 21:01:33 +08:00
if (data.target === Page.Devtools) {
2021-11-06 21:42:37 +08:00
console.log("[Devtools]", data);
2021-04-28 19:49:26 +08:00
PluginEvent.finish(data);
2021-11-18 22:41:07 +08:00
const {msg} = data;
if (msg) {
const func = msgFunctionMap[msg];
if (func) {
func(data.data)
} else {
console.warn(`没有${msg}消息的函数`)
2021-11-17 23:28:40 +08:00
}
2021-04-26 22:27:47 +08:00
}
2021-04-03 19:31:47 +08:00
}
});
2021-05-08 17:52:29 +08:00
}
2021-11-18 22:41:07 +08:00
_onMsgGetObjectItemData(requestData: ObjectItemRequestData) {
if (requestData.id !== null) {
let findIndex = this.requestList.findIndex(el => el.id === requestData.id)
if (findIndex > -1) {
let del = this.requestList.splice(findIndex, 1)[0];
del.cb(requestData.data);
}
}
}
_onMsgUpdateFrames(details: FrameDetails[]) {
// 先把iframes里面无效的清空了
this.iframes = this.iframes.filter(item => {
details.find(el => el.frameID === item.value)
})
// 同步配置
details.forEach(item => {
let findItem = this.iframes.find(el => el.value === item.frameID);
if (findItem) {
findItem.label = item.url;
} else {
this.iframes.push({
label: item.url,
value: item.frameID,
})
}
})
// 第一次获取到frame配置后自动获取frame数据
if (this.frameID === null && this.iframes.length > 0 && !this.iframes.find(el => el.value === this.frameID)) {
this.frameID = this.iframes[0].value;
this.onChangeFrame();
}
}
_onMsgUpdateProperty(data: Info) {
2021-05-08 17:52:29 +08:00
const uuid = data.path[0];
const key = data.path[1];
const value = data.data;
2021-05-11 22:15:29 +08:00
let treeArray: Array<TreeData> = [];
function circle(array: Array<TreeData>) {
array.forEach(item => {
treeArray.push(item);
circle(item.children);
})
}
2021-05-08 17:52:29 +08:00
2021-05-11 22:15:29 +08:00
// 更新指定uuid节点的tree的name
circle(this.treeData)
let ret = treeArray.find(el => el.uuid === uuid);
if (ret) {
2021-11-04 21:01:33 +08:00
if (key === "name") {
2021-05-08 17:52:29 +08:00
ret.name = value;
}
2021-11-04 21:01:33 +08:00
if (key === "active") {
2021-05-11 22:15:29 +08:00
ret.active = !!value;
}
2021-05-08 17:52:29 +08:00
}
2021-04-03 19:31:47 +08:00
}
2019-04-16 10:34:34 +08:00
2021-05-08 17:52:29 +08:00
handleNodeClick(data: TreeData) {
this.selectedUUID = data.uuid;
2021-11-21 21:36:16 +08:00
this.updateNodeInfo();
}
private updateNodeInfo() {
if (this.selectedUUID) {
this.sendMsgToContentScript(Msg.NodeInfo, this.selectedUUID);
2021-04-03 19:31:47 +08:00
}
}
2019-04-16 10:34:34 +08:00
2021-11-20 16:10:52 +08:00
onEnableTreeWatch(watch: boolean, time = 300) {
if (watch) {
this._clearTimer();
2021-04-03 22:45:51 +08:00
this.timerID = setInterval(() => {
2021-04-03 19:31:47 +08:00
this.onBtnClickUpdateTree();
2021-11-20 16:10:52 +08:00
}, time);
2021-04-03 19:31:47 +08:00
} else {
2021-11-20 16:10:52 +08:00
this._clearTimer();
}
}
private _clearTimer() {
if (this.timerID !== null) {
2021-04-03 19:31:47 +08:00
clearInterval(this.timerID);
2021-11-20 16:10:52 +08:00
this.timerID = null;
2021-04-03 19:31:47 +08:00
}
}
2021-11-06 20:19:21 +08:00
sendMsgToContentScript(msg: Msg, data?: any) {
2021-04-05 18:38:44 +08:00
if (!chrome || !chrome.devtools) {
console.log("环境异常,无法执行函数");
return;
2021-04-03 19:31:47 +08:00
}
2021-05-08 17:52:29 +08:00
connectBackground.postMessageToBackground(msg, data);
2021-04-26 22:27:47 +08:00
}
// 问题没有上下文的权限只能操作DOM
_executeScript(para: Object) {
let tabID = chrome.devtools.inspectedWindow.tabId;
2021-04-22 19:09:35 +08:00
//@ts-ignore
2021-04-26 22:27:47 +08:00
chrome.tabs.executeScript(tabID, {code: `var CCInspectorPara='${JSON.stringify(para)}';`}, () => {
2021-04-22 19:09:35 +08:00
//@ts-ignore
2021-04-26 22:27:47 +08:00
chrome.tabs.executeScript(tabID, {file: "js/execute.js"})
2021-04-22 19:09:35 +08:00
});
}
2021-04-03 19:31:47 +08:00
2021-04-22 19:09:35 +08:00
_inspectedCode() {
2021-11-04 21:01:33 +08:00
let injectCode = "";
2021-04-05 18:38:44 +08:00
chrome.devtools.inspectedWindow.eval(injectCode, (result, isException) => {
if (isException) {
console.error(isException);
} else {
console.log(`执行结果:${result}`)
2021-04-03 19:31:47 +08:00
}
2021-04-05 18:38:44 +08:00
});
2021-04-03 19:31:47 +08:00
}
onBtnClickUpdateTree() {
2021-11-18 22:41:07 +08:00
this.sendMsgToContentScript(Msg.TreeInfo, this.frameID);
2021-04-03 19:31:47 +08:00
}
onBtnClickUpdatePage() {
2021-11-06 20:19:21 +08:00
this.sendMsgToContentScript(Msg.Support);
2019-03-15 10:08:39 +08:00
}
2021-04-03 19:31:47 +08:00
onMemoryTest() {
2021-11-06 20:19:21 +08:00
this.sendMsgToContentScript(Msg.MemoryInfo);
2019-03-15 10:08:39 +08:00
}
2021-05-08 17:52:29 +08:00
onNodeExpand(data: TreeData) {
2021-11-04 21:01:33 +08:00
if (data.hasOwnProperty("uuid") && data.uuid) {
this.expandedKeys.push(data.uuid)
}
}
2021-05-08 17:52:29 +08:00
onNodeCollapse(data: TreeData) {
2021-11-04 21:01:33 +08:00
if (data.hasOwnProperty("uuid")) {
let index = this.expandedKeys.findIndex(el => el === data.uuid);
if (index !== -1) {
this.expandedKeys.splice(index, 1)
}
}
}
2021-04-03 19:31:47 +08:00
}
</script>
2019-03-15 10:08:39 +08:00
2021-04-03 19:31:47 +08:00
<style scoped lang="less">
2021-10-30 16:40:51 +08:00
@import "../../index.less";
2019-03-15 10:08:39 +08:00
2021-04-03 19:31:47 +08:00
#devtools {
display: flex;
2021-11-17 23:28:40 +08:00
flex-direction: column;
2021-04-03 19:31:47 +08:00
width: 100%;
height: 100%;
2021-11-17 23:28:40 +08:00
overflow: hidden;
.head {
display: flex;
flex-direction: row;
align-items: center;
padding: 1px 0;
border-bottom: solid 1px grey;
.label {
margin: 0 3px;
}
}
2019-03-15 10:08:39 +08:00
2021-04-03 19:31:47 +08:00
.no-find {
display: flex;
flex: 1;
flex-direction: row;
align-items: center;
justify-content: center;
2019-03-18 12:05:07 +08:00
2021-04-03 19:31:47 +08:00
span {
margin-right: 20px;
}
2019-03-18 12:05:07 +08:00
}
2021-04-03 19:31:47 +08:00
.find {
display: flex;
flex: 1;
2019-03-18 12:05:07 +08:00
flex-direction: row;
2021-11-12 22:50:39 +08:00
overflow: auto;
2019-03-18 12:05:07 +08:00
2021-04-03 19:31:47 +08:00
.left {
display: flex;
flex-direction: column;
2021-12-05 17:40:48 +08:00
width: 300px;
2019-03-18 12:05:07 +08:00
2021-04-03 19:31:47 +08:00
.tool-btn {
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
}
2021-11-12 22:30:08 +08:00
.matchCase {
width: 30px;
height: 26px;
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
}
2021-11-12 22:50:39 +08:00
2021-04-03 19:31:47 +08:00
.treeList {
margin-top: 3px;
height: 100%;
border-radius: 4px;
min-height: 20px;
2021-04-07 17:26:16 +08:00
overflow: auto;
2021-12-05 17:40:48 +08:00
width: 100%;
2021-04-03 19:31:47 +08:00
2021-05-11 22:15:29 +08:00
.leaf {
width: 100%;
}
.leaf-show {
color: black;
}
.leaf-hide {
color: #c7bbbb;
text-decoration: line-through;
}
2021-04-03 19:31:47 +08:00
&::-webkit-scrollbar {
width: 6px;
height: 6px;
background: #999;
border-radius: 2px;
}
&::-webkit-scrollbar-thumb {
background-color: #333;
border-radius: 2px;
}
}
}
.right {
2021-04-06 19:00:38 +08:00
flex: 1;
2021-04-04 19:21:17 +08:00
background: #e5e9f2;
2021-04-07 17:26:16 +08:00
overflow-x: hidden;
2021-11-12 22:50:39 +08:00
overflow-y: overlay;
2021-04-07 17:26:16 +08:00
&::-webkit-scrollbar {
2021-11-12 22:50:39 +08:00
width: 6px;
2021-04-07 17:26:16 +08:00
background: #999;
border-radius: 2px;
height: 6px;
}
2021-04-07 17:26:16 +08:00
&::-webkit-scrollbar-thumb {
background-color: #333;
border-radius: 2px;
}
2021-04-03 19:31:47 +08:00
}
2019-03-18 12:05:07 +08:00
}
2021-04-03 19:31:47 +08:00
}
2019-03-15 10:08:39 +08:00
</style>