Global Session Storage

Global Storage

  1. // ==UserScript==
  2. // @name Global Session Storage
  3. // @namespace http://legitspam.edu/
  4. // @version 0.21
  5. // @description Global Storage
  6. // @match *://*/*
  7. // @require http://code.jquery.com/jquery-latest.js
  8. // @author http://blog.guya.net/2015/06/12/sharing-sessionstorage-between-tabs-for-secure-multi-tab-authentication/
  9. // @grant GM_getValue
  10. // @grant GM_setValue
  11. // ==/UserScript==
  12.  
  13. var sessionStorage_transfer = function(event) {
  14. if(!event) { event = window.event; } // ie suq
  15. if(!event.newValue) return; // do nothing if no value to work with
  16. if (event.key == 'getSessionStorage') {
  17. // another tab asked for the sessionStorage -> send it
  18. localStorage.setItem('sessionStorage', JSON.stringify(sessionStorage));
  19. // the other tab should now have it, so we're done with it.
  20. localStorage.removeItem('sessionStorage'); // <- could do short timeout as well.
  21. } else if (event.key == 'sessionStorage' && !sessionStorage.length) {
  22. // another tab sent data <- get it
  23. var data = JSON.parse(event.newValue);
  24. for (var key in data) {
  25. sessionStorage.setItem(key, data[key]);
  26. }
  27. }
  28. };
  29.  
  30. // listen for changes to localStorage
  31. if(window.addEventListener) {
  32. window.addEventListener("storage", sessionStorage_transfer, false);
  33. } else {
  34. window.attachEvent("onstorage", sessionStorage_transfer);
  35. };
  36.  
  37.  
  38. // Ask other tabs for session storage (this is ONLY to trigger event)
  39. if (!sessionStorage.length) {
  40. localStorage.setItem('getSessionStorage', 'foobar');
  41. localStorage.removeItem('getSessionStorage', 'foobar');
  42. };