background.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. localforage.config({
  2. driver : [localforage.INDEXEDDB, localforage.WEBSQL, localforage.LOCALSTORAGE],
  3. name : 'bmux',
  4. version : 1.0,
  5. storeName : 'sessions',
  6. description : 'browser session manager'
  7. });
  8. /*
  9. // when chrome restores a session, check if the tabs match bmux saved sessions
  10. // and if so, attach to the matching one
  11. chrome.windows.onCreated.addListener(function() {
  12. chrome.tabs.query({"currentWindow": true}, function(tabs) {
  13. for (var i = 0; i < sessions.length; i++) {
  14. if (tabs == sessions) {
  15. sessions[i].active = true;
  16. break;
  17. }
  18. }
  19. });
  20. });*/
  21. /*
  22. *
  23. * update sessions when page updates
  24. *
  25. */
  26. function updateCurrentSession(currentWindowId) {
  27. localforage.iterate(function(value, key, iteration) {
  28. value.windows.forEach(function(window) {
  29. if (window.id === currentWindowId) {
  30. // currently in session, update extension icon
  31. saveSession(key);
  32. }
  33. });
  34. });
  35. }
  36. chrome.tabs.onCreated.addListener(function(tab) {
  37. updateCurrentSession(tab.windowId);
  38. });
  39. chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
  40. updateCurrentSession(tab.windowId);
  41. });
  42. chrome.tabs.onMoved.addListener(function(tabId, moveInfo) {
  43. updateCurrentSession(moveInfo.windowId);
  44. });
  45. chrome.tabs.onActivated.addListener(function(activeInfo) {
  46. updateCurrentSession(activeInfo.windowId);
  47. });
  48. chrome.tabs.onDetached.addListener(function(tabId, detachInfo) {
  49. updateCurrentSession(detachInfo.oldWindowId);
  50. });
  51. chrome.tabs.onAttached.addListener(function(tabId, attachInfo) {
  52. updateCurrentSession(attachInfo.newWindowId);
  53. });
  54. chrome.tabs.onRemoved.addListener(function(tabId, removeInfo) {
  55. updateCurrentSession(removeInfo.windowId);
  56. });
  57. function getAllSessionWindows() {
  58. sessions = [];
  59. localforage.iterate(function(value, key, iteration) {
  60. session = value;
  61. session['name'] = key;
  62. sessions.push(session);
  63. }).then(function() {
  64. sessions.forEach(function(session) {
  65. session.windows.forEach(function(window) {
  66. chrome.windows.get(window.id, {populate: true}, function(window) {
  67. console.log(window);
  68. // do something
  69. });
  70. });
  71. });
  72. });
  73. }
  74. chrome.windows.onCreated.addListener(function(window) {
  75. updateCurrentSession(window.id);
  76. });
  77. chrome.windows.onRemoved.addListener(function(windowId) {
  78. updateCurrentSession(windowId);
  79. });
  80. chrome.windows.onFocusChanged.addListener(function(windowId) {
  81. updateCurrentSession(windowId);
  82. });
  83. var messenger = {
  84. sessionList: function(message) {
  85. chrome.runtime.sendMessage({sessionList: message});
  86. }
  87. }
  88. function listSessions() {
  89. sessions = [];
  90. localforage.iterate(function(value, key, iteration) {
  91. session = value;
  92. session['name'] = key;
  93. sessions.push(session);
  94. }, function() {
  95. messenger.sessionList(sessions);
  96. });
  97. }
  98. function saveSession(sessionName, callback) {
  99. chrome.windows.getAll({populate: true}, function(windows) {
  100. var session = {};
  101. session['windows'] = windows;
  102. session['timeStamp'] = new Date().toString();
  103. session['name'] = sessionName;
  104. localforage.setItem(sessionName, session, function(err, value) {
  105. if (err) console.log(err);
  106. if (callback) callback();
  107. });
  108. })
  109. }
  110. function openSession(session) {
  111. session.windows.forEach(function(w) {
  112. chrome.windows.create({state: w.state, focused: w.focused}, function(window) {
  113. w.tabs.forEach(function(tab) {
  114. chrome.tabs.create({windowId: window.id, index: tab.index, url: tab.url, active: tab.active, pinned: tab.pinned});
  115. });
  116. });
  117. });
  118. saveSession(session.name);
  119. }
  120. function closeAllWindows() {
  121. chrome.windows.getAll(function(windows) {
  122. windows.forEach(function(window) {
  123. chrome.windows.remove(window.id)
  124. });
  125. });
  126. }
  127. function closeWindow(windowId) {
  128. chrome.windows.remove(windowId);
  129. }
  130. /*
  131. *
  132. * send data to UIs
  133. *
  134. */
  135. chrome.runtime.onMessage.addListener(function(msg) {
  136. if (msg.request) {
  137. /*
  138. * send session data on UI start
  139. */
  140. if (msg.request[0] === "sessions") {
  141. listSessions();
  142. /*
  143. * create a new session
  144. */
  145. } else if (msg.request[0] === "createSession") {
  146. saveSession(msg.request[1], listSessions);
  147. /*
  148. * attach to session
  149. */
  150. } else if (msg.request[0] === "attach") {
  151. closeAllWindows();
  152. localforage.getItem(msg.request[1], function(err, value) {
  153. openSession(value);
  154. });
  155. /*
  156. * detach from session
  157. */
  158. } else if (msg.request[0] === 'detach') {
  159. closeAllWindows();
  160. chrome.windows.create();
  161. /*
  162. * rename a session
  163. * ~not functional~
  164. */
  165. } else if (msg.request[0] === 'rename') {
  166. localforage.getItem(msg.request[1], function(err, value) {
  167. localforage.removeItem(msg.request[1], function(err) {
  168. saveSession(msg.request[2], value);
  169. });
  170. });
  171. /*
  172. * remove a session
  173. */
  174. } else if (msg.request[0] === "removeSession") {
  175. localforage.removeItem(msg.request[1], function(err) {
  176. listSessions();
  177. });
  178. /*
  179. * clear sessions
  180. */
  181. } else if (msg.request[0] === "clearSessions") {
  182. localforage.clear().then(function() {
  183. listSessions();
  184. });
  185. }
  186. }
  187. });