-
Notifications
You must be signed in to change notification settings - Fork 2
/
background.js
96 lines (88 loc) · 2.49 KB
/
background.js
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
/**
* Listen for when tabs are activated, check if XDebug icon should be active.
*/
chrome.tabs.onActivated.addListener(function(tabId, changeInfo, tab) {
chrome.tabs.query({
active: true,
currentWindow: true
}, function(tabs) {
checkXDebug(tabs[0]);
});
});
/**
* Toggle XDebug cookie and icon state on click.
*/
chrome.browserAction.onClicked.addListener(function(tab) {
toggleXDebug(tab);
});
/**
* Wrapper to handle retrieval of XDebug cookie.
*/
function getCookie(domain, name, callback) {
chrome.cookies.get({'url': domain, 'name': name}, function(cookie) {
if (callback && typeof cookie !== 'undefined' && cookie !== null) {
callback(cookie.value);
} else {
callback(false);
}
});
}
/**
* Wrapper to handle setting of XDebug cookie.
*/
function setCookie(domain, name, value, callback) {
chrome.cookies.set({'url': domain, 'name': name, 'value': value}, function(cookie) {
if (callback && typeof cookie !== 'undefined' && cookie !== null) {
callback(cookie.value);
} else {
callback(false);
}
});
}
/**
* Get the base domain from a provided URL.
*/
function getCookieDomain(url) {
url = new URL(url);
return url.protocol + '//' + url.hostname;
}
/**
* Check if XDebug Cookie is active/inactive based on provided tab object.
*/
function checkXDebug(tab) {
var domain = getCookieDomain(tab.url);
getCookie(domain, 'xdebug', function(value) {
if (value === 'on') {
chrome.browserAction.setIcon({path:"icon-active.png"});
chrome.browserAction.setTitle({title:"Pilothouse XDebug - ACTIVE"});
} else {
chrome.browserAction.setIcon({path:"icon-inactive.png"});
chrome.browserAction.setTitle({title:"Pilothouse XDebug - INACTIVE"});
}
});
}
/**
* Handle switching XDebug to Active/Inactive.
*/
function toggleXDebug(tab) {
var domain = getCookieDomain(tab.url);
getCookie(domain, 'xdebug', function(value) {
if (value !== 'on') {
// Toggle the XDebug cookie off if it's currently on, for this domain.
setCookie(domain, 'xdebug', 'on', function(value) {
if (value === 'on') {
chrome.browserAction.setIcon({path:"icon-active.png"});
chrome.browserAction.setTitle({title:"Pilothouse XDebug - ACTIVE"});
}
});
} else {
// Toggle the XDebug cookie on if it's currently off, for this domain.
setCookie(domain, 'xdebug', '', function(value) {
if (value !== 'on') {
chrome.browserAction.setIcon({path:"icon-inactive.png"});
chrome.browserAction.setTitle({title:"Pilothouse XDebug - INACTIVE"});
}
});
}
});
}