forked from zhaocore/egret-inspector-install
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevtoolinit.js
More file actions
executable file
·156 lines (128 loc) · 5.1 KB
/
Copy pathdevtoolinit.js
File metadata and controls
executable file
·156 lines (128 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// The function below is executed in the context of the inspected page.
var page_getProperties = function() {
var data = egret ? egret.MainContext.instance : {};
// Make a shallow copy with a null prototype, so that sidebar does not
// expose prototype.
var props = Object.getOwnPropertyNames(data);
var copy = {
__proto__: null
};
for (var i = 0; i < props.length; ++i)
copy[props[i]] = data[props[i]];
return copy;
};
chrome.devtools.panels.elements.createSidebarPane("Egret Properties", function(sidebar) {
function updateElementProperties() {
sidebar.setExpression("(" + page_getProperties.toString() + ")()");
}
updateElementProperties();
chrome.devtools.panels.elements.onSelectionChanged.addListener(updateElementProperties);
});
//(function () { var t = window.setInterval(function () { var a = egret && (window.clearInterval(t) || egret.devtool.start()); console.log("waiting") }, 100);egret && egret.devtool && (window.clearInterval(t) || egret.devtool.start());})();
chrome.devtools.panels.create("Egret", "icon.png", "ipt/panel/index.html", function(panel) {
var connected = false;
var lastNavigation = 0;
// 提取初始化逻辑为单独函数
function initializeEgretInspector() {
chrome.devtools.inspectedWindow.eval(`
(function() {
// 检查egret对象和devtool是否存在
if (typeof egret === 'undefined') {
return;
}
if (!egret.devtool) {
return;
}
if (!egret.devtool.start) {
return;
}
var attempts = 0;
var maxAttempts = 50; // 5秒超时
var intervalId = setInterval(function() {
attempts++;
if (egret && egret.devtool && egret.devtool.start) {
clearInterval(intervalId);
try {
egret.devtool.start();
// console.log('[Egret调试器] 调试器启动成功');
} catch(e) {
// console.error('[Egret调试器] 启动失败:', e);
}
} else if (attempts >= maxAttempts) {
clearInterval(intervalId);
// console.error('[Egret调试器] 超时: 无法启动egret.devtool.start');
}
}, 100);
})();
`);
}
// 监听页面导航事件,当检测到页面刷新时重置连接状态并主动重新初始化
chrome.devtools.network.onNavigated.addListener(function(url) {
var currentTime = Date.now();
// 防止重复触发,间隔小于1秒的导航事件认为是同一次
if (currentTime - lastNavigation > 1000) {
connected = false;
// console.log('[Egret调试器] 由于页面刷新,重置连接状态');
// 延迟执行初始化,等待页面脚本加载完成
// 使用多个延迟尝试,确保初始化成功
setTimeout(function() {
initializeEgretInspector();
}, 500);
setTimeout(function() {
initializeEgretInspector();
}, 1500);
setTimeout(function() {
initializeEgretInspector();
}, 3000);
}
lastNavigation = currentTime;
});
panel.onShown.addListener(function(w) {
// 移除connected限制,每次面板显示都尝试初始化
// 这样可以确保页面刷新后面板能正常工作
initializeEgretInspector();
connected = true;
// 每次面板显示时都发送这个消息,确保状态同步
backgroundPageConnection.postMessage({
toDevTool: true,
toggleMask: true,
devToolHidden: false
});
connected = true;
});
panel.onHidden.addListener(function(w) {
backgroundPageConnection.postMessage({
toDevTool: true,
toggleMask: true,
devToolHidden: true
});
});
var backgroundPageConnection = chrome.runtime.connect({
name: btoa("for" + String(chrome.devtools.inspectedWindow.tabId))
});
backgroundPageConnection.onMessage.addListener(function(message) {
// Handle responses from the background page, if any
if (message.action === 'injectViaDevTool' && message.code) {
try {
// 使用chrome.devtools.inspectedWindow.eval,这个可以绕过CSP
chrome.devtools.inspectedWindow.eval(message.code, function(result, isException) {
if (isException) {
// console.error('[Egret调试器] DevTool脚本执行失败:', isException);
} else {
}
});
} catch (e) {
// console.error('[Egret调试器] DevTool脚本错误:', e);
}
}
});
backgroundPageConnection.postMessage({
tabId: chrome.devtools.inspectedWindow.tabId
});
panel.onSearch.addListener(function(action, query) {
return false;
});
});