setupBeforeUnloadListener = () => {
      window.addEventListener('beforeunload', function () {
        // Don't save anything unless the user opted in to storage
        if (
          !/(?:^|;\s*)svgeditstore=(?:prefsAndContent|prefsOnly)/.test(
            document.cookie
          )
        ) {
          return
        }
        if (/(?:^|;\s*)svgeditstore=prefsAndContent/.test(document.cookie)) {
          setSvgContentStorage(svgCanvas.getSvgString())
        }

        svgEditor.setConfig({ no_save_warning: true }) // No need for explicit saving at all once storage is on

        const { curPrefs } = svgEditor.configObj

        Object.entries(curPrefs).forEach(([key, val]) => {
          const store = val !== undefined
          key = 'svg-edit-' + key
          if (!store) {
            return
          }
          if (storage) {
            storage.setItem(key, val)
          } else if (window.widget) {
            window.widget.setPreferenceForKey(val, key)
          } else {
            val = encodeURIComponent(val)
            document.cookie =
              encodeURIComponent(key) +
              '=' +
              val +
              '; expires=Fri, 31 Dec 9999 23:59:59 GMT'
          }
        })
      })
    }
Listen for unloading: If and only if opted in by the user, set the content
  document and preferences into storage:
1. Prevent save warnings (since we're automatically saving unsaved
      content into storage)
2. Use localStorage to set SVG contents (potentially too large to allow in cookies)
3. Use localStorage (where available) or cookies to set preferences.
@returns {void}