File size: 12,406 Bytes
0dac506
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
<!DOCTYPE html>

<!-- 
  VOICEVOXエンジンの設定ページです。
  VueとBootstrapを使っています。
  ライブラリを読み込んだあと、Vueコンポーネントの初期化が完了してからUIを表示します。
-->

<html lang="ja">
  <head>
    <meta charset="utf-8" />
    <title>VOICEVOX Engine 設定</title>
    <link
      rel="shortcut icon"
      href="https://voicevox.hiroshiba.jp/favicon-32x32.png"
    />

    <style>
      .before-init-fadein {
        animation: fadein 0.5s;
      }

      /* 指定時間の最後に現れるフェードイン */
      @keyframes fadein {
        0% {
          opacity: 0;
        }
        95% {
          opacity: 0;
        }
        100% {
          opacity: 1;
        }
      }
    </style>
  </head>

  <body>
    <!-- Vueの準備が完了した後にdisplay: noneにする -->
    <div id="before-init" style="display: block" class="before-init-fadein">
      <p>読み込み中です。表示には数秒かかることがあります。</p>
    </div>

    <!-- Vueの準備が完了した後にdisplay: blockにする -->
    <div id="app" class="container p-3" style="display: none">
      <h1 class="mb-3">{{brandName}} エンジン 設定</h1>

      <div class="alert alert-warning" role="alert">
        変更を反映するにはエンジンの再起動が必要です。
      </div>

      <div class="mb-3">
        <label class="form-label">CORS Policy Mode</label>
        <select
          class="form-select"
          aria-label="corsPolicyMode"
          v-model="corsPolicyMode"
        >
          <option value="localapps">localapps</option>
          <option value="all">all</option>
        </select>
        <div class="form-text">
          <p class="mb-1">
            localappsはオリジン間リソース共有ポリシーを、app://.とlocalhost関連に限定します。
          </p>
          <p class="mb-1">
            その他のオリジンはAllow Originオプションで追加できます。
          </p>
          <p>allはすべてを許可します。危険性を理解した上でご利用ください。</p>
        </div>
      </div>

      <div class="mb-3">
        <label class="form-label">Allow Origin</label>
        <input
          class="form-control"
          type="text"
          v-model.trim.lazy="allowOrigin"
        />
        <div class="form-text">
          許可するオリジンを指定します。スペースで区切ることで複数指定できます。
        </div>
      </div>

      <div class="mb-3">
        <label class="form-label">ユーザー辞書のインポート</label>
        <div class="col-12">
          <button
            type="button"
            class="btn btn-primary"
            data-bs-toggle="modal"
            data-bs-target="#importUserDictModal"
          >
            インポート
          </button>
        </div>
      </div>

      <div class="mb-3">
        <label class="form-label">ユーザー辞書のエクスポート</label>
        <div class="col-12">
          <a
            download="VOICEVOXユーザー辞書.json"
            class="btn btn-primary mb-3"
            href="/user_dict"
            @click="showToastWithMessage('辞書をエクスポートしました。');"
            target="_blank"
            rel="noopener noreferrer"
          >
            エクスポート
          </a>
        </div>
      </div>

      <!-- ユーザー辞書インポート用モーダル -->
      <div
        class="modal fade"
        id="importUserDictModal"
        tabindex="-1"
        aria-labelledby="importUserDictModalLabel"
        aria-hidden="true"
      >
        <div class="modal-dialog">
          <div class="modal-content">
            <div class="modal-header">
              <h5 class="modal-title" id="importUserDictModalLabel">
                ユーザー辞書のインポート
              </h5>
              <button
                type="button"
                class="btn-close"
                data-bs-dismiss="modal"
                aria-label="Close"
              ></button>
            </div>
            <div class="modal-body">
              <input
                class="form-control"
                type="file"
                accept="application/json"
                @change="(e) => { userDictFileForImport = e.target.files[0]; }"
              />
            </div>
            <div class="modal-footer">
              <button
                type="button"
                class="btn btn-secondary"
                data-bs-dismiss="modal"
              >
                キャンセル
              </button>
              <button
                type="button"
                @click="importUserDict"
                class="btn btn-primary"
                data-bs-dismiss="modal"
                :disabled="userDictFileForImport == undefined"
              >
                インポート
              </button>
            </div>
          </div>
        </div>
      </div>

      <!-- トースト -->
      <div class="position-fixed bottom-0 end-0 p-3" style="z-index: 5">
        <div
          class="toast align-items-center autohide text-white bg-success"
          role="alert"
          aria-live="assertive"
          aria-atomic="true"
          ref="toastElem"
        >
          <div class="d-flex">
            <div class="toast-body">{{toastMessage}}</div>
          </div>
        </div>
      </div>
    </div>

    <script>
      // Vueの初期化
      function initVue() {
        const { createApp, ref, watch, onMounted } = Vue;
        createApp({
          setup() {
            // 設定値周り
            const corsPolicyMode = ref(
              "<JINJA_PRE>cors_policy_mode<JINJA_POST>"
            );
            const allowOrigin = ref("<JINJA_PRE>allow_origin<JINJA_POST>");

            // 設定が変更されたら自動保存
            watch([corsPolicyMode, allowOrigin], () => {
              const formData = new FormData();
              formData.append("cors_policy_mode", corsPolicyMode.value);
              formData.append("allow_origin", allowOrigin.value);

              fetch("/setting", {
                method: "POST",
                mode: "same-origin",
                body: formData,
              }).then((res) => {
                if (res.ok) {
                  showToastWithMessage("設定を保存しました。");
                } else {
                  showToastWithMessage("設定の保存に失敗しました。");
                }
              });
            });

            // ユーザー辞書周り
            const userDictFileForImport = ref();

            const importUserDict = () => {
              if (userDictFileForImport.value == undefined) {
                throw new Error("userDictFileForImportが見つかりません。");
              }

              const reader = new FileReader();
              reader.addEventListener("load", async () => {
                const params = new URLSearchParams({
                  override: true, // 重複するエントリを上書きする
                });
                await fetch(`/import_user_dict?${params}`, {
                  method: "POST",
                  mode: "same-origin",
                  headers: { "Content-Type": "application/json" },
                  body: reader.result,
                });

                showToastWithMessage("辞書をインポートしました。");
              });

              reader.readAsText(userDictFileForImport.value);
            };

            // トースト
            const toastElem = ref(undefined);
            const bootstrapToast = ref(undefined);
            const toastMessage = ref("");
            onMounted(() => {
              if (toastElem.value == undefined) {
                throw new Error("toastElemが見つかりません。");
              }
              bootstrapToast.value = new bootstrap.Toast(toastElem.value);
            });
            const showToastWithMessage = (message) => {
              console.log(`showToastWithMessage: ${message}`);
              bootstrapToast.value.show();
              toastMessage.value = message;
            };

            // 表示用の情報
            const brandName = ref("<JINJA_PRE>brand_name<JINJA_POST>");

            // Vueの準備が完了したら表示・非表示を切り替える
            onMounted(() => {
              document.getElementById("before-init").style.display = "none";
              document.getElementById("app").style.display = "block";
            });

            return {
              corsPolicyMode,
              allowOrigin,
              userDictFileForImport,
              importUserDict,
              toastElem,
              toastMessage,
              showToastWithMessage,
              brandName,
            };
          },
        }).mount("#app");
      }

      /**
       * CDNからscriptやCSSを読み込む。
       * CDNが使えないときのために複数の候補を試す。
       */
      const loadCDN = async (scriptOrCss, candidateUrlList, integrity) => {
        if (scriptOrCss !== "script" && scriptOrCss !== "css") {
          throw new Error("scriptOrCssはscriptかcssを指定してください。");
        }

        let current = 0;
        await new Promise((resolve, reject) => {
          const loadNext = async () => {
            if (current >= candidateUrlList.length) {
              reject(new Error("全てのCDNで読み込みに失敗しました。"));
              return;
            }

            let elem;
            if (scriptOrCss === "script") {
              elem = document.createElement("script");
              elem.src = candidateUrlList[current];
            } else {
              elem = document.createElement("link");
              elem.href = candidateUrlList[current];
              elem.rel = "stylesheet";
            }
            elem.integrity = integrity;
            elem.crossOrigin = "anonymous";
            elem.onload = resolve;
            elem.onerror = () => {
              console.warn(
                `CDNの読み込みに失敗しました。 ${candidateUrlList[current]}`
              );
              document.head.removeChild(elem);
              current++;
              loadNext();
            };
            document.head.appendChild(elem);
          };
          loadNext();
        });
      };

      // 初期化用の関数
      const init = async () => {
        // ライブラリ読み込み用のPromiseリスト
        const libraryLoadingPromises = [];

        // Bootstrapを読み込む
        const bootstrapCssPromise = loadCDN(
          "css",
          [
            "https://unpkg.com/[email protected]/dist/css/bootstrap.min.css",
            "https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css",
            "https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.0.2/css/bootstrap.min.css",
          ],
          "sha384-EVSTQN3/azprG1Anm3QDgpJLIm9Nao0Yz1ztcQTwFspd3yD65VohhpuuCOmLASjC"
        );
        libraryLoadingPromises.push(bootstrapCssPromise);

        const bootstrapScriptPromise = loadCDN(
          "script",
          [
            "https://unpkg.com/[email protected]/dist/js/bootstrap.bundle.min.js",
            "https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js",
            "https://cdnjs.cloudflare.com/ajax/libs/bootstrap/5.0.2/js/bootstrap.bundle.min.js",
          ],
          "sha384-MrcW6ZMFYlzcLA8Nl+NtUVF0sA7MsXsP1UyJoMp4YLEuNSfAP+JcXn/tWtIaxVXM"
        );
        libraryLoadingPromises.push(bootstrapScriptPromise);

        // Vueを読み込む
        const vuePromise = loadCDN(
          "script",
          [
            "https://unpkg.com/[email protected]/dist/vue.global.js",
            "https://cdn.jsdelivr.net/npm/[email protected]/dist/vue.global.js",
            "https://cdnjs.cloudflare.com/ajax/libs/vue/3.3.10/vue.global.js",
          ],
          "sha384-ttfhgYK68lNlS8ak6Z//mvUbpRbRCh43MYGuqEtK8mj/yzlKqY8GA8o3BPMi23cE"
        );
        libraryLoadingPromises.push(vuePromise);

        // ライブラリの読み込みが完了したらVueを初期化
        await Promise.all(libraryLoadingPromises);
        initVue();
      };
      init();
    </script>
  </body>
</html>