popup.js 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. function start() {
  2. messenger.request(["sessions", ""]);
  3. var clearButton = document.getElementById("btn_clear");
  4. clearButton.onclick = function() {
  5. messenger.request(["clearSessions", ""]);
  6. };
  7. var confirmSaveButton = document.getElementById("btn_confirmSave");
  8. var inputSession = document.getElementById("input_session");
  9. confirmSaveButton.onclick = function() {
  10. messenger.request(["createSession", inputSession.value]);
  11. };
  12. }
  13. function updateCurrentSession(currentSession) {
  14. var currentSessionName = document.getElementById("current_session_name");
  15. currentSessionName.textContent = currentSession;
  16. }
  17. function setData(sessionName, windows, timeStamp) {
  18. //Session HTML Element
  19. var session = document.createElement("p");
  20. session.setAttribute("class", "session_cell ellipsis");
  21. session.appendChild(document.createTextNode(sessionName + ' (' + windows.length + ')'));
  22. //Session timestamp
  23. var date = document.createElement("p");
  24. date.setAttribute("class", "date");
  25. date.appendChild(document.createTextNode(timeStamp));
  26. var sessionContents = document.createElement('div');
  27. sessionContents.setAttribute('class', 'sessionContents');
  28. windows.forEach(function(window) {
  29. var sessionWindow = document.createElement('div');
  30. sessionWindow.setAttribute("class", "sessionWindow");
  31. var sessionWindowId = document.createElement('p');
  32. sessionWindowId.setAttribute('class', 'sessionWindowId');
  33. sessionWindowId.appendChild(document.createTextNode(window.id));
  34. sessionWindow.appendChild(sessionWindowId);
  35. var sessionTabList = document.createElement('ul');
  36. window.tabs.forEach(function(tab) {
  37. var sessionTab = document.createElement('li');
  38. sessionTab.setAttribute('class', 'sessionTab');
  39. sessionTab.appendChild(document.createTextNode(tab.title));
  40. sessionTabList.appendChild(sessionTab);
  41. });
  42. sessionWindow.appendChild(sessionTabList);
  43. sessionContents.appendChild(sessionWindow);
  44. });
  45. //Remove button icon and HTML Element
  46. var iconRemove = document.createElement("i");
  47. iconRemove.setAttribute("class", "fa fa fa-times");
  48. var removeP = document.createElement("p");
  49. removeP.setAttribute("class", "remove_session");
  50. removeP.appendChild(iconRemove);
  51. removeP.style.visibility = "hidden";
  52. var openNewP = document.createElement("p");
  53. openNewP.setAttribute("class", "new_window_session");
  54. openNewP.style.visibility = "hidden";
  55. //Adding session HTML elements to the table
  56. var table = document.getElementById("sessions_table");
  57. var row = table.insertRow(0);
  58. var cellSession = row.insertCell(0);
  59. cellSession.setAttribute("title", sessionName);
  60. cellSession.appendChild(session);
  61. cellSession.appendChild(date);
  62. var cellRemove = row.insertCell(1);
  63. cellRemove.setAttribute("class", "remove_cell");
  64. cellRemove.appendChild(removeP);
  65. var cellContent = table.insertRow(1);
  66. cellContent.setAttribute("class", "sessionContents");
  67. cellContent.appendChild(sessionContents);
  68. cellSession.addEventListener("click", function() {
  69. messenger.request(["attach", sessionName]);
  70. });
  71. cellRemove.addEventListener("click", function() {
  72. messenger.request(["removeSession", sessionName]);
  73. });
  74. row.onmouseover = function() {
  75. removeP.style.visibility = "visible";
  76. cellSession.style.borderRight = "2px solid white";
  77. }
  78. row.onmouseout = function() {
  79. removeP.style.visibility = "hidden";
  80. cellSession.style.borderRight = "";
  81. }
  82. }
  83. function clearTable() {
  84. document.getElementsByTagName("tbody")[0].innerHTML = '';
  85. document.getElementById("current_session_name").textContent = '';
  86. }
  87. // kickoff when popup is opened
  88. document.addEventListener('DOMContentLoaded', function() {
  89. start();
  90. });
  91. /*
  92. *
  93. * make requests to background
  94. *
  95. */
  96. var messenger = {
  97. request: function(message) {
  98. chrome.runtime.sendMessage({request: message});
  99. }
  100. }
  101. chrome.runtime.onMessage.addListener(function(msg) {
  102. if (msg.sessionList) {
  103. clearTable();
  104. console.log(JSON.stringify(msg.sessionList));
  105. msg.sessionList.forEach(function(session) {
  106. setData(session.name, session.windows, session.timeStamp);
  107. });
  108. }
  109. });