619 lines
17 KiB
Vue
Raw Normal View History

2024-01-05 16:45:23 +08:00
<template>
2024-01-09 12:02:47 +08:00
<div id="devtools">
2024-12-09 19:25:06 +08:00
<Test @valid-game="testValidGame"> </Test>
2024-01-09 12:02:47 +08:00
<div class="head" v-show="iframes.length > 1">
<div class="label">inspect target:</div>
2024-02-26 09:07:01 +08:00
<CCSelect v-model:value="frameID" placeholder="please select ..." @change="onChangeFrame" :data="getFramesData()"> </CCSelect>
2024-01-09 12:02:47 +08:00
</div>
<div v-show="isShowDebug" class="find">
<div v-if="false">
2024-01-09 15:33:34 +08:00
<CCButton type="success" @click="onMemoryTest">内存测试</CCButton>
2024-01-09 12:02:47 +08:00
<span>JS堆栈限制: {{ memory.performance.jsHeapSizeLimit }}</span>
<span>JS堆栈大小: {{ memory.performance.totalJSHeapSize }}</span>
<span>JS堆栈使用: {{ memory.performance.usedJSHeapSize }}</span>
</div>
2024-01-23 22:25:32 +08:00
<div class="left">
2024-01-09 12:02:47 +08:00
<div class="tool-btn">
2024-01-23 22:25:32 +08:00
<div style="padding-left: 5px; flex: 1; user-select: none">Node Tree</div>
<CCButtonGroup :items="buttonGroup" :recover="true"></CCButtonGroup>
2024-01-09 12:02:47 +08:00
</div>
2024-01-09 15:33:34 +08:00
<CCInput placeholder="enter keywords to filter" :data="filterText">
<slot>
2024-01-23 22:25:32 +08:00
<i class="matchCase iconfont icon_font_size" @click.stop="onChangeCase" title="match case" :style="{ color: matchCase ? 'red' : '' }"></i>
2024-01-09 15:33:34 +08:00
</slot>
</CCInput>
2024-01-09 12:02:47 +08:00
<div class="treeList">
2024-02-26 09:07:01 +08:00
<CCTree></CCTree>
2024-01-09 12:02:47 +08:00
<!-- <el-tree
:data="treeData"
ref="tree"
style="display: inline-block"
:props="defaultProps"
:highlight-current="true"
:default-expand-all="false"
:default-expanded-keys="expandedKeys"
:filter-node-method="filterNode"
:expand-on-click-node="false"
node-key="uuid"
@node-expand="onNodeExpand"
@node-collapse="onNodeCollapse"
@node-click="handleNodeClick"
>
<span
slot-scope="{ node, data }"
class="leaf"
:class="data.active ? 'leaf-show' : 'leaf-hide'"
>
<span>{{ node.label }}</span>
</span>
</el-tree> -->
</div>
</div>
2024-01-23 22:25:32 +08:00
<CCDivider></CCDivider>
<div class="right">
2024-01-09 12:02:47 +08:00
<properties v-if="treeItemData" :data="treeItemData"></properties>
</div>
</div>
<div v-show="!isShowDebug" class="no-find">
<span>No games created by cocos creator found!</span>
2024-01-09 16:34:16 +08:00
<CCButton type="success" @click="onBtnClickUpdatePage">
<i class="iconfont icon_refresh"></i>
2024-01-09 15:33:34 +08:00
</CCButton>
2024-01-09 12:02:47 +08:00
</div>
2024-12-09 19:25:06 +08:00
<CCDialog></CCDialog>
<CCFootBar :version="version"></CCFootBar>
2024-01-09 12:02:47 +08:00
</div>
</template>
<script lang="ts">
2024-01-09 15:33:34 +08:00
import ccui from "@xuyanfeng/cc-ui";
2024-12-09 16:23:58 +08:00
import { ButtonGroupItem } from "@xuyanfeng/cc-ui/types/cc-button-group/const";
import { Option } from "@xuyanfeng/cc-ui/types/cc-select/const";
import { ITreeData } from "@xuyanfeng/cc-ui/types/cc-tree/const";
2024-12-09 19:25:06 +08:00
import { _UnwrapAll, Store, storeToRefs } from "pinia";
import { defineComponent, nextTick, onMounted, PropType, reactive, Ref, ref, toRaw, watch } from "vue";
import PluginConfig from "../../../cc-plugin.config";
2024-01-09 12:02:47 +08:00
import { Msg, Page, PluginEvent } from "../../core/types";
2024-12-09 16:23:58 +08:00
import Bus, { BusMsg } from "./bus";
2024-01-09 12:02:47 +08:00
import { connectBackground } from "./connectBackground";
2024-01-23 22:25:32 +08:00
import { EngineData, FrameDetails, Info, NodeInfoData, ObjectData, ObjectItemRequestData, TreeData } from "./data";
2024-12-09 19:25:06 +08:00
import { appStore, RefreshType } from "./store";
import Test from "./test.vue";
2024-12-09 16:23:58 +08:00
import properties from "./ui/propertys.vue";
import SettingsVue from "./ui/settings.vue";
2024-12-09 19:25:06 +08:00
const { CCTree, CCFootBar, CCDialog, CCInput, CCButton, CCInputNumber, CCSelect, CCButtonGroup, CCCheckBox, CCColor, CCDivider } = ccui.components;
2024-01-09 15:33:34 +08:00
interface FrameInfo {
label: string;
value: number;
}
2024-01-09 12:02:47 +08:00
export default defineComponent({
2024-12-09 19:25:06 +08:00
components: { Test, CCFootBar, CCDialog, CCTree, CCDivider, CCButtonGroup, properties, SettingsVue, CCInput, CCButton, CCInputNumber, CCSelect, CCCheckBox, CCColor },
2024-01-09 15:33:34 +08:00
name: "devtools",
props: {},
2024-01-09 12:02:47 +08:00
setup(props, ctx) {
2024-12-09 19:25:06 +08:00
appStore().init();
const { config } = storeToRefs(appStore());
2024-01-09 15:33:34 +08:00
const treeItemData = ref<NodeInfoData | null>({ uuid: "", group: [] });
2024-01-23 22:25:32 +08:00
const isShowDebug = ref<boolean>(true);
2024-01-09 15:33:34 +08:00
const frameID = ref<number>(0);
const iframes = ref<Array<FrameInfo>>([]);
2024-01-23 22:25:32 +08:00
const btnRefresh: ButtonGroupItem = reactive<ButtonGroupItem>({
icon: "icon_refresh",
click: () => {
onBtnClickUpdateTree();
},
visible: true,
});
const buttonGroup = ref<ButtonGroupItem[]>([
btnRefresh,
{
icon: "icon_settings",
click: () => {
2024-12-09 19:25:06 +08:00
ccui.dialog.showDialog({
comp: SettingsVue,
title: "Settings",
});
2024-01-23 22:25:32 +08:00
},
},
]);
2024-01-09 12:02:47 +08:00
function _checkSelectedUUID() {
2024-01-09 15:33:34 +08:00
if (selectedUUID) {
const b = _findUuidInTree(toRaw(treeData.value), selectedUUID);
2024-01-09 12:02:47 +08:00
if (b) {
return true;
}
}
2024-01-09 15:33:34 +08:00
selectedUUID = null;
treeItemData.value = null;
2024-01-09 12:02:47 +08:00
return false;
}
function _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);
}
const requestList: Array<{ id: string; cb: Function }> = [];
function _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();
}
}
}
}
2024-01-09 15:33:34 +08:00
circle(treeData);
2024-01-09 12:02:47 +08:00
expandKeys.forEach((key) => {
2024-01-09 15:33:34 +08:00
if (!expandedKeys.value.find((el) => el === key)) {
expandedKeys.value.push(key);
2024-01-09 12:02:47 +08:00
}
});
// 高亮uuid
}
function _onMsgNodeInfo(eventData: NodeInfoData) {
2024-01-09 15:33:34 +08:00
isShowDebug.value = true;
treeItemData.value = eventData;
2024-01-09 12:02:47 +08:00
}
function _onMsgMemoryInfo(eventData: any) {
2024-01-09 15:33:34 +08:00
memory.value = eventData;
2024-01-09 12:02:47 +08:00
}
function _onMsgSupport(isCocosGame: boolean) {
2024-01-09 15:33:34 +08:00
isShowDebug.value = isCocosGame;
2024-01-09 12:02:47 +08:00
if (isCocosGame) {
syncSettings();
2024-01-09 15:33:34 +08:00
onBtnClickUpdateTree();
2024-01-09 12:02:47 +08:00
} else {
2024-01-09 15:33:34 +08:00
_clearTimer();
treeData.value.length = 0;
treeItemData.value = null;
selectedUUID = null;
2024-01-09 12:02:47 +08:00
}
}
function _onMsgGetObjectItemData(requestData: ObjectItemRequestData) {
if (requestData.id !== null) {
2024-01-09 15:33:34 +08:00
let findIndex = requestList.findIndex((el) => el.id === requestData.id);
2024-01-09 12:02:47 +08:00
if (findIndex > -1) {
2024-01-09 15:33:34 +08:00
let del = requestList.splice(findIndex, 1)[0];
2024-01-09 12:02:47 +08:00
del.cb(requestData.data);
}
}
}
function _onMsgUpdateFrames(details: FrameDetails[]) {
// 先把iframes里面无效的清空了
2024-01-09 15:33:34 +08:00
iframes.value = iframes.value.filter((item) => {
2024-01-09 12:02:47 +08:00
details.find((el) => el.frameID === item.value);
});
// 同步配置
details.forEach((item) => {
2024-01-09 15:33:34 +08:00
let findItem = iframes.value.find((el) => el.value === item.frameID);
2024-01-09 12:02:47 +08:00
if (findItem) {
findItem.label = item.url;
} else {
2024-01-09 15:33:34 +08:00
iframes.value.push({
2024-01-09 12:02:47 +08:00
label: item.url,
value: item.frameID,
});
}
});
// 第一次获取到frame配置后自动获取frame数据
2024-01-23 22:25:32 +08:00
if (frameID === null && iframes.value.length > 0 && !iframes.value.find((el) => el.value === frameID.value)) {
2024-01-09 15:33:34 +08:00
frameID.value = iframes[0].value;
onChangeFrame();
2024-01-09 12:02:47 +08:00
}
}
function _onMsgUpdateProperty(data: Info) {
const uuid = data.path[0];
const key = data.path[1];
const value = data.data;
let treeArray: Array<TreeData> = [];
function circle(array: Array<TreeData>) {
array.forEach((item) => {
treeArray.push(item);
circle(item.children);
});
}
// 更新指定uuid节点的tree的name
2024-01-09 15:33:34 +08:00
circle(treeData.value);
2024-01-09 12:02:47 +08:00
let ret = treeArray.find((el) => el.uuid === uuid);
if (ret) {
if (key === "name") {
ret.name = value;
}
if (key === "active") {
ret.active = !!value;
}
}
}
// 问题没有上下文的权限只能操作DOM
function _executeScript(para: Object) {
2024-02-26 09:07:01 +08:00
// chrome.tabs.executeScript()//v2版本使用的函数
const tabID = chrome.devtools.inspectedWindow.tabId;
chrome.scripting.executeScript({ files: ["js/execute.js"], target: { tabId: tabID } }, (results: chrome.scripting.InjectionResult[]) => {});
2024-01-09 12:02:47 +08:00
}
function _inspectedCode() {
let injectCode = "";
2024-01-23 22:25:32 +08:00
chrome.devtools.inspectedWindow.eval(injectCode, (result, isException) => {
if (isException) {
console.error(isException);
} else {
console.log(`执行结果:${result}`);
2024-01-09 12:02:47 +08:00
}
2024-01-23 22:25:32 +08:00
});
2024-01-09 12:02:47 +08:00
}
2024-01-09 15:33:34 +08:00
const elTree = ref<HTMLElement>();
2024-01-09 12:02:47 +08:00
function _onMsgTreeInfo(treeData: Array<TreeData>) {
2024-02-26 09:07:01 +08:00
console.log("treeData");
2024-01-09 15:33:34 +08:00
isShowDebug.value = true;
2024-01-09 12:02:47 +08:00
if (!Array.isArray(treeData)) {
treeData = [treeData];
}
2024-01-09 15:33:34 +08:00
treeData = treeData;
if (_checkSelectedUUID()) {
updateNodeInfo();
nextTick(() => {
if (elTree.value) {
//@ts-ignore
elTree.value.setCurrentKey(selectedUUID);
}
2024-01-09 12:02:47 +08:00
});
}
}
function _initChromeRuntimeConnect() {
const msgFunctionMap: Record<string, Function> = {};
2024-01-09 15:33:34 +08:00
msgFunctionMap[Msg.TreeInfo] = _onMsgTreeInfo;
msgFunctionMap[Msg.Support] = _onMsgSupport;
msgFunctionMap[Msg.NodeInfo] = _onMsgNodeInfo;
msgFunctionMap[Msg.MemoryInfo] = _onMsgMemoryInfo;
msgFunctionMap[Msg.UpdateProperty] = _onMsgUpdateProperty;
msgFunctionMap[Msg.UpdateFrames] = _onMsgUpdateFrames;
msgFunctionMap[Msg.GetObjectItemData] = _onMsgGetObjectItemData;
2024-01-09 12:02:47 +08:00
// 接收来自background.js的消息数据
2024-01-23 22:25:32 +08:00
connectBackground.onBackgroundMessage((data: PluginEvent, sender: any) => {
if (!data) {
return;
}
if (data.target === Page.Devtools) {
console.log("[Devtools]", data);
PluginEvent.finish(data);
const { msg } = data;
if (msg) {
const func = msgFunctionMap[msg];
if (func) {
func(data.data);
} else {
console.warn(`没有${msg}消息的函数`);
2024-01-09 12:02:47 +08:00
}
}
}
2024-01-23 22:25:32 +08:00
});
2024-01-09 12:02:47 +08:00
}
if (chrome && chrome.runtime) {
_initChromeRuntimeConnect();
}
window.addEventListener(
"message",
2024-02-26 09:07:01 +08:00
(event) => {
2024-01-09 12:02:47 +08:00
console.log("on vue:" + JSON.stringify(event));
},
false
);
Bus.on(BusMsg.ShowPlace, (data: EngineData) => {
console.log(data);
_expand(data.engineUUID);
});
Bus.on(BusMsg.RequestObjectData, (data: ObjectData, cb: Function) => {
if (!data.id || requestList.find((el) => el.id === data.id)) {
return;
}
requestList.push({ id: data.id, cb });
2024-02-26 09:07:01 +08:00
connectBackground.sendMsgToContentScript(Msg.GetObjectItemData, data);
2024-01-09 12:02:47 +08:00
});
Bus.on(BusMsg.LogData, (data: string[]) => {
2024-02-26 09:07:01 +08:00
connectBackground.sendMsgToContentScript(Msg.LogData, data);
2024-01-09 12:02:47 +08:00
});
onMounted(() => {
syncSettings();
});
const treeData = ref<Array<TreeData>>([]);
const expandedKeys = ref<Array<string>>([]);
const memory = ref<{
performance: {
jsHeapSizeLimit?: number;
totalJSHeapSize?: number;
usedJSHeapSize?: number;
};
console: Object;
}>({
performance: {},
console: {},
});
// el-tree的渲染key
const defaultProps = ref<{ children: string; label: string }>({
children: "children",
label: "name",
});
const filterText = ref<string>("");
watch(filterText, (val) => {
// TODO: 过滤树
updateFilterText(val);
});
const matchCase = ref<boolean>(false);
function onEnableTreeWatch(watch: boolean, time = 300) {
if (watch) {
2024-01-09 15:33:34 +08:00
_clearTimer();
timerID = setInterval(() => {
onBtnClickUpdateTree();
2024-01-09 12:02:47 +08:00
}, time);
} else {
2024-01-09 15:33:34 +08:00
_clearTimer();
2024-01-09 12:02:47 +08:00
}
}
2024-01-09 15:33:34 +08:00
let timerID: NodeJS.Timer | null = null;
2024-01-09 12:02:47 +08:00
function _clearTimer() {
2024-01-09 15:33:34 +08:00
if (timerID !== null) {
clearInterval(timerID);
timerID = null;
2024-01-09 12:02:47 +08:00
}
}
function syncSettings() {
2024-12-09 19:25:06 +08:00
const { refreshType, refreshTime } = config.value;
switch (refreshType) {
case RefreshType.Auto: {
btnRefresh.visible = false;
onEnableTreeWatch(true, refreshTime);
break;
}
case RefreshType.Manual: {
btnRefresh.visible = true;
onEnableTreeWatch(false);
2024-01-09 12:02:47 +08:00
}
}
}
function updateNodeInfo() {
2024-01-09 15:33:34 +08:00
if (selectedUUID) {
2024-02-26 09:07:01 +08:00
connectBackground.sendMsgToContentScript(Msg.NodeInfo, selectedUUID);
2024-01-09 12:02:47 +08:00
}
}
let selectedUUID: string | null = null;
function updateFilterText(val: any) {
2024-01-09 15:33:34 +08:00
(elTree.value as any)?.filter(val);
2024-01-09 12:02:47 +08:00
}
2024-01-09 15:33:34 +08:00
function onBtnClickUpdateTree() {
2024-02-26 09:07:01 +08:00
connectBackground.sendMsgToContentScript(Msg.TreeInfo, frameID);
2024-01-09 15:33:34 +08:00
}
function onChangeFrame() {
2024-02-26 09:07:01 +08:00
connectBackground.sendMsgToContentScript(Msg.UseFrame, frameID);
2024-01-09 15:33:34 +08:00
}
const elLeft = ref<HTMLDivElement>();
2024-12-09 19:25:06 +08:00
const version = ref(PluginConfig.manifest.version);
2024-01-09 12:02:47 +08:00
return {
2024-12-09 19:25:06 +08:00
version,
2024-01-23 22:25:32 +08:00
buttonGroup,
2024-01-09 15:33:34 +08:00
elTree,
2024-01-09 12:02:47 +08:00
memory,
defaultProps,
filterText,
matchCase,
2024-01-09 15:33:34 +08:00
iframes,
isShowDebug,
2024-01-09 12:02:47 +08:00
expandedKeys,
treeData,
2024-01-09 15:33:34 +08:00
treeItemData,
frameID,
2024-12-09 19:25:06 +08:00
testValidGame(b: boolean) {
isShowDebug.value = !!b;
},
2024-01-09 15:33:34 +08:00
getFramesData(): Option[] {
const frames: FrameInfo[] = toRaw(iframes.value);
const options: Option[] = [];
frames.forEach((frame) => {
options.push({
label: frame.label,
value: frame.value,
});
});
return options;
},
2024-01-09 12:02:47 +08:00
onChangeCase() {
2024-01-09 15:33:34 +08:00
matchCase.value = !matchCase.value;
updateFilterText(filterText);
2024-01-09 12:02:47 +08:00
},
handleNodeClick(data: TreeData) {
2024-01-09 15:33:34 +08:00
selectedUUID = data.uuid;
updateNodeInfo();
2024-01-09 12:02:47 +08:00
},
filterNode(value: any, data: any) {
if (!value) {
return true;
} else {
2024-01-09 15:33:34 +08:00
if (matchCase) {
2024-01-09 12:02:47 +08:00
// 严格匹配大写
return data?.name?.indexOf(value) !== -1;
} else {
2024-01-23 22:25:32 +08:00
return data?.name?.toLowerCase().indexOf(value.toLowerCase()) !== -1;
2024-01-09 12:02:47 +08:00
}
}
},
2024-01-23 22:25:32 +08:00
2024-01-09 12:02:47 +08:00
onBtnClickUpdatePage() {
2024-02-26 09:07:01 +08:00
connectBackground.sendMsgToContentScript(Msg.Support);
2024-01-09 12:02:47 +08:00
},
onMemoryTest() {
2024-02-26 09:07:01 +08:00
connectBackground.sendMsgToContentScript(Msg.MemoryInfo);
2024-01-09 12:02:47 +08:00
},
2024-01-23 22:25:32 +08:00
2024-01-09 15:33:34 +08:00
onChangeFrame,
2024-01-09 12:02:47 +08:00
onNodeExpand(data: TreeData) {
if (data.hasOwnProperty("uuid") && data.uuid) {
2024-01-09 15:33:34 +08:00
expandedKeys.value.push(data.uuid);
2024-01-09 12:02:47 +08:00
}
},
onNodeCollapse(data: TreeData) {
if (data.hasOwnProperty("uuid")) {
2024-01-09 15:33:34 +08:00
let index = expandedKeys.value.findIndex((el) => el === data.uuid);
2024-01-09 12:02:47 +08:00
if (index !== -1) {
2024-01-09 15:33:34 +08:00
expandedKeys.value.splice(index, 1);
2024-01-09 12:02:47 +08:00
}
}
},
};
},
});
</script>
<style scoped lang="less">
#devtools {
display: flex;
flex-direction: column;
width: 100%;
height: 100%;
overflow: hidden;
.head {
display: flex;
flex-direction: row;
align-items: center;
padding: 1px 0;
border-bottom: solid 1px grey;
.label {
margin: 0 3px;
}
2024-01-05 16:45:23 +08:00
}
2024-01-09 12:02:47 +08:00
.no-find {
display: flex;
flex: 1;
flex-direction: row;
align-items: center;
justify-content: center;
span {
margin-right: 20px;
}
}
.find {
display: flex;
flex: 1;
flex-direction: row;
overflow: auto;
.left {
display: flex;
flex-direction: column;
2024-01-23 22:25:32 +08:00
min-width: 200px;
2024-01-09 12:02:47 +08:00
width: 300px;
.tool-btn {
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
}
.matchCase {
width: 30px;
height: 26px;
display: flex;
flex-direction: row;
align-items: center;
justify-content: center;
}
.treeList {
margin-top: 3px;
height: 100%;
border-radius: 4px;
min-height: 20px;
overflow: auto;
width: 100%;
.leaf {
width: 100%;
}
.leaf-show {
color: black;
}
.leaf-hide {
color: #c7bbbb;
text-decoration: line-through;
}
&::-webkit-scrollbar {
width: 6px;
height: 6px;
background: #999;
border-radius: 2px;
}
&::-webkit-scrollbar-thumb {
background-color: #333;
border-radius: 2px;
}
}
}
.right {
flex: 1;
overflow-x: hidden;
overflow-y: overlay;
&::-webkit-scrollbar {
width: 6px;
background: #999;
border-radius: 2px;
height: 6px;
}
&::-webkit-scrollbar-thumb {
background-color: #333;
border-radius: 2px;
}
}
}
}
</style>