git-svn-id: svn://euphorik.ch/pompage@45 02bbb61a-6d21-0410-aba0-cb053bdfd66a
[pompage.git] / doc / webdeveloper / common / cookie.js
1 // Get the cookies
2 function webdeveloper_getCookies(host, path, sort)
3 {
4 var cookies = new Array();
5
6 // If the host is set
7 if(host)
8 {
9 var cookie = null;
10 var cookieEnumeration = Components.classes["@mozilla.org/cookiemanager;1"].getService(Components.interfaces.nsICookieManager).enumerator;
11 var cookieHost = null;
12 var cookiePath = null;
13
14 // Loop through the cookies
15 while(cookieEnumeration.hasMoreElements())
16 {
17 cookie = cookieEnumeration.getNext().QueryInterface(Components.interfaces.nsICookie);
18
19 cookieHost = cookie.host;
20 cookiePath = cookie.path;
21
22 // If there is a host and path for this cookie
23 if(cookieHost && cookiePath)
24 {
25 // If the cookie host starts with '.'
26 if(cookieHost.charAt(0) == ".")
27 {
28 cookieHost = cookieHost.substring(1);
29 }
30
31 // If the host and cookie host and path and cookie path match
32 if((host == cookieHost || host.indexOf("." + cookieHost) != -1) && (path == cookiePath || path.indexOf(cookiePath) == 0))
33 {
34 cookies.push(cookie);
35 }
36 }
37 }
38
39 // If sorting cookies
40 if(sort)
41 {
42 cookies.sort(webdeveloper_sortCookies);
43 }
44 }
45
46 return cookies;
47 }
48
49 // Sorts two cookies
50 function webdeveloper_sortCookies(cookieOne, cookieTwo)
51 {
52 // If cookie one and cookie two are set
53 if(cookieOne && cookieTwo)
54 {
55 var cookieOneHost = cookieOne.host;
56 var cookieOneName = cookieOne.name;
57 var cookieTwoHost = cookieTwo.host;
58 var cookieTwoName = cookieTwo.name;
59
60 // If the cookies are equal
61 if(cookieOneHost == cookieTwoHost && cookieOneName == cookieTwoName)
62 {
63 return 0;
64 }
65 else if(cookieOneHost < cookieTwoHost || (cookieOneHost == cookieTwoHost && cookieOneName < cookieTwoName))
66 {
67 return -1;
68 }
69 }
70
71 return 1;
72 }