Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.71 KB | None | 0 0
  1. window.addEventListener("beforeunload", function() {
  2. let id = getSessionStorageItem("sessionStorageTabId");
  3.  
  4. if (getLocalStorageItem('leadTab') === id) {
  5. electNewLead();
  6. } else {
  7. removeTab(id);
  8. }
  9.  
  10. // Chrome requires returnValue to be set
  11. e.returnValue = '';
  12. });
  13.  
  14. function initializeTab() {
  15. if (!getSessionStorageItem('sessionStorageTabId')) {
  16. setSessionStorageItem('sessionStorageTabId', Math.random().toString())
  17. }
  18.  
  19. return getSessionStorageItem("sessionStorageTabId");
  20. }
  21.  
  22. function registerTab(currentTabId) {
  23. console.log("Registering tab with ID:", currentTabId);
  24.  
  25. const leadTabID = getLocalStorageItem('leadTab');
  26. if (leadTabID) {
  27. if (leadTabID !== currentTabId) {
  28. console.log("Assigning tab ID: ", currentTabId, "as excess tab.");
  29. assignAsExcessTab(currentTabId);
  30. return;
  31. } else {
  32. console.log("This tab is already a leader.");
  33. return;
  34. }
  35. }
  36.  
  37. console.log("Assigning this (current) tab with ID: ", currentTabId, "as a leader");
  38. assignAsLeader(currentTabId);
  39. return;
  40. }
  41.  
  42. function electNewLead() {
  43. let existingTabs = JSON.parse(getLocalStorageItem('excessTabs'));
  44.  
  45. if (existingTabs && existingTabs[0]) {
  46. assignAsLeader(existingTabs[0]);
  47. removeTab(existingTabs[0]);
  48. }
  49. }
  50.  
  51. function removeTab(tabId) {
  52. let existingTabs = JSON.parse(getLocalStorageItem('excessTabs'));
  53.  
  54. if (existingTabs) {
  55. existingTabs = existingTabs.filter(excessTabId => excessTabId !== tabId);
  56. setLocalStorageItem('excessTabs', JSON.stringify(existingTabs))
  57. }
  58. }
  59.  
  60. function assignAsLeader(tabId) {
  61. setLocalStorageItem('leadTab', tabId);
  62. }
  63.  
  64. function assignAsExcessTab(tabId) {
  65. const excessTabs = getLocalStorageItem('excessTabs');
  66. if(excessTabs) {
  67. let existingTabs = JSON.parse(excessTabs);
  68.  
  69. existingTabs.push(tabId);
  70. setLocalStorageItem('excessTabs', JSON.stringify(existingTabs));
  71. console.log("Tab with ID: ", tabId, "was successfully added to the excess tabs list.");
  72. return;
  73. } else {
  74. setLocalStorageItem('excessTabs', JSON.stringify([tabId]));
  75. console.log("This tab is the first excess tab.");
  76. return;
  77. }
  78. }
  79.  
  80. function getSessionStorageItem(key) {
  81. return window.sessionStorage.getItem(key);
  82. }
  83.  
  84. function setSessionStorageItem(key, value) {
  85. return window.sessionStorage.setItem(key, value);
  86. }
  87.  
  88. function getLocalStorageItem(key) {
  89. return window.localStorage.getItem(key);
  90. }
  91.  
  92. function setLocalStorageItem(key, value) {
  93. return window.localStorage.setItem(key, value);
  94. }
  95.  
  96. function removeLocalStorageItem(key) {
  97. return window.localStorage.removeItem(key);
  98. }
  99.  
  100. registerTab(initializeTab());
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement