Windows 上で、hta アプリの設定を JSON ファイルに保存する Javascript クラス。localstorage 風味。

MyStorage.hta

<!DOCTYPE html>
<html>

<head>
   <meta charset="utf-8" />
   <meta http-equiv="X-UA-Compatible" content="IE=edge">
   <title>Class Test</title>
   <meta name="viewport" content="width=device-width, initial-scale=1">
   <!-- <link rel="stylesheet" type="text/css" media="screen" href="ClassTest.css" />
   <script src="ClassTest.ja"></script> -->
</head>

<body>
    <script>
       "use strict"
       // class Storage {
       //  constructor() {
       //      this.hello = "Hello"
       //  }
       //  alert() {
       //      window.alert(this.hello)
       //  }
       // }

       // ------------------------------------------------------------
       // namespace My
       var My = My || {};

       // ------------------------------------------------------------
       // class Storage
       // this.rootKey:    ルートキー。このキーで JSON ファイルに保存される。文字列。
       // this.rootData:   ルートキーに対応する value として保存されるデータ。連想配列。
       My.Storage = function (rootKey) {
           this.rootKey = rootKey;
           this.rootData = {};
       }
       My.Storage.prototype = {
           // key に対応する value を返す。
           // 対応する value が内場合は unefined を返す。
           getItem: function (key) {
               if (key in this.rootData) {
                   return this.rootData[key];
               }
               return undefined;
           },
           // key, value のペアを保存する。
           // save() するまでは JSON ファイルに書き出されない。
           setItem: function (key, value) {
               this.rootData[key] = value;
           },
           // JSON ファイル名を返す。
           // この hta ファイルの名前から拡張子を .json にしたファイル名を作って返す。
           getJsonFileName: function () {
               let filename = location.pathname;
               // なぜか先頭に "/" が入っているので削除する
               filename = filename.replace(/^\//, "");
               // alert(filename);
               let jsonfile = filename.replace(/^(.+)\.hta$/gi, "$1.json");
               // alert(jsonfile);
               return jsonfile;
           },
           // JSON ファイルから this.rootKey に紐付く連想配列を読み込む。
           load: function () {
               const ForReading = 1;
               const TristateTrue = -1; // unicode
               let allData = {};
               let fso = new ActiveXObject("Scripting.FileSystemObject");
               let jsonfile = this.getJsonFileName();
               if (fso.FileExists(jsonfile)) {
                   const ForReading = 1;
                   let ts = fso.OpenTextFile(jsonfile, ForReading, true, TristateTrue);
                   if (!ts.AtEndOfStream) {
                       allData = JSON.parse(ts.ReadAll());
                   }
                   ts.Close();
               }
               this.rootData = allData[this.rootKey] || {};
           },
           // JSON ファイルに this.rootKey に紐付く連想配列を保存する。
           save: function () {
               const ForReading = 1;
               const TristateTrue = -1; // unicode
               let allData = {};
               allData[this.rootKey] = this.rootData;
               let fso = new ActiveXObject("Scripting.FileSystemObject");
               let jsonfile = this.getJsonFileName();
               if (fso.FileExists(jsonfile)) {
                   let ts = fso.OpenTextFile(jsonfile, ForReading, true, TristateTrue);
                   if (!ts.AtEndOfStream) {
                       let readed = JSON.parse(ts.ReadAll());
                       readed[this.rootKey] = this.rootData;
                       allData = readed;
                   }
                   ts.Close();
               }
               let ts = fso.CreateTextFile(jsonfile, true, TristateTrue);
               ts.Write(JSON.stringify(allData));
               ts.Close();
           },
       }
       // ------------------------------------------------------------

       window.onload = function () {
           window.resizeTo(800, 640);
           // alert("onload");
           let sto = new My.Storage("Class1");
           sto.load();
           alert(sto.getItem("Hello"));
           alert(sto.getItem("ハロー"));
           sto.setItem("Hello", "World");
           sto.setItem("ハロー", "ワールド")
           sto.save();
       }
   </script>
</body>

</html>