preventMultitabs.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. /* eslint-disable no-console */
  2. export default ({ store, isHMR, app }, inject) => {
  3. inject('preventMultitabs', main)
  4. }
  5. function main(store) {
  6. const id = Date.now()
  7. window.id = id
  8. window.localStorage.setItem('firstTab', id)
  9. const onLocalStorageEvent = function(e) {
  10. // the second tab will write its id to this key. The first one will notice it
  11. if (e.key === 'firstTab') {
  12. const newID = Date.now()
  13. console.log('Another tab detected. Setting the new page id', newID)
  14. setTimeout(() => {
  15. window.localStorage.secondTab = newID // this is going to be a message for the second tab
  16. }, 200)
  17. }
  18. // the second tab proccesses the message
  19. if (e.key === 'secondTab' && window.id.toString() === window.localStorage.firstTab) {
  20. console.log('There is another tab that already opened. We will close this one')
  21. window.multipleTabsDetected = true
  22. window.onbeforeunload = null
  23. window.alert(
  24. 'Multiple tabs opened. Your page will be closed. Please only use single instance of https://tornado.cash'
  25. )
  26. window.location = 'https://twitter.com/tornadocash'
  27. }
  28. }
  29. // this event will only trigger when a window other than itself makes changes to local storage.
  30. setTimeout(() => {
  31. window.addEventListener('storage', onLocalStorageEvent, false)
  32. }, 100)
  33. }