422 lines
10 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="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"
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"
: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-06-19 19:51:05 +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>
<div class="right">
2021-04-20 11:15:30 +08:00
<properties :all-group="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">
<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 Vue from "vue";
2021-04-22 19:09:35 +08:00
import {Component} from "vue-property-decorator";
2021-04-20 11:15:30 +08:00
import properties from "./propertys.vue";
2021-04-28 19:49:26 +08:00
import {Msg, Page, PluginEvent} from '@/core/types'
2021-04-26 22:27:47 +08:00
import {connectBackground} from "@/devtools/connectBackground";
2021-06-19 19:51:05 +08:00
import {EngineData, Info, TreeData} from "@/devtools/data";
import Bus, {BusMsg} from './bus';
2021-04-03 19:31:47 +08:00
@Component({
components: {
2021-04-20 11:15:30 +08:00
properties,
}
})
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-04-05 18:38:44 +08:00
treeItemData: Array<Record<string, any>> = [];
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
// 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() {
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);
})
}
_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;
return h('span', {class:''}, data.name)
// return(<span>1111</span>)
2021-04-03 19:31:47 +08:00
}
2019-03-18 12:05:07 +08:00
2021-05-08 17:52:29 +08:00
_onMsgListInfo(eventData: Array<TreeData>) {
2021-04-22 19:09:35 +08:00
this.isShowDebug = true;
if (!Array.isArray(eventData)) {
eventData = [eventData]
}
this.treeData = eventData;
if (this.selectedUUID) {
this.$nextTick(() => {
//@ts-ignore
this.$refs.tree.setCurrentKey(this.selectedUUID);
// todo 需要重新获取下node的数据
})
}
}
_onMsgNodeInfo(eventData: any) {
this.isShowDebug = true;
if (!Array.isArray(eventData)) {
eventData = [eventData]
}
this.treeItemData = eventData;
}
_onMsgMemory(eventData: any) {
this.memory = eventData;
}
2021-05-08 17:52:29 +08:00
_onMsgSupport(data: boolean) {
2021-04-28 19:49:26 +08:00
this.isShowDebug = data;
if (data) {
2021-04-26 22:27:47 +08:00
// 如果节点树为空,就刷新一次
if (this.treeData.length === 0) {
2021-05-11 21:26:36 +08:00
this.onBtnClickUpdateTree();
2021-04-26 22:27:47 +08:00
}
2021-04-22 19:09:35 +08:00
} else {
this._reset();
}
}
_reset() {
this.treeData = [];
this.treeItemData = [];
}
2021-04-03 19:31:47 +08:00
_initChromeRuntimeConnect() {
// 接收来自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-04-28 19:49:26 +08:00
if (PluginEvent.check(data, Page.Background, Page.Devtools)) {
console.log(`[Devtools] ${JSON.stringify(data)}`);
PluginEvent.finish(data);
2021-04-28 13:59:16 +08:00
let eventData: any = data.data;
switch (data.msg) {
case Msg.UrlChange: {
break;
}
case Msg.TreeInfo: {
2021-05-08 17:52:29 +08:00
this._onMsgListInfo(eventData as Array<TreeData>);
2021-04-28 13:59:16 +08:00
break;
}
case Msg.Support: {
2021-04-28 19:49:26 +08:00
this._onMsgSupport(!!eventData)
2021-04-28 13:59:16 +08:00
break;
}
case Msg.NodeInfo: {
this._onMsgNodeInfo(eventData);
break;
}
case Msg.MemoryInfo: {
this._onMsgMemory(eventData)
break;
}
2021-05-08 17:52:29 +08:00
case Msg.UpdateProperty: {
this._updateProperty(eventData)
break;
}
2021-04-28 13:59:16 +08:00
case Msg.TabsInfo: {
debugger
break
}
2021-04-26 22:27:47 +08:00
}
2021-04-03 19:31:47 +08:00
}
});
2021-05-08 17:52:29 +08:00
}
_updateProperty(data: Info) {
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) {
if (key === 'name') {
2021-05-08 17:52:29 +08:00
ret.name = value;
}
2021-05-11 22:15:29 +08:00
if (key === 'active') {
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-04-03 19:31:47 +08:00
let uuid = data.uuid;
if (uuid !== undefined) {
2021-04-28 13:59:16 +08:00
this.runToContentScript(Msg.NodeInfo, uuid);
2021-04-03 19:31:47 +08:00
}
}
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-05-11 21:26:36 +08:00
if (this.selectedUUID) {
this.runToContentScript(Msg.NodeInfo, this.selectedUUID);
}
}, 300);
2021-04-03 19:31:47 +08:00
} else {
clearInterval(this.timerID);
}
}
2021-04-28 13:59:16 +08:00
runToContentScript(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() {
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-04-28 13:59:16 +08:00
this.runToContentScript(Msg.TreeInfo);
2021-04-03 19:31:47 +08:00
}
onBtnClickUpdatePage() {
2021-04-28 13:59:16 +08:00
this.runToContentScript(Msg.Support);
2019-03-15 10:08:39 +08:00
}
2021-04-03 19:31:47 +08:00
onMemoryTest() {
2021-04-28 13:59:16 +08:00
this.runToContentScript(Msg.MemoryInfo);
2019-03-15 10:08:39 +08:00
}
2021-05-08 17:52:29 +08:00
onNodeExpand(data: TreeData) {
2021-04-22 19:09:35 +08:00
if (data.hasOwnProperty('uuid') && data.uuid) {
this.expandedKeys.push(data.uuid)
}
}
2021-05-08 17:52:29 +08:00
onNodeCollapse(data: TreeData) {
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">
@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-07 17:26:16 +08:00
overflow: hidden;
2019-03-18 12:05:07 +08:00
2021-04-03 19:31:47 +08:00
.left {
display: flex;
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;
height: 100%;
border-radius: 4px;
min-height: 20px;
2021-04-07 17:26:16 +08:00
overflow: auto;
2021-04-03 19:31:47 +08:00
width: 300px;
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;
overflow-y: auto;
&::-webkit-scrollbar {
width: 0;
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>