Skip to content

Commit 947e71f

Browse files
committed
logic for enable / disable xdebug session, when popup is disabled
1 parent 2cf9f8d commit 947e71f

File tree

2 files changed

+114
-9
lines changed

2 files changed

+114
-9
lines changed

source/background.js

Lines changed: 111 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,11 @@ chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab)
4646
{
4747
if (chrome.runtime.lastError) {
4848
console.log("Error: ", chrome.runtime.lastError);
49-
} else {
50-
updateIcon(response.status, tabId);
49+
return;
5150
}
51+
52+
// Update the icon
53+
updateIcon(response.status, tabId);
5254
}
5355
);
5456
});
@@ -99,14 +101,14 @@ chrome.commands.onCommand.addListener(function(command)
99101
},
100102
function(response)
101103
{
102-
// If state is debugging (1) toggle to disabled (0), else toggle to debugging
103-
var newState = (1 == response.status) ? 0 : 1;
104+
// Get new status by current status
105+
const newStatus = getNewStatus(response.status);
104106

105107
chrome.tabs.sendMessage(
106108
tabs[0].id,
107109
{
108110
cmd: "setStatus",
109-
status: newState,
111+
status: newStatus,
110112
idekey: ideKey,
111113
traceTrigger: traceTrigger,
112114
profileTrigger: profileTrigger
@@ -123,13 +125,98 @@ chrome.commands.onCommand.addListener(function(command)
123125
}
124126
});
125127

128+
// Will not be called, if popup is disabled, so not needed to wrap this in a if statement
129+
chrome.browserAction.onClicked.addListener((tab) => {
130+
var ideKey = "XDEBUG_ECLIPSE";
131+
var traceTrigger = ideKey;
132+
var profileTrigger = ideKey;
133+
134+
// Check if localStorage is available and get the settings out of it
135+
if (localStorage)
136+
{
137+
if (localStorage["xdebugIdeKey"])
138+
{
139+
ideKey = localStorage["xdebugIdeKey"];
140+
}
141+
142+
if (localStorage["xdebugTraceTrigger"])
143+
{
144+
traceTrigger = localStorage["xdebugTraceTrigger"];
145+
}
146+
147+
if (localStorage["xdebugProfileTrigger"])
148+
{
149+
profileTrigger = localStorage["xdebugProfileTrigger"];
150+
}
151+
}
152+
153+
// Get the current state
154+
chrome.tabs.sendMessage(
155+
tab.id,
156+
{
157+
cmd: "getStatus",
158+
idekey: ideKey,
159+
traceTrigger: traceTrigger,
160+
profileTrigger: profileTrigger
161+
},
162+
function(response)
163+
{
164+
// Get new status by current status
165+
const newStatus = getNewStatus(response.status);
166+
167+
chrome.tabs.sendMessage(
168+
tab.id,
169+
{
170+
cmd: "setStatus",
171+
status: newStatus,
172+
idekey: ideKey,
173+
traceTrigger: traceTrigger,
174+
profileTrigger: profileTrigger
175+
},
176+
function(response)
177+
{
178+
// Update the icon
179+
updateIcon(response.status, tab.id);
180+
}
181+
);
182+
}
183+
);
184+
});
185+
186+
/**
187+
* Get new status by current status.
188+
*
189+
* @param {number} status - Current status from sendMessage() cmd: 'getStatus'.
190+
*
191+
* @returns {number}
192+
*/
193+
function getNewStatus(status) {
194+
// Reset status, when trace or profile is selected and popup is disabled
195+
if ((localStorage.xdebugDisablePopup === '1')
196+
&& ((status === 2) || (status === 3))
197+
) {
198+
return 0;
199+
}
200+
201+
// If state is debugging (1) toggle to disabled (0), else toggle to debugging
202+
return (status === 1) ? 0 : 1;
203+
}
204+
126205
function updateIcon(status, tabId)
127206
{
128-
// Figure the correct title/image with the given state
129-
var title = "Debugging, profiling & tracing disabled",
130-
image = "images/bug-gray.png";
207+
// Reset status, when trace or profile is selected and popup is disabled
208+
if ((localStorage.xdebugDisablePopup === '1')
209+
&& ((status === 2) || (status === 3))
210+
) {
211+
status = 0;
212+
}
131213

132-
if (status == 1)
214+
// Figure the correct title / image by the given state
215+
let image = "images/bug-gray.png";
216+
let title = (localStorage.xdebugDisablePopup === '1')
217+
? 'Debugging disabled' : 'Debugging, profiling & tracing disabled';
218+
219+
if (status == 1)
133220
{
134221
title = "Debugging enabled";
135222
image = "images/bug.png";
@@ -158,6 +245,10 @@ function updateIcon(status, tabId)
158245
});
159246
}
160247

248+
/**
249+
* @deprecated
250+
* @todo to remove silver
251+
*/
161252
function isValueInArray(arr, val)
162253
{
163254
for (i = 0; i < arr.length; i++)
@@ -171,3 +262,14 @@ function isValueInArray(arr, val)
171262

172263
return false;
173264
}
265+
266+
// Disable / Enable Popup by localStorage
267+
if (localStorage.xdebugDisablePopup === '1') {
268+
chrome.browserAction.setPopup({
269+
popup: '',
270+
});
271+
} else {
272+
chrome.browserAction.setPopup({
273+
popup: 'popup.html',
274+
});
275+
}

source/options.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,9 @@
9595

9696
// Persist
9797
save_options();
98+
99+
// We need to reload the extension, because to hide the popup
100+
chrome.extension.getBackgroundPage().window.location.reload(true);
98101
}
99102

100103
})();

0 commit comments

Comments
 (0)