-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
236 lines (146 loc) · 7.02 KB
/
index.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
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
var self = require('sdk/self');
var button = require("sdk/ui/button/action").ActionButton;
var tabs = require('sdk/tabs');
var sp = require("sdk/simple-prefs");
var imgXDebugerEnabled = self.data.url("debug_enabled.png");
var imgXDebugerDisabled = self.data.url("debug_disabled.png");
var imgProfilerEnabled = self.data.url("profiler_enabled.png");
var imgProfilerDisabled = self.data.url("profiler_disabled.png");
//console.log("test");
// listener to
tabs.on('activate', function(tab) {
recheckCookieValue(tab);
});
// listener to
tabs.on('ready', function(tab) {
recheckCookieValue(tab);
});
function recheckCookieValue(tab) {
tab.attach({
contentScript: 'self.postMessage(document.cookie);',
onMessage: function (cookieStr) {
cookieValue = getCookie(sp.prefs.DebugCookieName,cookieStr);
var enabled = (cookieValue == undefined) ? true : false;
if (enabled!=true) {
xdebug_xdebuger.icon = imgXDebugerEnabled;
} else {
xdebug_xdebuger.icon = imgXDebugerDisabled;
}
cookieValue = getCookie(sp.prefs.ProfilerCookieName,cookieStr);
enabled = (cookieValue == undefined) ? true : false;
if (enabled!=true) {
xdebug_profiler.icon = imgProfilerEnabled;
} else {
xdebug_profiler.icon = imgProfilerDisabled;
}
}
});
}
// set a cookie in the tab (should be the active tab)
function setCookie(tab,cookieName,cookieValue='',minutes=null) {
var m = parseInt(minutes);
var expStr = '';
if (isNaN(m)) { m = -1; }
if (m>0 || minutes==null){
var exp=new Date();
exp.setTime(exp.getTime()+(m*60*1000));
expStr = '; expires='+exp.toGMTString();
}
var cookieStr = escape(cookieName) +'='+escape(cookieValue)+expStr+'; path=/;';
var worker = tabs.activeTab.attach({
contentScriptFile: self.data.url("my-outsourced.js")
});
worker.port.emit("setCookie", cookieStr);
}
// find cookie value in the cookie string (document.cookie)
function getCookie(c_name,cookieStr='')
{
//check if empty
if (cookieStr==null || !(cookieStr.length>0))
return ;
//console.log('cookieStr:'+cookieStr);
var i,x,y,ARRcookies=cookieStr.split(";");
for (i=0;i<ARRcookies.length;i++)
{
x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
x=x.replace(/^\s+|\s+$/g,"");
if (x==c_name)
{
return unescape(y);
}
}
}
exports.main = function() {
// Widget documentation: https://addons.mozilla.org/en-US/developers/docs/sdk/latest/packages/addon-kit/widget.html
xdebug_xdebuger = new button({
// Mandatory string used to identify your widget in order to
// save its location when the user moves it in the browser.
// This string has to be unique and must not be changed over time.
id: "Debug",
// A required string description of the widget used for
// accessibility, title bars, and error reporting.
label: "Toggle xdebug cookie (red->active)",
// needed to make the button visible in the customisation dialog >= ff 29
icon: imgXDebugerDisabled,
// Add a function to trigger when the Widget is clicked.
onClick: function(event) {
// set cookie in active tab
tabs.activeTab.attach({
contentScript: 'self.postMessage(document.cookie);',
onMessage: function (cookieStr) {
// get cookie values from simple prefs
var minutes=sp.prefs.DebugCookieTimeout; //120;
var cookieName=sp.prefs.DebugCookieName; //"XDEBUG_SESSION";
var cookieValue=sp.prefs.DebugCookieValue; //"netbeans-xdebug";
// check if cookie is already set
var cookieVal = getCookie(cookieName,cookieStr);
var enabled = (cookieVal == undefined) ? true : false;
// toggle the cookie
if (enabled==true) {
setCookie(tabs.activeTab,cookieName,cookieValue,minutes);
xdebug_xdebuger.icon = imgXDebugerEnabled;
} else {
setCookie(tabs.activeTab,cookieName);
xdebug_xdebuger.icon = imgXDebugerDisabled;
}
}
});
},
});
xdebug_profiler = new button({
// Mandatory string used to identify your widget in order to
// save its location when the user moves it in the browser.
// This string has to be unique and must not be changed over time.
id: "Profil",
// A required string description of the widget used for
// accessibility, title bars, and error reporting.
label: "Toggle Xdebug Profiler Cookie (red->active)",
// needed to make the button visible in the customisation dialog >= ff 29
icon: imgProfilerDisabled,
// Add a function to trigger when the Widget is clicked.
onClick: function(event) {
// set cookie in active tab
tabs.activeTab.attach({
contentScript: 'self.postMessage(document.cookie);',
onMessage: function (cookieStr) {
// get cookie values from simple prefs
var minutes=sp.prefs.ProfilerCookieTimeout; //120;
var cookieName=sp.prefs.ProfilerCookieName; //"XDEBUG_SESSION";
var cookieValue=sp.prefs.ProfilerCookieValue; //"netbeans-xdebug";
// check if cookie is already set
var cookieVal = getCookie(cookieName,cookieStr);
var enabled = (cookieVal == undefined) ? true : false;
// toggle the cookie
if (enabled==true) {
setCookie(tabs.activeTab,cookieName,cookieValue,minutes);
xdebug_profiler.icon = imgProfilerEnabled;
} else {
setCookie(tabs.activeTab,cookieName);
xdebug_profiler.icon = imgProfilerDisabled;
}
}
});
}
});
};