forked from mac-cain13/xdebug-helper-for-chrome
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
270 lines (238 loc) · 8.05 KB
/
background.js
File metadata and controls
270 lines (238 loc) · 8.05 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// Listen for tab updates
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
// We only react on a complete load of a http(s) page,
// only then we're sure the content.js is loaded.
if (changeInfo.status !== "complete" || tab.url.indexOf("http") !== 0) {
return;
}
// Prep some variables
let ideKey = "XDEBUG_ECLIPSE";
let traceTrigger = ideKey;
let profileTrigger = ideKey;
// Get settings from storage
chrome.storage.local.get(
["xdebugIdeKey", "xdebugTraceTrigger", "xdebugProfileTrigger", "xdebugDisablePopup"],
function(data) {
if (data.xdebugIdeKey) {
ideKey = data.xdebugIdeKey;
}
if (data.xdebugTraceTrigger) {
traceTrigger = data.xdebugTraceTrigger;
}
if (data.xdebugProfileTrigger) {
profileTrigger = data.xdebugProfileTrigger;
}
// Request the current status and update the icon accordingly
chrome.tabs.sendMessage(
tabId,
{
cmd: "getStatus",
idekey: ideKey,
traceTrigger: traceTrigger,
profileTrigger: profileTrigger
},
function(response) {
if (chrome.runtime.lastError) {
console.log("Error: ", chrome.runtime.lastError);
return;
}
// Update the icon
updateIcon(response.status, tabId, data.xdebugDisablePopup === '1');
}
);
}
);
});
// Handle keyboard commands
chrome.commands.onCommand.addListener(function(command) {
if ('toggle_debug_action' == command) {
let ideKey = "XDEBUG_ECLIPSE";
let traceTrigger = ideKey;
let profileTrigger = ideKey;
let disablePopup = false;
// Get settings from storage
chrome.storage.local.get(
["xdebugIdeKey", "xdebugTraceTrigger", "xdebugProfileTrigger", "xdebugDisablePopup"],
function(data) {
if (data.xdebugIdeKey) {
ideKey = data.xdebugIdeKey;
}
if (data.xdebugTraceTrigger) {
traceTrigger = data.xdebugTraceTrigger;
}
if (data.xdebugProfileTrigger) {
profileTrigger = data.xdebugProfileTrigger;
}
disablePopup = data.xdebugDisablePopup === '1';
// Fetch the active tab
chrome.tabs.query({ active: true, currentWindow: true }, function(tabs) {
// Do nothing when there is no active tab
if (tabs.length == 0) {
return;
}
// Get the current state
chrome.tabs.sendMessage(
tabs[0].id,
{
cmd: "getStatus",
idekey: ideKey,
traceTrigger: traceTrigger,
profileTrigger: profileTrigger
},
function(response) {
if (chrome.runtime.lastError) {
console.log("Error: ", chrome.runtime.lastError);
return;
}
// Get new status by current status
const newStatus = getNewStatus(response.status, disablePopup);
chrome.tabs.sendMessage(
tabs[0].id,
{
cmd: "setStatus",
status: newStatus,
idekey: ideKey,
traceTrigger: traceTrigger,
profileTrigger: profileTrigger
},
function(response) {
if (chrome.runtime.lastError) {
console.log("Error: ", chrome.runtime.lastError);
return;
}
// Update the icon
updateIcon(response.status, tabs[0].id, disablePopup);
}
);
}
);
});
}
);
}
});
// Handle action button clicks
chrome.action.onClicked.addListener((tab) => {
chrome.storage.local.get(
["xdebugIdeKey", "xdebugTraceTrigger", "xdebugProfileTrigger", "xdebugDisablePopup"],
function(data) {
let ideKey = data.xdebugIdeKey || "XDEBUG_ECLIPSE";
let traceTrigger = data.xdebugTraceTrigger || ideKey;
let profileTrigger = data.xdebugProfileTrigger || ideKey;
let disablePopup = data.xdebugDisablePopup === '1';
// Get the current state
chrome.tabs.sendMessage(
tab.id,
{
cmd: "getStatus",
idekey: ideKey,
traceTrigger: traceTrigger,
profileTrigger: profileTrigger
},
function(response) {
if (chrome.runtime.lastError) {
console.log("Error: ", chrome.runtime.lastError);
return;
}
// Get new status by current status
const newStatus = getNewStatus(response.status, disablePopup);
chrome.tabs.sendMessage(
tab.id,
{
cmd: "setStatus",
status: newStatus,
idekey: ideKey,
traceTrigger: traceTrigger,
profileTrigger: profileTrigger
},
function(response) {
if (chrome.runtime.lastError) {
console.log("Error: ", chrome.runtime.lastError);
return;
}
// Update the icon
updateIcon(response.status, tab.id, disablePopup);
}
);
}
);
}
);
});
/**
* Get new status by current status.
*
* @param {number} status - Current status from sendMessage() cmd: 'getStatus'.
* @param {boolean} disablePopup - Whether popup is disabled
*
* @returns {number}
*/
function getNewStatus(status, disablePopup) {
// Reset status, when trace or profile is selected and popup is disabled
if (disablePopup && ((status === 2) || (status === 3))) {
return 0;
}
// If state is debugging (1) toggle to disabled (0), else toggle to debugging
return (status === 1) ? 0 : 1;
}
/**
* Update the extension icon based on status
*
* @param {number} status - The status code
* @param {number} tabId - The ID of the tab
* @param {boolean} disablePopup - Whether popup is disabled
*/
function updateIcon(status, tabId, disablePopup) {
// Reset status, when trace or profile is selected and popup is disabled
if (disablePopup && ((status === 2) || (status === 3))) {
status = 0;
}
// Figure the correct title / image by the given state
let image = "images/bug-gray.png";
let title = disablePopup ? 'Debugging disabled' : 'Debugging, profiling & tracing disabled';
if (status == 1) {
title = "Debugging enabled";
image = "images/bug.png";
} else if (status == 2) {
title = "Profiling enabled";
image = "images/clock.png";
} else if (status == 3) {
title = "Tracing enabled";
image = "images/script.png";
}
// Update title
chrome.action.setTitle({
tabId: tabId,
title: title
});
// Update image
chrome.action.setIcon({
tabId: tabId,
path: image
});
}
// Set up the popup based on preferences
chrome.storage.local.get(["xdebugDisablePopup"], function(data) {
if (data.xdebugDisablePopup === '1') {
chrome.action.setPopup({
popup: '',
});
} else {
chrome.action.setPopup({
popup: 'popup.html',
});
}
});
// Migration from localStorage to chrome.storage.local (one-time)
if (typeof localStorage !== 'undefined') {
const keysToMigrate = ["xdebugIdeKey", "xdebugTraceTrigger", "xdebugProfileTrigger", "xdebugDisablePopup"];
let dataToMigrate = {};
keysToMigrate.forEach(key => {
if (localStorage[key]) {
dataToMigrate[key] = localStorage[key];
}
});
if (Object.keys(dataToMigrate).length > 0) {
chrome.storage.local.set(dataToMigrate);
}
}