about summary refs log tree commit diff stats
path: root/src/pages/options_src/General/SettingsButtons.svelte
blob: 1b92ecbca94c48b09f900859e6cadc119a349c4b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<script>
  const browser = window.browser || window.chrome

  import { onDestroy } from "svelte"
  import Button from "../../components/Button.svelte"
  import ExportIcon from "../../icons/ExportIcon.svelte"
  import ImportIcon from "../../icons/ImportIcon.svelte"
  import ResetIcon from "../../icons/ResetIcon.svelte"
  import { options } from "../stores"
  import servicesHelper from "../../../assets/javascripts/services.js"
  import utils from "../../../assets/javascripts/utils.js"

  let _options
  const unsubscribe = options.subscribe(val => (_options = val))
  onDestroy(unsubscribe)

  let importSettingsInput
  let importSettingsFiles
  $: if (importSettingsFiles) {
    const reader = new FileReader()
    reader.readAsText(importSettingsFiles[0])
    reader.onload = async () => {
      let data = JSON.parse(reader.result)
      if (data.version != browser.runtime.getManifest().version) {
        alert("Importing from a previous version. Be careful")
      }
      data = await servicesHelper.processUpdate(data)
      options.set(data)
    }
    reader.onerror = error => {
      console.log("error", error)
      alert("Error!")
    }
  }

  async function exportSettings() {
    _options.version = browser.runtime.getManifest().version
    const resultString = JSON.stringify(_options, null, "  ")
    const anchor = document.createElement("a")
    anchor.href = "data:application/json;base64," + btoa(resultString)
    anchor.download = `libredirect-settings-v${_options.version}-${(new Date().toISOString().replace(':','-').slice(0,-5))}.json`
    anchor.click()
  }

  async function exportSettingsSync() {
    _options.version = browser.runtime.getManifest().version
    browser.storage.sync.set({ options: _options })
  }

  async function importSettingsSync() {
    browser.storage.sync.get({ options }, async r => {
      let data = r.options
      if (data.version != browser.runtime.getManifest().version) {
        alert("Importing from a previous version. Be careful")
      }
      data = await servicesHelper.processUpdate(data)
      options.set(data)
    })
  }

  async function resetSettings() {
    browser.storage.local.clear(async () => {
      const data = await servicesHelper.initDefaults()
      options.set(data)
    })
  }
</script>

<div class="buttons">
  <Button on:click={() => importSettingsInput.click()}>
    <ImportIcon class="margin margin_{document.body.dir}" />
    {browser.i18n.getMessage("importSettings") || "Import Settings"}
  </Button>
  <input
    type="file"
    accept=".json"
    style="display: none"
    bind:this={importSettingsInput}
    bind:files={importSettingsFiles}
  />

  <Button on:click={exportSettings}>
    <ExportIcon class="margin margin_{document.body.dir}" />
    {browser.i18n.getMessage("exportSettings") || "Export Settings"}
  </Button>

  <Button on:click={exportSettingsSync}>
    <ExportIcon class="margin margin_{document.body.dir}" />
    {browser.i18n.getMessage("exportSettingsToSync") || "Export Settings to Sync"}
  </Button>

  <Button on:click={importSettingsSync}>
    <ImportIcon class="margin margin_{document.body.dir}" />
    {browser.i18n.getMessage("importSettingsFromSync") || "Import Settings from Sync"}
  </Button>

  <Button on:click={resetSettings}>
    <ResetIcon class="margin margin_{document.body.dir}" />
    {browser.i18n.getMessage("resetSettings") || "Reset Settings"}
  </Button>
</div>

<style>
  :global(.margin) {
    margin-right: 10px;
    margin-left: 0;
  }
  :global(.margin_rtl) {
    margin-right: 0;
    margin-left: 10px;
  }
</style>