git-svn-id: svn://euphorik.ch/pompage@45 02bbb61a-6d21-0410-aba0-cb053bdfd66a
[pompage.git] / doc / webdeveloper / cookies.js
1 // Adds a cookie
2 function webdeveloper_addCookie()
3 {
4 window.openDialog("chrome://webdeveloper/content/dialogs/cookie.xul", "webdeveloper-cookie-dialog", "centerscreen,chrome,modal", "add");
5 }
6
7 // Clears all session cookies
8 function webdeveloper_clearSessionCookies()
9 {
10 var stringBundle = document.getElementById("webdeveloper-string-bundle");
11
12 // If the clearing is confirmed
13 if(webdeveloper_clearConfirmation(stringBundle.getString("webdeveloper_clearSessionCookiesConfirmation")))
14 {
15 var cookie = null;
16 var cookieManager = Components.classes["@mozilla.org/cookiemanager;1"].getService(Components.interfaces.nsICookieManager);
17 var cookies = cookieManager.enumerator;
18 var removed = 0;
19
20 // Loop through the cookies
21 while(cookies.hasMoreElements())
22 {
23 cookie = cookies.getNext();
24
25 // If this is a cookie with no expiration
26 if(cookie instanceof Components.interfaces.nsICookie && cookie.expires == "0")
27 {
28 cookieManager.remove(cookie.host, cookie.name, cookie.path, false);
29 removed++;
30 }
31 }
32
33 // If the hide informational dialogs preference is not set
34 if(!webdeveloper_getBooleanPreference("webdeveloper.informational.dialogs.hide", true))
35 {
36 var title = stringBundle.getString("webdeveloper_clearSessionCookies");
37
38 // If one session cookie was removed
39 if(removed == 1)
40 {
41 webdeveloper_informationalDialog(title, stringBundle.getString("webdeveloper_clearSessionCookiesSingleResult"));
42 }
43 else
44 {
45 webdeveloper_informationalDialog(title, stringBundle.getFormattedString("webdeveloper_clearSessionCookiesMultipleResult", [removed]));
46 }
47 }
48 }
49 }
50
51 // Deletes a cookie
52 function webdeveloper_deleteCookie(event)
53 {
54 var eventTarget = event.target;
55
56 // If there is an event target and the click was not a right click
57 if(eventTarget && event.button != 2)
58 {
59 var stringBundle = document.getElementById("webdeveloper-string-bundle");
60
61 // If the deletion is confirmed
62 if(webdeveloper_deleteConfirmation(stringBundle.getString("webdeveloper_deleteCookieConfirmation")))
63 {
64 Components.classes["@mozilla.org/cookiemanager;1"].getService(Components.interfaces.nsICookieManager).remove(eventTarget.getAttribute("cookie-host"), eventTarget.getAttribute("cookie-name"), eventTarget.getAttribute("cookie-path"), false);
65
66 // If the hide informational dialogs preference is not set
67 if(!webdeveloper_getBooleanPreference("webdeveloper.informational.dialogs.hide", true))
68 {
69 webdeveloper_informationalDialog(stringBundle.getString("webdeveloper_deleteCookie"), stringBundle.getString("webdeveloper_deleteCookieResult"));
70 }
71 }
72
73 event.preventDefault();
74 }
75 }
76
77 // Called when a delete cookie link is moused out
78 function webdeveloper_deleteCookieMouseOut(event)
79 {
80 // If there is an event target
81 if(event.target)
82 {
83 getBrowser().contentWindow.status = "";
84
85 event.preventDefault();
86 }
87 }
88
89 // Called when a delete cookie link is moused over
90 function webdeveloper_deleteCookieMouseOver(event)
91 {
92 // If there is an event target
93 if(event.target)
94 {
95 getBrowser().contentWindow.status = document.getElementById("webdeveloper-string-bundle").getString("webdeveloper_deleteCookie");
96
97 event.preventDefault();
98 }
99 }
100
101 // Deletes all the cookies for the current domain
102 function webdeveloper_deleteDomainCookies()
103 {
104 var cookies = new Array();
105 var cookiesLength = null;
106 var documentList = webdeveloper_getDocuments(webdeveloper_getContentWindow());
107 var documentLength = documentList.length;
108 var message = null;
109 var pageDocument = null;
110 var stringBundle = document.getElementById("webdeveloper-string-bundle");
111
112 // Loop through the documents
113 for(var i = 0; i < documentLength; i++)
114 {
115 pageDocument = documentList[i];
116 cookies = cookies.concat(webdeveloper_getCookies(pageDocument.location.hostname, "/", false));
117 }
118
119 cookiesLength = cookies.length;
120
121 // If one cookie was found
122 if(cookiesLength == 1)
123 {
124 message = stringBundle.getString("webdeveloper_deleteDomainCookiesSingleConfirmation");
125 }
126 else
127 {
128 message = stringBundle.getFormattedString("webdeveloper_deleteDomainCookiesMultipleConfirmation", [cookiesLength]);
129 }
130
131 // If the deletion is confirmed
132 if(cookiesLength == 0 || webdeveloper_deleteConfirmation(message))
133 {
134 var cookie = null;
135 var cookieManager = Components.classes["@mozilla.org/cookiemanager;1"].getService(Components.interfaces.nsICookieManager);
136
137 // Loop through all the cookies
138 for(i = 0 ; i < cookiesLength; i++)
139 {
140 cookie = cookies[i];
141
142 cookieManager.remove(cookie.host, cookie.name, cookie.path, false);
143 }
144
145 // If the hide informational dialogs preference is not set
146 if(!webdeveloper_getBooleanPreference("webdeveloper.informational.dialogs.hide", true))
147 {
148 var title = stringBundle.getString("webdeveloper_deleteDomainCookies");
149
150 // If one cookie was found
151 if(cookiesLength == 1)
152 {
153 webdeveloper_informationalDialog(title, stringBundle.getString("webdeveloper_deleteDomainCookiesSingleResult"));
154 }
155 else
156 {
157 webdeveloper_informationalDialog(title, stringBundle.getFormattedString("webdeveloper_deleteDomainCookiesMultipleResult", [cookiesLength]));
158 }
159 }
160 }
161 }
162
163 // Deletes all the cookies for the current path
164 function webdeveloper_deletePathCookies()
165 {
166 var cookies = new Array();
167 var cookiesLength = null;
168 var documentList = webdeveloper_getDocuments(webdeveloper_getContentWindow());
169 var documentLength = documentList.length;
170 var message = null;
171 var pageDocument = null;
172 var stringBundle = document.getElementById("webdeveloper-string-bundle");
173
174 // Loop through the documents
175 for(var i = 0; i < documentLength; i++)
176 {
177 pageDocument = documentList[i];
178 cookies = cookies.concat(webdeveloper_getCookies(pageDocument.location.hostname, pageDocument.location.pathname, false));
179 }
180
181 cookiesLength = cookies.length;
182
183 // If one cookie was found
184 if(cookiesLength == 1)
185 {
186 message = stringBundle.getString("webdeveloper_deletePathCookiesSingleConfirmation");
187 }
188 else
189 {
190 message = stringBundle.getFormattedString("webdeveloper_deletePathCookiesMultipleConfirmation", [cookiesLength]);
191 }
192
193 // If the deletion is confirmed
194 if(cookiesLength == 0 || webdeveloper_deleteConfirmation(message))
195 {
196 var cookie = null;
197 var cookieManager = Components.classes["@mozilla.org/cookiemanager;1"].getService(Components.interfaces.nsICookieManager);
198
199 // Loop through all the cookies
200 for(i = 0 ; i < cookiesLength; i++)
201 {
202 cookie = cookies[i];
203
204 cookieManager.remove(cookie.host, cookie.name, cookie.path, false);
205 }
206
207 // If the hide informational dialogs preference is not set
208 if(!webdeveloper_getBooleanPreference("webdeveloper.informational.dialogs.hide", true))
209 {
210 var title = stringBundle.getString("webdeveloper_deletePathCookies");
211
212 // If one cookie was found
213 if(cookiesLength == 1)
214 {
215 webdeveloper_informationalDialog(title, stringBundle.getString("webdeveloper_deletePathCookiesSingleResult"));
216 }
217 else
218 {
219 webdeveloper_informationalDialog(title, stringBundle.getFormattedString("webdeveloper_deletePathCookiesMultipleResult", [cookiesLength]));
220 }
221 }
222 }
223 }
224
225 // Edits a cookie
226 function webdeveloper_editCookie(event)
227 {
228 var eventTarget = event.target;
229
230 // If there is an event target and the click was not a right click
231 if(eventTarget && event.button != 2)
232 {
233 window.openDialog("chrome://webdeveloper/content/dialogs/cookie.xul", "webdeveloper-cookie-dialog", "centerscreen,chrome,modal", "edit", eventTarget.getAttribute("cookie-name"), eventTarget.getAttribute("cookie-value"), eventTarget.getAttribute("cookie-host"), eventTarget.getAttribute("cookie-path"), eventTarget.getAttribute("cookie-expires"), eventTarget.getAttribute("cookie-secure"));
234
235 event.preventDefault();
236 }
237 }
238
239 // Called when an edit cookie link is moused out
240 function webdeveloper_editCookieMouseOut(event)
241 {
242 // If there is an event target
243 if(event.target)
244 {
245 getBrowser().contentWindow.status = "";
246
247 event.preventDefault();
248 }
249 }
250
251 // Called when an edit cookie link is moused over
252 function webdeveloper_editCookieMouseOver(event)
253 {
254 // If there is an event target
255 if(event.target)
256 {
257 getBrowser().contentWindow.status = document.getElementById("webdeveloper-string-bundle").getString("webdeveloper_editCookie");
258
259 event.preventDefault();
260 }
261 }
262
263 // Toggles cookies
264 function webdeveloper_toggleCookies(element)
265 {
266 var cookieBehavior = 0;
267
268 // If disabling cookies
269 if(element.getAttribute("checked"))
270 {
271 cookieBehavior = 2;
272 }
273
274 webdeveloper_setIntegerPreference("network.cookie.cookieBehavior", cookieBehavior);
275 }
276
277 // Toggles external site cookies
278 function webdeveloper_toggleExternalSiteCookies(element)
279 {
280 var cookieBehavior = 0;
281
282 // If disabling external cookies
283 if(element.getAttribute("checked"))
284 {
285 cookieBehavior = 1;
286 }
287
288 webdeveloper_setIntegerPreference("network.cookie.cookieBehavior", cookieBehavior);
289 }
290
291 // Updates the disable cookies menu
292 function webdeveloper_updateDisableCookiesMenu(suffix)
293 {
294 var disableExternalSiteCookiesChecked = false;
295 var disableExternalSiteCookiesMenu = document.getElementById("webdeveloper-disable-external-site-cookies-" + suffix);
296 var disableCookiesChecked = false;
297 var disableCookiesPreferenceValue = webdeveloper_getIntegerPreference("network.cookie.cookieBehavior", true);
298
299 // If the cookie preference value is set to 2
300 if(disableCookiesPreferenceValue == 2)
301 {
302 disableCookiesChecked = true;
303 }
304 else if(disableCookiesPreferenceValue == 1)
305 {
306 disableExternalSiteCookiesChecked = true;
307 }
308
309 webdeveloper_configureElement(document.getElementById("webdeveloper-disable-all-cookies-" + suffix), "checked", disableCookiesChecked);
310 webdeveloper_configureElement(disableExternalSiteCookiesMenu, "checked", disableExternalSiteCookiesChecked);
311 webdeveloper_configureElement(disableExternalSiteCookiesMenu, "disabled", disableCookiesChecked);
312 }
313
314 // Displays all the cookies for the page
315 function webdeveloper_viewCookieInformation()
316 {
317 var cellDataElement = null;
318 var cellHeaderElement = null;
319 var cookie = null;
320 var cookieExpires = null;
321 var cookieHost = null;
322 var cookieLength = null;
323 var cookieName = null;
324 var cookiePath = null;
325 var cookies = null;
326 var cookieSecure = null;
327 var cookieValue = null;
328 var divElement = null;
329 var documentList = webdeveloper_getDocuments(webdeveloper_getContentWindow());
330 var documentLength = documentList.length;
331 var hostName = null;
332 var linkElement = null;
333 var listElement = null;
334 var listItemElement = null;
335 var location = null;
336 var oldTab = getBrowser().selectedTab;
337 var oldURL = getBrowser().currentURI.spec;
338 var generatedDocument = webdeveloper_generateDocument("");
339 var bodyElement = webdeveloper_getDocumentBodyElement(generatedDocument);
340 var headElement = webdeveloper_getDocumentHeadElement(generatedDocument);
341 var headerElement = generatedDocument.createElement("h1");
342 var pageDocument = null;
343 var preElement = null;
344 var scriptElement = generatedDocument.createElement("script");
345 var spanElement = null;
346 var stringBundle = document.getElementById("webdeveloper-string-bundle");
347 var tableElement = null;
348 var tableRowElement = null;
349 var title = stringBundle.getFormattedString("webdeveloper_viewCookieInformationTitle", [oldURL]);
350
351 generatedDocument.title = title;
352
353 webdeveloper_addGeneratedStyles(generatedDocument);
354
355 headerElement.appendChild(generatedDocument.createTextNode(title));
356 bodyElement.appendChild(headerElement);
357
358 webdeveloper_addGeneratedTools(generatedDocument);
359
360 // Loop through the documents
361 for(var i = 0; i < documentLength; i++)
362 {
363 hostName = null;
364 pageDocument = documentList[i];
365 location = pageDocument.location;
366
367 // Try to get the host name
368 try
369 {
370 hostName = location.hostname;
371 }
372 catch(exception)
373 {
374 // Do nothing
375 }
376
377 // If the host name is set
378 if(hostName)
379 {
380 divElement = generatedDocument.createElement("div");
381 headerElement = generatedDocument.createElement("h2");
382 linkElement = generatedDocument.createElement("a");
383 cookies = webdeveloper_getCookies(hostName, location.pathname, true);
384 cookieLength = cookies.length;
385 spanElement = generatedDocument.createElement("span");
386
387 linkElement.setAttribute("href", pageDocument.documentURI);
388 linkElement.appendChild(generatedDocument.createTextNode(pageDocument.documentURI));
389 headerElement.appendChild(linkElement);
390 bodyElement.appendChild(headerElement);
391
392 headerElement = generatedDocument.createElement("h3");
393
394 spanElement.setAttribute("class", "expanded pivot");
395 headerElement.appendChild(spanElement);
396
397 // If there is one cookie
398 if(cookieLength == 1)
399 {
400 headerElement.appendChild(generatedDocument.createTextNode(cookieLength + " " + stringBundle.getString("webdeveloper_cookie").toLowerCase()));
401 }
402 else
403 {
404 headerElement.appendChild(generatedDocument.createTextNode(cookieLength + " " + stringBundle.getString("webdeveloper_cookies").toLowerCase()));
405 }
406
407 bodyElement.appendChild(headerElement);
408
409 // Loop through the cookies
410 for(var j = 0; j < cookieLength; j++)
411 {
412 cookie = cookies[j];
413 cookieHost = cookie.host;
414 cookieName = cookie.name;
415 cookiePath = cookie.path;
416 cookieSecure = cookie.isSecure;
417 cookieValue = cookie.value;
418 tableElement = generatedDocument.createElement("table");
419
420 // Cookie name
421 cellDataElement = generatedDocument.createElement("td");
422 cellHeaderElement = generatedDocument.createElement("th");
423 tableRowElement = generatedDocument.createElement("tr");
424
425 cellHeaderElement.appendChild(generatedDocument.createTextNode(stringBundle.getString("webdeveloper_name")));
426 tableRowElement.appendChild(cellHeaderElement);
427 cellDataElement.appendChild(generatedDocument.createTextNode(cookieName));
428 tableRowElement.appendChild(cellDataElement);
429 tableElement.appendChild(tableRowElement);
430
431 // Cookie value
432 cellDataElement = generatedDocument.createElement("td");
433 cellHeaderElement = generatedDocument.createElement("th");
434 tableRowElement = generatedDocument.createElement("tr");
435
436 cellHeaderElement.appendChild(generatedDocument.createTextNode(stringBundle.getString("webdeveloper_value")));
437 tableRowElement.appendChild(cellHeaderElement);
438 cellDataElement.setAttribute("class", "shaded");
439 cellDataElement.appendChild(generatedDocument.createTextNode(cookieValue));
440 tableRowElement.appendChild(cellDataElement);
441 tableElement.appendChild(tableRowElement);
442
443 // Cookie host
444 cellDataElement = generatedDocument.createElement("td");
445 cellHeaderElement = generatedDocument.createElement("th");
446 tableRowElement = generatedDocument.createElement("tr");
447
448 cellHeaderElement.appendChild(generatedDocument.createTextNode(stringBundle.getString("webdeveloper_host")));
449 tableRowElement.appendChild(cellHeaderElement);
450 cellDataElement.appendChild(generatedDocument.createTextNode(cookieHost));
451 tableRowElement.appendChild(cellDataElement);
452 tableElement.appendChild(tableRowElement);
453
454 // Cookie path
455 cellDataElement = generatedDocument.createElement("td");
456 cellHeaderElement = generatedDocument.createElement("th");
457 tableRowElement = generatedDocument.createElement("tr");
458
459 cellHeaderElement.appendChild(generatedDocument.createTextNode(stringBundle.getString("webdeveloper_path")));
460 tableRowElement.appendChild(cellHeaderElement);
461 cellDataElement.setAttribute("class", "shaded");
462 cellDataElement.appendChild(generatedDocument.createTextNode(cookiePath));
463 tableRowElement.appendChild(cellDataElement);
464 tableElement.appendChild(tableRowElement);
465
466 // Cookie secure
467 cellDataElement = generatedDocument.createElement("td");
468 cellHeaderElement = generatedDocument.createElement("th");
469 tableRowElement = generatedDocument.createElement("tr");
470
471 cellHeaderElement.appendChild(generatedDocument.createTextNode(stringBundle.getString("webdeveloper_secure")));
472 tableRowElement.appendChild(cellHeaderElement);
473
474 // If the cookie is secure
475 if(cookieSecure)
476 {
477 cellDataElement.appendChild(generatedDocument.createTextNode(stringBundle.getString("webdeveloper_yes")));
478 }
479 else
480 {
481 cellDataElement.appendChild(generatedDocument.createTextNode(stringBundle.getString("webdeveloper_no")));
482 }
483
484 tableRowElement.appendChild(cellDataElement);
485 tableElement.appendChild(tableRowElement);
486
487 // If the cookie has an expiration date
488 if(cookie.expires)
489 {
490 cookieExpires = new Date(cookie.expires * 1000).toUTCString();
491 }
492 else
493 {
494 cookieExpires = stringBundle.getString("webdeveloper_viewCookieInformationSession");
495 }
496
497 // Cookie expires
498 cellDataElement = generatedDocument.createElement("td");
499 cellHeaderElement = generatedDocument.createElement("th");
500 tableRowElement = generatedDocument.createElement("tr");
501
502 cellHeaderElement.appendChild(generatedDocument.createTextNode(stringBundle.getString("webdeveloper_expires")));
503 tableRowElement.appendChild(cellHeaderElement);
504 cellDataElement.setAttribute("class", "shaded");
505 cellDataElement.appendChild(generatedDocument.createTextNode(cookieExpires));
506 tableRowElement.appendChild(cellDataElement);
507 tableElement.appendChild(tableRowElement);
508
509 divElement.appendChild(tableElement);
510
511 // Edit link
512 linkElement = generatedDocument.createElement("a");
513 listElement = generatedDocument.createElement("ul");
514 listItemElement = generatedDocument.createElement("li");
515
516 linkElement.addEventListener("blur", webdeveloper_editCookieMouseOut, false);
517 linkElement.addEventListener("click", webdeveloper_editCookie, false);
518 linkElement.addEventListener("focus", webdeveloper_editCookieMouseOver, false);
519 linkElement.addEventListener("mouseout", webdeveloper_editCookieMouseOut, false);
520 linkElement.addEventListener("mouseover", webdeveloper_editCookieMouseOver, false);
521 linkElement.setAttribute("cookie-expires", cookieExpires);
522 linkElement.setAttribute("cookie-host", cookieHost);
523 linkElement.setAttribute("cookie-name", cookieName);
524 linkElement.setAttribute("cookie-path", cookiePath);
525 linkElement.setAttribute("cookie-secure", cookieSecure);
526 linkElement.setAttribute("cookie-value", cookieValue);
527 linkElement.setAttribute("href", "http://example.com/");
528
529 linkElement.appendChild(generatedDocument.createTextNode(stringBundle.getString("webdeveloper_editCookie")));
530 listItemElement.appendChild(linkElement);
531 listItemElement.setAttribute("class", "edit");
532 listElement.appendChild(listItemElement);
533
534 // Delete link
535 linkElement = generatedDocument.createElement("a");
536 listItemElement = generatedDocument.createElement("li");
537
538 linkElement.addEventListener("blur", webdeveloper_deleteCookieMouseOut, false);
539 linkElement.addEventListener("click", webdeveloper_deleteCookie, false);
540 linkElement.addEventListener("focus", webdeveloper_deleteCookieMouseOver, false);
541 linkElement.addEventListener("mouseout", webdeveloper_deleteCookieMouseOut, false);
542 linkElement.addEventListener("mouseover", webdeveloper_deleteCookieMouseOver, false);
543 linkElement.setAttribute("cookie-host", cookieHost);
544 linkElement.setAttribute("cookie-name", cookieName);
545 linkElement.setAttribute("cookie-path", cookiePath);
546 linkElement.setAttribute("href", "http://example.com/");
547
548 linkElement.appendChild(generatedDocument.createTextNode(stringBundle.getString("webdeveloper_deleteCookie")));
549 listItemElement.appendChild(linkElement);
550 listItemElement.setAttribute("class", "delete");
551 listElement.appendChild(listItemElement);
552 listElement.setAttribute("class", "commands");
553 divElement.appendChild(listElement);
554 divElement.appendChild(generatedDocument.createElement("hr"));
555 }
556
557 divElement.setAttribute("class", "output");
558 bodyElement.appendChild(divElement);
559 }
560 }
561
562 scriptElement.setAttribute("defer", "defer");
563 scriptElement.setAttribute("src", "chrome://webdeveloper/content/common/xpath.js");
564 scriptElement.setAttribute("type", "text/javascript");
565 headElement.appendChild(scriptElement);
566
567 scriptElement = generatedDocument.createElement("script");
568
569 scriptElement.setAttribute("defer", "defer");
570 scriptElement.setAttribute("src", "chrome://webdeveloper/content/generated/output_pivot.js");
571 scriptElement.setAttribute("type", "text/javascript");
572 headElement.appendChild(scriptElement);
573
574 // If the open tabs in background preference is set to true
575 if(webdeveloper_getBooleanPreference("webdeveloper.open.tabs.background", true))
576 {
577 getBrowser().selectedTab = oldTab;
578 }
579 }