cookie_helpers.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. function buildUrl(domain, path, searchUrl) {
  2. // Keep same protocol as searchUrl
  3. // This fixes a bug when we want to unset 'secure' property in an https domain
  4. var secure = searchUrl.indexOf("https://") === 0;
  5. if (domain.substr(0, 1) === '.')
  6. domain = domain.substring(1);
  7. return "http" + ((secure) ? "s" : "") + "://" + domain + path;
  8. }
  9. function deleteAll(cookieList, searchUrl) {
  10. for (var i = 0; i < cookieList.length; i++) {
  11. var curr = cookieList[i];
  12. var url = buildUrl(curr.domain, curr.path, searchUrl);
  13. deleteCookie(url, curr.name, curr.storeId);
  14. }
  15. }
  16. function deleteCookie(url, name, store, callback) {
  17. chrome.cookies.remove({
  18. 'url': url,
  19. 'name': name,
  20. 'storeId': store
  21. }, function (details) {
  22. if (typeof callback === "undefined")
  23. return;
  24. if (details === "null" || details === undefined || details === "undefined") {
  25. callback(false);
  26. } else {
  27. callback(true);
  28. }
  29. })
  30. }
  31. function Filter() {
  32. var filter = {};
  33. this.setUrl = function (url) {
  34. filter.url = url;
  35. };
  36. this.setDomain = function (domain) {
  37. filter.domain = domain;
  38. };
  39. this.setName = function (name) {
  40. filter.name = name;
  41. };
  42. this.setSecure = function (secure) {
  43. filter.secure = secure;
  44. };
  45. this.setSession = function (session) {
  46. filter.session = session;
  47. };
  48. this.getFilter = function (session) {
  49. return filter;
  50. };
  51. }
  52. function cookieForCreationFromFullCookie(fullCookie) {
  53. var newCookie = {};
  54. //If no real url is available use: "https://" : "http://" + domain + path
  55. newCookie.url = "http" + ((fullCookie.secure) ? "s" : "") + "://" + fullCookie.domain + fullCookie.path;
  56. newCookie.name = fullCookie.name;
  57. newCookie.value = fullCookie.value;
  58. if (!fullCookie.hostOnly)
  59. newCookie.domain = fullCookie.domain;
  60. newCookie.path = fullCookie.path;
  61. newCookie.secure = fullCookie.secure;
  62. newCookie.httpOnly = fullCookie.httpOnly;
  63. if (!fullCookie.session)
  64. newCookie.expirationDate = fullCookie.expirationDate;
  65. newCookie.storeId = fullCookie.storeId;
  66. return newCookie;
  67. }
  68. function compareCookies(b, a) {
  69. try {
  70. if (b.name !== a.name)
  71. return false;
  72. if (b.value !== a.value)
  73. return false;
  74. if (b.path !== a.path)
  75. return false;
  76. if (b.secure !== a.secure)
  77. return false;
  78. if (b.httpOnly !== a.httpOnly)
  79. return false;
  80. var aHostOnly = !!(a.hostOnly || a.domain === undefined);
  81. var bHostOnly = !!(b.hostOnly || b.domain === undefined);
  82. if (aHostOnly !== bHostOnly)
  83. return false;
  84. if (!aHostOnly && b.domain !== a.domain)
  85. return false;
  86. var aSession = !!(a.session || a.expirationDate === undefined);
  87. var bSession = !!(b.session || b.expirationDate === undefined);
  88. if (aSession !== bSession)
  89. return false;
  90. if (aSession === false && b.expirationDate !== a.expirationDate)
  91. return false;
  92. } catch (e) {
  93. console.error(e.message);
  94. return false;
  95. }
  96. return true;
  97. }
  98. var cookiesToString = {
  99. "get": function (cookies, url) {
  100. if (cookiesToString[preferences.copyCookiesType] !== undefined && cookies.length > 0)
  101. return cookiesToString[preferences.copyCookiesType](cookies, url);
  102. else
  103. return undefined;
  104. },
  105. "netscape": function (cookies, url) {
  106. var string = "";
  107. string += "# Netscape HTTP Cookie File\n";
  108. string += "# http://curl.haxx.se/rfc/cookie_spec.html\n";
  109. string += "# This file was generated by EditThisCookie\n";
  110. if (url !== undefined)
  111. string += "# URL: " + url + "\n";
  112. for (var i = 0; i < cookies.length; i++) {
  113. cookie = cookies[i];
  114. string += cookie.domain + "\t" +
  115. (!cookie.hostOnly).toString().toUpperCase() + "\t" +
  116. cookie.path + "\t" +
  117. cookie.secure.toString().toUpperCase() + "\t" +
  118. (cookie.expirationDate ? Math.round(cookie.expirationDate) : "0") + "\t" +
  119. cookie.name + "\t" +
  120. cookie.value + ((i === cookies.length - 1) ? "" : "\n");
  121. }
  122. return string;
  123. },
  124. "json": function (cookies, url) {
  125. var string = "";
  126. string += "[\n";
  127. for (var i = 0; i < cookies.length; i++) {
  128. cookie = cookies[i];
  129. cookie.id = i + 1;
  130. string += JSON.stringify(cookie, null, 4);
  131. if (i < cookies.length - 1)
  132. string += ",\n";
  133. }
  134. string += "\n]";
  135. return string;
  136. },
  137. "semicolonPairs": function (cookies, url) {
  138. var string = "";
  139. string += "// Semicolon separated Cookie File\n";
  140. string += "// This file was generated by EditThisCookie\n";
  141. string += "// Details: http://www.ietf.org/rfc/rfc2109.txt\n"
  142. string += "// Example: http://www.tutorialspoint.com/javascript/javascript_cookies.htm\n"
  143. if (url !== undefined)
  144. string += "// URL: " + url + "\n";
  145. for (var i = 0; i < cookies.length; i++) {
  146. cookie = cookies[i];
  147. string += cookie.name + "=" + cookie.value + ";";
  148. }
  149. return string;
  150. },
  151. "lpw": function (cookies, url) {
  152. var string = "";
  153. string += "// Semicolon separated Cookie File\n";
  154. string += "// This file was generated by EditThisCookie\n";
  155. string += "// Details: http://www.cookiecentral.com/faq/#3.5\n"
  156. string += "// Example: http://www.tutorialspoint.com/javascript/javascript_cookies.htm\n"
  157. if (url !== undefined)
  158. string += "// URL: " + url + "\n";
  159. for (var i = 0; i < cookies.length; i++) {
  160. cookie = cookies[i];
  161. string += 'Set-Cookie3: ' + cookie.name + '=' + cookie.value + '; path="/"; domain=' + cookie.domain + '; path_spec; expires="' + (cookie.expirationDate ? cookie.expirationDate : "0") + '"; version=0\n';
  162. }
  163. return string;
  164. }
  165. };