423 lines
11 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">
<div v-show="isShowDebug" class="find">
<div v-if="false">
<el-button type="success" @click="onBtnClickTest1">Test1</el-button>
<el-button type="success" @click="onBtnClickTest2">Test2</el-button>
<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-04-03 19:31:47 +08:00
<div class="left">
<div class="tool-btn">
<el-switch active-text="实时监控" v-model="watchEveryTime" @change="onChangeWatchState"></el-switch>
<div class="flex1"></div>
<el-button type="success" class="el-icon-refresh" @click="onBtnClickUpdateTree"></el-button>
</div>
<div class="treeList">
<el-tree :data="treeData"
style="display: inline-block;"
:props="defaultProps"
:highlight-current="true"
:default-expand-all="false"
:expand-on-click-node="true"
@node-click="handleNodeClick"></el-tree>
</div>
</div>
<div class="right">
2021-04-04 19:21:17 +08:00
<NodeBaseProperty :item-data="treeItemData"></NodeBaseProperty>
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">
<span>未发现cocos creator的游戏!</span>
<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 injectScript from '../injectScript.js'
// import EvalCode from "./evalCodeString.js";
import Vue from "vue";
import {Component, Prop} from "vue-property-decorator";
2021-04-03 22:45:51 +08:00
import SceneProperty from "@/devtools/ccType/SceneProperty.vue";
import ComponentsProperty from "@/devtools/ccType/ComponentsProperty.vue";
import NodeBaseProperty from "@/devtools/ccType/NodeBaseProperty.vue";
2021-04-03 19:31:47 +08:00
const PluginMsg = require("../core/plugin-msg");
@Component({
components: {
2021-04-03 22:45:51 +08:00
NodeBaseProperty, ComponentsProperty, SceneProperty,
}
})
2021-04-03 19:31:47 +08:00
export default class Index extends Vue {
2021-04-04 20:48:18 +08:00
private isShowDebug: boolean = false;
2021-04-03 22:45:51 +08:00
treeItemData: Record<string, any> = {};
treeData: Array<Record<string, any>> = []
bgConn: chrome.runtime.Port | null = null// 与background.js的链接
2021-04-03 19:31:47 +08:00
// el-tree的渲染key
2021-04-03 19:31:47 +08:00
defaultProps = {
children: "children",
label: "name"
};
private watchEveryTime: boolean = false;// 实时监控节点树
2021-04-03 19:31:47 +08:00
memory = {
performance: {},
console: {},
}
2021-04-03 22:45:51 +08:00
timerID: number = 0;
2019-03-18 12:05:07 +08:00
2021-04-03 19:31:47 +08:00
created() {
this.onTestData();
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.data));
console.log("on vue:" + JSON.stringify(event));
}, false);
}
2019-03-18 12:05:07 +08:00
2021-04-03 19:31:47 +08:00
_initChromeRuntimeConnect() {
// chrome.devtools.inspectedWindow.tabId
// 接收来自background.js的消息数据
this.bgConn = chrome.runtime.connect({name: PluginMsg.Page.Devtools});
this.bgConn.onMessage.addListener((data, sender) => {
if (!data) {
return;
2019-03-15 10:08:39 +08:00
}
2021-04-03 19:31:47 +08:00
let eventData = data.data;
let eventMsg = data.msg;
if (eventMsg === PluginMsg.Msg.ListInfo) {
this.isShowDebug = true;
this._updateTreeView(eventData);
} else if (eventMsg === PluginMsg.Msg.Support) {
this.isShowDebug = eventData.support;
} else if (eventMsg === PluginMsg.Msg.NodeInfo) {
this.isShowDebug = true;
this.treeItemData = eventData;
} else if (eventMsg === PluginMsg.Msg.MemoryInfo) {
this.memory = eventData;
}
});
}
2019-04-16 10:34:34 +08:00
2021-04-03 19:31:47 +08:00
onTestData() {
for (let i = 0; i < 40; i++) {
this.treeData.push({name: `node${i}`, children: [{name: `children11111111111111111111111111111111111111${i}`}]})
}
2019-04-16 10:34:34 +08:00
2021-04-03 19:31:47 +08:00
this.treeItemData = {
2021-04-03 22:45:51 +08:00
"uuid": "11",
"name": "name",
2021-04-03 19:31:47 +08:00
"type": "cc_Node",
"height": 1080.986301369863,
"color": "#fff85f",
"opacity": 255,
"components": [
{
"uuid": "Comp.931",
"type": "cc_Canvas",
"name": "Canvas<Canvas>"
},
{
"uuid": "Comp.932",
"type": "HotUpdateScene",
"name": "Canvas<HotUpdateScene>"
}],
"active": true
};
}
2019-04-16 10:34:34 +08:00
2021-04-03 22:45:51 +08:00
handleNodeClick(data: any) {
2021-04-03 19:31:47 +08:00
// todo 去获取节点信息
// console.log(data);
let uuid = data.uuid;
if (uuid !== undefined) {
this.evalInspectorFunction("getNodeInfo", `"${uuid}"`);
}
}
2019-04-16 10:34:34 +08:00
2021-04-03 19:31:47 +08:00
onChangeWatchState() {
if (this.watchEveryTime) {
2021-04-03 22:45:51 +08:00
this.timerID = setInterval(() => {
2021-04-03 19:31:47 +08:00
this.onBtnClickUpdateTree();
2021-04-03 22:45:51 +08:00
}, 100);
2021-04-03 19:31:47 +08:00
} else {
clearInterval(this.timerID);
}
2019-04-16 10:34:34 +08:00
2021-04-03 19:31:47 +08:00
}
2021-04-03 22:45:51 +08:00
_updateTreeView(data: any) {
2021-04-03 19:31:47 +08:00
this.treeData = [data.scene];
return;
// 构建树形数据
if (this.treeData.length === 0) {// 第一次赋值
} else {
}
let treeData = [];
debugger
let sceneData = data.scene;
if (sceneData) {
// scene info
let dataRoot = {
type: sceneData.type, uuid: sceneData.uuid,
label: sceneData.name, children: []
};
treeData.push(dataRoot);
this.handleNodeClick(dataRoot);
// scene children info
for (let k in sceneData.children) {
let itemSceneData = sceneData.children[k];
// let sceneItem = {uuid: itemSceneData.uuid, label: itemSceneData.name, children: []};
let sceneItem = {};
dealChildrenNode(itemSceneData, sceneItem);
treeData[0].children.push(sceneItem);
}
}
this.treeData = treeData;
2021-04-03 22:45:51 +08:00
function dealChildrenNode(rootData: any, obj: any) {
2021-04-03 19:31:47 +08:00
obj["data"] = rootData;
obj["uuid"] = rootData.uuid;
obj["label"] = rootData.name;
obj["type"] = rootData.type;
obj["children"] = [];
let rootChildren = rootData.children;
for (let k in rootChildren) {
let itemData = rootChildren[k];
let item = {};
dealChildrenNode(itemData, item);
obj.children.push(item);
}
}
}
_getInjectScriptString() {
let injectScript = "";
let code = injectScript.toString();
let array = code.split("\n");
array.splice(0, 1);// 删除开头
array.splice(-1, 1);// 删除结尾
let evalCode = "";
for (let i = 0; i < array.length; i++) {
evalCode += array[i] + "\n";
}
// console.log(evalCode);
return evalCode;
}
2021-04-03 22:45:51 +08:00
evalInspectorFunction(funcString: string, parm?: any) {
2021-04-03 19:31:47 +08:00
if (funcString || funcString.length > 0) {
let injectCode =
`if(window.ccinspector){
2019-03-18 12:05:07 +08:00
let func = window.ccinspector.${funcString};
if(func){
console.log("执行${funcString}成功");
func.apply(window.ccinspector,[${parm}]);
}else{
console.log("未发现${funcString}函数");
}
}else{
console.log("可能脚本没有注入");
}`;
2021-04-03 19:31:47 +08:00
console.log(injectCode);
if (chrome && chrome.devtools) {
let ret = chrome.devtools.inspectedWindow.eval(injectCode, function (result, info) {
if (info && info.isException) {
console.log(info.value);
}
});
console.log(`ret:${ret}`);
}
} else {
console.log("执行失败!");
}
}
onBtnClickUpdateTree() {
this.evalInspectorFunction("updateTreeInfo");
}
onBtnClickUpdatePage() {
this.evalInspectorFunction("checkIsGamePage", "true");
// let code = this._getInjectScriptString();
// chrome.devtools.inspectedWindow.eval(code, function () {
// console.log("刷新成功!");
// });
}
onBtnClickTest1() {
chrome.devtools.inspectedWindow.eval(`window.ccinspector.testMsg1()`);
}
_getTime() {
return new Date().getTime().toString();
}
onBtnClickTest2() {
// chrome.devtools.inspectedWindow.eval(`window.ccinspector.testMsg2()`)
let newData = [
{
name: this._getTime(),
children: [
2019-04-16 10:34:34 +08:00
{
name: this._getTime(),
children: [
{
name: this._getTime(),
}
]
2021-04-03 19:31:47 +08:00
},
{
name: this._getTime(),
2019-04-16 10:34:34 +08:00
}
]
2021-04-03 19:31:47 +08:00
}
2019-04-16 10:34:34 +08:00
2021-04-03 19:31:47 +08:00
];
// this.treeData = newData;
this._update37(this.treeData[0], newData[0]);
}
2019-04-16 10:34:34 +08:00
2021-04-03 22:45:51 +08:00
_update37(oldTreeNode: any, newTreeNode: any) {
2021-04-03 19:31:47 +08:00
debugger
if (!newTreeNode) {
return;
}
if (!oldTreeNode) {
oldTreeNode = {name: "", children: []};
}
if (oldTreeNode.name !== newTreeNode.name) {
oldTreeNode.name = newTreeNode.name;
}
2019-04-16 10:34:34 +08:00
2021-04-03 19:31:47 +08:00
let oldChildren = oldTreeNode.children;
let newChildren = newTreeNode.children;
if (oldChildren.length === 0) {
oldChildren = newChildren;
} else {
// 比较2个数据: treeData, newTreeData
// 比较该层级的数据
for (let i = 0; i < newChildren.length; i++) {
let itemNew = newChildren[i];
let itemOld = oldChildren[i];
if (itemOld === undefined) {
// 老节点中没有
oldChildren.push(itemNew);
} else if (itemNew.name !== itemOld.name) {
// 替换
oldChildren.splice(i, 1, itemNew);
2019-04-16 10:34:34 +08:00
} else {
2021-04-03 19:31:47 +08:00
this._update37(itemOld, itemNew);
2019-04-16 10:34:34 +08:00
}
2021-04-03 19:31:47 +08:00
}
// 多余的删除了
if (oldChildren.length > newChildren.length) {
oldChildren.splice(newChildren.length, oldChildren.length - newChildren.length);
2019-03-17 21:44:47 +08:00
}
2019-03-15 10:08:39 +08:00
}
}
2021-04-03 19:31:47 +08:00
onBtnClickTest3() {
// chrome.devtools.inspectedWindow.eval(`window.ccinspector.testMsg3()`)
2019-03-15 10:08:39 +08:00
}
2021-04-03 19:31:47 +08:00
onMemoryTest() {
this.evalInspectorFunction("onMemoryInfo");
2019-03-15 10:08:39 +08:00
}
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">
@import "../index.less";
2019-03-15 10:08:39 +08:00
2021-04-03 19:31:47 +08:00
#devtools {
display: flex;
width: 100%;
height: 100%;
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-04-03 19:31:47 +08:00
.left {
display: flex;
flex: 1;
flex-direction: column;
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;
}
.treeList {
margin-top: 3px;
flex: 1;
height: 100%;
border-radius: 4px;
min-height: 20px;
overflow: scroll;
width: 300px;
&::-webkit-scrollbar {
width: 6px;
height: 6px;
background: #999;
border-radius: 2px;
}
&::-webkit-scrollbar-thumb {
background-color: #333;
border-radius: 2px;
}
}
}
.right {
flex: 2;
2021-04-04 19:21:17 +08:00
background: #e5e9f2;
overflow: scroll;
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>