Skip to content

Commit

Permalink
logic for enable / disable xdebug session, when popup is disabled
Browse files Browse the repository at this point in the history
  • Loading branch information
silverqx committed Jul 27, 2017
1 parent 2cf9f8d commit 947e71f
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 9 deletions.
120 changes: 111 additions & 9 deletions source/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,11 @@ chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab)
{
if (chrome.runtime.lastError) {
console.log("Error: ", chrome.runtime.lastError);
} else {
updateIcon(response.status, tabId);
return;
}

// Update the icon
updateIcon(response.status, tabId);
}
);
});
Expand Down Expand Up @@ -99,14 +101,14 @@ chrome.commands.onCommand.addListener(function(command)
},
function(response)
{
// If state is debugging (1) toggle to disabled (0), else toggle to debugging
var newState = (1 == response.status) ? 0 : 1;
// Get new status by current status
const newStatus = getNewStatus(response.status);

chrome.tabs.sendMessage(
tabs[0].id,
{
cmd: "setStatus",
status: newState,
status: newStatus,
idekey: ideKey,
traceTrigger: traceTrigger,
profileTrigger: profileTrigger
Expand All @@ -123,13 +125,98 @@ chrome.commands.onCommand.addListener(function(command)
}
});

// Will not be called, if popup is disabled, so not needed to wrap this in a if statement
chrome.browserAction.onClicked.addListener((tab) => {
var ideKey = "XDEBUG_ECLIPSE";
var traceTrigger = ideKey;
var profileTrigger = ideKey;

// Check if localStorage is available and get the settings out of it
if (localStorage)
{
if (localStorage["xdebugIdeKey"])
{
ideKey = localStorage["xdebugIdeKey"];
}

if (localStorage["xdebugTraceTrigger"])
{
traceTrigger = localStorage["xdebugTraceTrigger"];
}

if (localStorage["xdebugProfileTrigger"])
{
profileTrigger = localStorage["xdebugProfileTrigger"];
}
}

// Get the current state
chrome.tabs.sendMessage(
tab.id,
{
cmd: "getStatus",
idekey: ideKey,
traceTrigger: traceTrigger,
profileTrigger: profileTrigger
},
function(response)
{
// Get new status by current status
const newStatus = getNewStatus(response.status);

chrome.tabs.sendMessage(
tab.id,
{
cmd: "setStatus",
status: newStatus,
idekey: ideKey,
traceTrigger: traceTrigger,
profileTrigger: profileTrigger
},
function(response)
{
// Update the icon
updateIcon(response.status, tab.id);
}
);
}
);
});

/**
* Get new status by current status.
*
* @param {number} status - Current status from sendMessage() cmd: 'getStatus'.
*
* @returns {number}
*/
function getNewStatus(status) {
// Reset status, when trace or profile is selected and popup is disabled
if ((localStorage.xdebugDisablePopup === '1')
&& ((status === 2) || (status === 3))
) {
return 0;
}

// If state is debugging (1) toggle to disabled (0), else toggle to debugging
return (status === 1) ? 0 : 1;
}

function updateIcon(status, tabId)
{
// Figure the correct title/image with the given state
var title = "Debugging, profiling & tracing disabled",
image = "images/bug-gray.png";
// Reset status, when trace or profile is selected and popup is disabled
if ((localStorage.xdebugDisablePopup === '1')
&& ((status === 2) || (status === 3))
) {
status = 0;
}

if (status == 1)
// Figure the correct title / image by the given state
let image = "images/bug-gray.png";
let title = (localStorage.xdebugDisablePopup === '1')
? 'Debugging disabled' : 'Debugging, profiling & tracing disabled';

if (status == 1)
{
title = "Debugging enabled";
image = "images/bug.png";
Expand Down Expand Up @@ -158,6 +245,10 @@ function updateIcon(status, tabId)
});
}

/**
* @deprecated
* @todo to remove silver
*/
function isValueInArray(arr, val)
{
for (i = 0; i < arr.length; i++)
Expand All @@ -171,3 +262,14 @@ function isValueInArray(arr, val)

return false;
}

// Disable / Enable Popup by localStorage
if (localStorage.xdebugDisablePopup === '1') {
chrome.browserAction.setPopup({
popup: '',
});
} else {
chrome.browserAction.setPopup({
popup: 'popup.html',
});
}
3 changes: 3 additions & 0 deletions source/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,9 @@

// Persist
save_options();

// We need to reload the extension, because to hide the popup
chrome.extension.getBackgroundPage().window.location.reload(true);
}

})();

0 comments on commit 947e71f

Please sign in to comment.