home *** CD-ROM | disk | FTP | other *** search
/ GameStar 2005 October / Gamestar_77_2005-10_dvd.iso / Programy / nsb-install-8-0.exe / chrome / browser.jar / content / browser / cookieviewer / CookieViewer.js < prev    next >
Encoding:
JavaScript  |  2005-07-29  |  7.0 KB  |  230 lines

  1.  
  2. // interface variables
  3. var cookiemanager = Components.classes["@mozilla.org/cookiemanager;1"].getService();
  4.     cookiemanager = cookiemanager.QueryInterface(Components.interfaces.nsICookieManager);
  5. var gDateService = null;
  6.  
  7. // cookies list
  8. var cookies              = [];
  9. var deletedCookies       = [];
  10.  
  11. var cookieBundle;
  12. var cookiesTree;
  13. const nsICookie = Components.interfaces.nsICookie;
  14.  
  15. function Startup() {
  16.  
  17.   // intialize gDateService
  18.   if (!gDateService) {
  19.     const nsScriptableDateFormat_CONTRACTID = "@mozilla.org/intl/scriptabledateformat;1";
  20.     const nsIScriptableDateFormat = Components.interfaces.nsIScriptableDateFormat;
  21.     gDateService = Components.classes[nsScriptableDateFormat_CONTRACTID]
  22.       .getService(nsIScriptableDateFormat);
  23.   }
  24.  
  25.   //XXXBlake
  26.   // I removed the observer stuff, so yes, there are edge cases where
  27.   // if you're loading a page when you open prefs/cookie manager,
  28.   // the cookie manager won't display the new cookie added.
  29.   // I don't think it's a big deal (considering how hard it would be to fix)
  30. }
  31.  
  32. function onOK() {
  33.   window.opener.top.wsm.savePageData(window.location.href, window);
  34.   window.opener.top.hPrefWindow.registerOKCallbackFunc(window.opener.cookieViewerOnPrefsOK);
  35. }
  36.  
  37. function GetFields()
  38. {
  39.   var dataObject = {};
  40.   dataObject.deletedCookies = deletedCookies;
  41.   dataObject.cookies = cookies;
  42.   dataObject.cookieBool = document.getElementById("checkbox").checked;
  43.   return dataObject;
  44. }
  45.  
  46. function SetFields(dataObject)
  47. {
  48.   if ('cookies' in dataObject)
  49.     cookies = dataObject.cookies;
  50.  
  51.   if ('deletedCookies' in dataObject)
  52.     deletedCookies = dataObject.deletedCookies;
  53.   
  54.   if ('cookieBool' in dataObject)
  55.     document.getElementById("checkbox").checked = dataObject.cookieBool;
  56.  
  57.   loadCookies();
  58. }
  59.  
  60.  
  61. /*** =================== COOKIES CODE =================== ***/
  62.  
  63. var cookiesTreeView = {
  64.   rowCount : 0,
  65.   setTree : function(tree){},
  66.   getImageSrc : function(row,column) {},
  67.   getProgressMode : function(row,column) {},
  68.   getCellValue : function(row,column) {},
  69.   getCellText : function(row,column){
  70.     var rv="";
  71.     if (column=="domainCol") {
  72.       rv = cookies[row].rawHost;
  73.     } else if (column=="nameCol") {
  74.       rv = cookies[row].name;
  75.     }
  76.     return rv;
  77.   },
  78.   isSeparator : function(index) {return false;},
  79.   isSorted: function() { return false; },
  80.   isContainer : function(index) {return false;},
  81.   cycleHeader : function(aColId, aElt) {},
  82.   getRowProperties : function(row,column,prop){},
  83.   getColumnProperties : function(column,columnElement,prop){},
  84.   getCellProperties : function(row,prop){}
  85.  };
  86.  
  87. function Cookie(number,name,value,isDomain,host,rawHost,path,isSecure,expires) {
  88.   this.number = number;
  89.   this.name = name;
  90.   this.value = value;
  91.   this.isDomain = isDomain;
  92.   this.host = host;
  93.   this.rawHost = rawHost;
  94.   this.path = path;
  95.   this.isSecure = isSecure;
  96.   this.expires = expires;
  97. }
  98.  
  99. function loadCookies() {
  100.   if (!cookieBundle)
  101.     cookieBundle = document.getElementById("cookieBundle");
  102.  
  103.   if (!cookiesTree)
  104.     cookiesTree = document.getElementById("cookiesTree");
  105.  
  106.   var dataObject = window.opener.top.hPrefWindow.wsm.dataManager.pageData[window.location.href].userData;
  107.   if (!('cookies' in dataObject)) {
  108.     // load cookies into a table
  109.     var enumerator = cookiemanager.enumerator;
  110.     var count = 0;
  111.     while (enumerator.hasMoreElements()) {
  112.       var nextCookie = enumerator.getNext();
  113.       if (!nextCookie) break;
  114.       nextCookie = nextCookie.QueryInterface(Components.interfaces.nsICookie);
  115.       var host = nextCookie.host;
  116.       cookies[count] =
  117.         new Cookie(count++, nextCookie.name, nextCookie.value, nextCookie.isDomain, host,
  118.                    (host.charAt(0)==".") ? host.substring(1,host.length) : host,
  119.                    nextCookie.path, nextCookie.isSecure, nextCookie.expires);
  120.     }
  121.   }
  122.  
  123.   cookiesTreeView.rowCount = cookies.length;
  124.   cookiesTree.treeBoxObject.view = cookiesTreeView;
  125.  
  126.  // sort by host column
  127.   CookieColumnSort('rawHost');
  128.  
  129.   // disable "remove all cookies" button if there are no cookies
  130.   document.getElementById("removeAllCookies").disabled = cookies.length == 0;
  131. }
  132.  
  133. function GetExpiresString(expires) {
  134.   if (expires) {
  135.     var date = new Date(1000*expires);
  136.     return gDateService.FormatDateTime("", gDateService.dateFormatLong,
  137.                                        gDateService.timeFormatSeconds, date.getFullYear(),
  138.                                        date.getMonth()+1, date.getDate(), date.getHours(),
  139.                                        date.getMinutes(), date.getSeconds());
  140.   }
  141.   return cookieBundle.getString("AtEndOfSession");
  142. }
  143.  
  144. function CookieSelected() {
  145.   var selections = GetTreeSelections(cookiesTree);
  146.   if (selections.length) {
  147.     document.getElementById("removeCookie").removeAttribute("disabled");
  148.   } else {
  149.     ClearCookieProperties();
  150.     return true;
  151.   }
  152.     
  153.   var idx = selections[0];
  154.   if (idx >= cookies.length) {
  155.     // Something got out of synch.  See bug 119812 for details
  156.     dump("Tree and viewer state are out of sync! " +
  157.          "Help us figure out the problem in bug 119812");
  158.     return;
  159.   }
  160.  
  161.   var props = [
  162.     {id: "ifl_name", value: cookies[idx].name},
  163.     {id: "ifl_value", value: cookies[idx].value}, 
  164.     {id: "ifl_isDomain",
  165.      value: cookies[idx].isDomain ?
  166.             cookieBundle.getString("domainColon") : cookieBundle.getString("hostColon")},
  167.     {id: "ifl_host", value: cookies[idx].host},
  168.     {id: "ifl_path", value: cookies[idx].path},
  169.     {id: "ifl_isSecure",
  170.      value: cookies[idx].isSecure ?
  171.             cookieBundle.getString("forSecureOnly") : 
  172.             cookieBundle.getString("forAnyConnection")},
  173.     {id: "ifl_expires", value: GetExpiresString(cookies[idx].expires)},
  174.   ];
  175.  
  176.   var value;
  177.   var field;
  178.   for (var i = 0; i < props.length; i++)
  179.   {
  180.     field = document.getElementById(props[i].id);
  181.     if ((selections.length > 1) && (props[i].id != "ifl_isDomain")) {
  182.       value = ""; // clear field if multiple selections
  183.     } else {
  184.       value = props[i].value;
  185.     }
  186.     field.value = value;
  187.   }
  188.   return true;
  189. }
  190.  
  191. function ClearCookieProperties() {
  192.   var properties = 
  193.     ["ifl_name","ifl_value","ifl_host","ifl_path","ifl_isSecure","ifl_expires"];
  194.   for (var prop=0; prop<properties.length; prop++) {
  195.     document.getElementById(properties[prop]).value = "";
  196.   }
  197. }
  198.  
  199. function DeleteCookie() {
  200.   DeleteSelectedItemFromTree(cookiesTree, cookiesTreeView,
  201.                                  cookies, deletedCookies,
  202.                                  "removeCookie", "removeAllCookies");
  203.   if (!cookies.length) {
  204.     ClearCookieProperties();
  205.   }
  206. }
  207.  
  208. function DeleteAllCookies() {
  209.   ClearCookieProperties();
  210.   DeleteAllFromTree(cookiesTree, cookiesTreeView,
  211.                         cookies, deletedCookies,
  212.                         "removeCookie", "removeAllCookies");
  213. }
  214.  
  215. function HandleCookieKeyPress(e) {
  216.   if (e.keyCode == 46) {
  217.     DeleteCookie();
  218.   }
  219. }
  220.  
  221. var lastCookieSortColumn = "";
  222. var lastCookieSortAscending = false;
  223.  
  224. function CookieColumnSort(column) {
  225.   lastCookieSortAscending =
  226.     SortTree(cookiesTree, cookiesTreeView, cookies,
  227.                  column, lastCookieSortColumn, lastCookieSortAscending);
  228.   lastCookieSortColumn = column;
  229. }
  230.