panel.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. var tabURL;
  2. var cookieList = [];
  3. var backgroundPageConnection = chrome.runtime.connect({
  4. name: "devtools-page"
  5. });
  6. backgroundPageConnection.onMessage.addListener(function (message) {
  7. if (message.action === "getall") {
  8. createTable(message);
  9. } else if (message.action === "refresh") {
  10. location.reload(true);
  11. }
  12. });
  13. jQuery(document).ready(function () {
  14. ++data.nPanelClicked;
  15. start();
  16. });
  17. function start() {
  18. var arguments = getUrlVars();
  19. if (arguments.url !== undefined) {//TESTING PURPOSES
  20. createList("https://google.com");
  21. return;
  22. }
  23. var tabId = chrome.devtools.inspectedWindow.tabId;
  24. backgroundPageConnection.postMessage({
  25. action: "getall",
  26. tabId: tabId
  27. });
  28. }
  29. function createList(url) {
  30. tabURL = url;
  31. chrome.cookies.getAll({
  32. url: tabURL
  33. }, function (cks) {
  34. createTable({
  35. url: tabURL,
  36. cks: cks
  37. });
  38. });
  39. }
  40. function createTable(message) {
  41. tabURL = message.url;
  42. cookieList = message.cks;
  43. $tableBody = $("#cookieTable > tbody");
  44. $tableBody.empty();
  45. for (var i = 0; i < cookieList.length; i++) {
  46. currentC = cookieList[i];
  47. var domainDisabled = (currentC.hostOnly) ? "disabled" : "";
  48. var expirationDisabled = (currentC.session) ? "disabled" : "";
  49. $row = $("<tr/>");
  50. $row.append($("<td/>").addClass("hiddenColumn").text(i));
  51. $row.append($("<td/>").addClass("editable").text(currentC.name));
  52. $row.append($("<td/>").addClass("editable").text(currentC.value));
  53. $row.append($("<td/>").addClass("editable domain " + domainDisabled).text(currentC.domain));
  54. $row.append($("<td/>").addClass("editable").text(currentC.path));
  55. if (currentC.session) {
  56. expDate = new Date();
  57. expDate.setFullYear(expDate.getFullYear() + 1);
  58. } else {
  59. expDate = new Date(currentC.expirationDate * 1000.0);
  60. }
  61. $row.append($("<td/>").addClass("editable expiration " + expirationDisabled).text(expDate));
  62. $row.append($("<td/>").append($("<input/>").attr("type", "checkbox").addClass("sessionCB").prop("checked", currentC.session)));
  63. $row.append($("<td/>").append($("<input/>").attr("type", "checkbox").addClass("hostOnlyCB").prop("checked", currentC.hostOnly)));
  64. $row.append($("<td/>").append($("<input/>").attr("type", "checkbox").prop("checked", currentC.secure)));
  65. $row.append($("<td/>").append($("<input/>").attr("type", "checkbox").prop("checked", currentC.httpOnly)));
  66. var $sameSite = $("<select/>");
  67. $sameSite.append($("<option/>").attr("value", "no_restriction").attr("i18n", "SameSite_None"));
  68. $sameSite.append($("<option/>").attr("value", "lax").attr("i18n", "SameSite_Lax"));
  69. $sameSite.append($("<option/>").attr("value", "strict").attr("i18n", "SameSite_Strict"));
  70. $sameSite.val(currentC.sameSite);
  71. $row.append($("<td/>").append($sameSite));
  72. $row.append($("<td/>").addClass("hiddenColumn").text(currentC.name));
  73. $row.append($("<td/>").addClass("hiddenColumn").text(currentC.storeId));
  74. $tableBody.append($row);
  75. }
  76. localizePage();
  77. setEvents();
  78. }
  79. function setEvents() {
  80. $(".sessionCB").click(function () {
  81. var checked = $(this).prop("checked");
  82. var $domain = $(".expiration", $(this).parent().parent()).first();
  83. if (!!checked)
  84. $domain.addClass("disabled");
  85. else
  86. $domain.removeClass("disabled");
  87. });
  88. $(".hostOnlyCB").click(function () {
  89. var checked = $(this).prop("checked");
  90. var $domain = $(".domain", $(this).parent().parent()).first();
  91. if (!!checked)
  92. $domain.addClass("disabled");
  93. else
  94. $domain.removeClass("disabled");
  95. });
  96. $(":checkbox").click(function () {
  97. updateCookie.call($(this).parent().first());
  98. });
  99. $("#cookieTable").tablesorter({
  100. // sort on the first column and third column in ascending order
  101. sortList: [[1, 0]],
  102. widgets: ["resizable"],
  103. widgetOptions: {
  104. resizable: false
  105. }
  106. });
  107. $('.editable').editable(function (newValue, settings) {
  108. updateCookie.call(this);
  109. return newValue;
  110. }, {
  111. type: 'textarea',
  112. onblur: "submit"
  113. });
  114. }
  115. function updateCookie() {
  116. $row = $(this).parent();
  117. $cols = $row.children();
  118. var isForm = function (element) {
  119. return $("textarea", element).length;
  120. };
  121. var isCheckbox = function (element) {
  122. return $("input", element).length;
  123. };
  124. var isSelect = function (element) {
  125. return $("select", element).length;
  126. }
  127. var getValue = function (column, container) {
  128. element = container[column];
  129. if (isCheckbox(element)) {
  130. return $(element).children(0).prop("checked");
  131. } else if (isForm(element)) {
  132. return $("textarea", element).first().val();
  133. }
  134. else if (isSelect(element)) {
  135. return $("select", element).val();
  136. } else {
  137. return $(element).text();
  138. }
  139. };
  140. var id = getValue(0, $cols);
  141. var name = getValue(1, $cols);
  142. var value = getValue(2, $cols);
  143. var domain = getValue(3, $cols);
  144. var path = getValue(4, $cols);
  145. var expiration = getValue(5, $cols);
  146. var session = getValue(6, $cols);
  147. var hostOnly = getValue(7, $cols);
  148. var secure = getValue(8, $cols);
  149. var httpOnly = getValue(9, $cols);
  150. var sameSite = getValue(10, $cols);
  151. var origName = getValue(11, $cols);
  152. var storeId = getValue(12, $cols);
  153. newCookie = {};
  154. newCookie.url = tabURL;
  155. newCookie.name = name.replace(";", "").replace(",", "");
  156. value = value.replace(";", "");
  157. newCookie.value = value;
  158. newCookie.path = path;
  159. newCookie.storeId = storeId;
  160. if (!hostOnly)
  161. newCookie.domain = domain;
  162. if (!session) {
  163. var expirationDate = new Date(expiration).getTime() / 1000.0;
  164. console.log(expirationDate);
  165. newCookie.expirationDate = expirationDate;
  166. }
  167. newCookie.secure = secure;
  168. newCookie.httpOnly = httpOnly;
  169. newCookie.sameSite = sameSite;
  170. backgroundPageConnection.postMessage({
  171. action: "submitCookie",
  172. cookie: newCookie,
  173. origName: origName
  174. });
  175. }