about summary refs log tree commit diff stats
path: root/src/pages/options_src/Services/Services.svelte
blob: dcc826faaa669bc8d8cef8c6e2b60672ece1d0b4 (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<script>
  const browser = window.browser || window.chrome

  import url from "../url"
  import Row from "../../components/Row.svelte"
  import Label from "../../components/Label.svelte"
  import Select from "../../components/Select.svelte"
  import { options, config } from "../stores"
  import RedirectType from "./RedirectType.svelte"
  import { onDestroy } from "svelte"
  import Instances from "./Instances.svelte"
  import SvelteSelect from "svelte-select"
  import ServiceIcon from "./ServiceIcon.svelte"
  import FrontendIcon from "./FrontendIcon.svelte"
  import Checkbox from "../../components/Checkbox.svelte"

  let _options
  let _config

  const unsubscribeOptions = options.subscribe(val => (_options = val))
  const unsubscribeConfig = config.subscribe(val => (_config = val))
  onDestroy(() => {
    unsubscribeOptions()
    unsubscribeConfig()
  })
  let selectedService = $url.hash.startsWith("#services:") ? $url.hash.split(":")[1] : "youtube"
  let hideServiceSelection = false
  let hideFrontendSelection = false
  $: serviceConf = _config.services[selectedService]
  $: serviceOptions = _options[selectedService]
  $: frontendWebsite = serviceConf.frontends[serviceOptions.frontend].url
  $: servicesEntries = Object.entries(_config.services)
  $: frontendEntries = Object.entries(serviceConf.frontends)
</script>

<div>
  <Row>
    <Label>
      <a href={serviceConf.url} style="text-decoration: underline;" target="_blank" rel="noopener noreferrer">
        {browser.i18n.getMessage("service") || "Service"}
      </a>
    </Label>
    <div dir="ltr" on:click={() => (hideServiceSelection = true)} on:keydown={null}>
      <SvelteSelect
        clearable={false}
        class="svelte_select"
        value={selectedService}
        showChevron
        on:change={e => {
          selectedService = e.detail.value
          window.location.hash = `services:${e.detail.value}`
          hideServiceSelection = false
        }}
        on:pointerup={() => (hideServiceSelection = false)}
        on:focus={() => (hideServiceSelection = true)}
        on:blur={() => (hideServiceSelection = false)}
        items={[
          ...servicesEntries.map(([serviceKey, service]) => {
            return { value: serviceKey, label: service.name }
          }),
        ]}
      >
        <div class={"slot " + (!_options[item.value].enabled && "disabled")} slot="item" let:item>
          <ServiceIcon details={item} />
          {item.label}
        </div>
        <div
          class={"slot " + (!_options[selection.value].enabled && !hideServiceSelection && "disabled")}
          slot="selection"
          let:selection
        >
          {#if !hideServiceSelection}
            <ServiceIcon details={selection} />
            {selection.label}
          {:else}
            {browser.i18n.getMessage("searchService") || "Search Service"}
          {/if}
        </div>
        <div style="font-size: 10px;" slot="chevron-icon">🮦</div>
      </SvelteSelect>
    </div>
  </Row>

  <hr />

  <Row>
    <Label>{browser.i18n.getMessage("enable") || "Enable"}</Label>
    <Checkbox
      checked={serviceOptions.enabled}
      onChange={e => {
        serviceOptions.enabled = e.target.checked
        options.set(_options)
      }}
    />
  </Row>

  <Row>
    <Label>{browser.i18n.getMessage("showInPopup") || "Show in popup"}</Label>
    <Checkbox
      checked={_options.popupServices.includes(selectedService)}
      onChange={e => {
        if (e.target.checked && !_options.popupServices.includes(selectedService)) {
          _options.popupServices.push(selectedService)
        } else if (_options.popupServices.includes(selectedService)) {
          const index = _options.popupServices.indexOf(selectedService)
          if (index !== -1) _options.popupServices.splice(index, 1)
        }
        options.set(_options)
      }}
    />
  </Row>

  <div style={!serviceOptions.enabled && "pointer-events: none;opacity: 0.4;user-select: none;"}>
    <Row>
      <Label>
        <a href={frontendWebsite} style="text-decoration: underline;" target="_blank" rel="noopener noreferrer">
          {browser.i18n.getMessage("frontend") || "Frontend"}
        </a>
      </Label>
      <div dir="ltr" on:click={() => (hideFrontendSelection = true)} on:keydown={null}>
        <SvelteSelect
          clearable={false}
          dir="ltr"
          class="svelte_select"
          value={serviceOptions.frontend}
          showChevron
          on:change={e => {
            serviceOptions.frontend = e.detail.value
            options.set(_options)
            hideFrontendSelection = false
          }}
          on:pointerup={() => (hideServiceSelection = false)}
          on:focus={() => (hideFrontendSelection = true)}
          on:blur={() => (hideFrontendSelection = false)}
          items={[
            ...frontendEntries.map(([frontendId, frontend]) => ({
              value: frontendId,
              label: frontend.name,
            })),
          ]}
        >
          <div class="slot" slot="item" let:item>
            <FrontendIcon details={item} {selectedService} />
            {item.label}
          </div>
          <div class="slot" slot="selection" let:selection>
            {#if !hideFrontendSelection}
              <FrontendIcon details={selection} {selectedService} />
              {selection.label}
            {:else}
              {browser.i18n.getMessage("search_frontend") || "Search Frontend"}
            {/if}
          </div>
          <div style="font-size: 10px;" slot="chevron-icon">🮦</div>
        </SvelteSelect>
      </div>
    </Row>

    <RedirectType {selectedService} />

    <Row>
      <Label>{browser.i18n.getMessage("unsupportedIframesHandling") || "Unsupported embeds handling"}</Label>
      <Select
        value={serviceOptions.unsupportedUrls}
        onChange={e => {
          serviceOptions.unsupportedUrls = e.target.options[e.target.options.selectedIndex].value
          options.set(_options)
        }}
        values={[
          { value: "bypass", name: browser.i18n.getMessage("bypass") || "Bypass" },
          { value: "block", name: browser.i18n.getMessage("block") || "Block" },
        ]}
      />
    </Row>

    <div style={_options.redirectOnlyInIncognito && "pointer-events: none;opacity: 0.4;user-select: none;"}>
      <Row>
        <Label>{browser.i18n.getMessage("redirectOnlyInIncognito") || "Redirect Only in Incognito"}</Label>
        <Checkbox
          checked={serviceOptions.redirectOnlyInIncognito}
          onChange={e => {
            serviceOptions.redirectOnlyInIncognito = e.target.checked
            options.set(_options)
          }}
        />
      </Row>
    </div>

    {#if selectedService == "search"}
      <Row>
        <Label>{browser.i18n.getMessage("redirectGoogle") || "Redirect Google"}</Label>
        <Checkbox
          checked={serviceOptions.redirectGoogle}
          onChange={e => {
            serviceOptions.redirectGoogle = e.target.checked
            options.set(_options)
          }}
        />
      </Row>
      <Row>
        <Label>
          {@html browser.i18n.getMessage("searchHint") ||
            `Set LibRedirect as Default Search Engine. For how to do in chromium browsers, click
          <a
            href="https://libredirect.github.io/docs.html#search_engine_chromium"
            target="_blank"
            rel="noopener noreferrer"
            >here
          </a>.`}
        </Label>
      </Row>
    {/if}

    <Instances
      {selectedService}
      selectedFrontend={!serviceConf.frontends[serviceOptions.frontend].desktopApp ||
      serviceOptions.redirectType == "main_frame"
        ? serviceOptions.frontend
        : serviceOptions.embedFrontend}
    />

    <Row></Row>
  </div>
</div>

<style>
  :global(.svelte_select) {
    font-weight: bold;
    --item-padding: 0 10px;
    --border: none;
    --border-hover: none;
    --border-focused: none;
    --width: 210px;
    --background: var(--bg-secondary);
    --list-background: var(--bg-secondary);
    --item-is-active-bg: grey;
    --item-hover-bg: grey;
    --item-is-active-color: var(--text);
    --list-max-height: 400px;
    --padding: 0 0 0 10px;
    --item-color: var(--text);
  }
  :global(.svelte_select .slot) {
    display: flex;
    justify-content: start;
    align-items: center;
  }

  :global(.svelte_select img, .svelte_select svg) {
    margin-right: 10px;
    margin-left: 0;
    height: 26px;
    width: 26px;
    color: var(--text);
  }

  :global(.svelte_select .disabled) {
    opacity: 0.4;
  }
</style>