File size: 9,381 Bytes
ddabc6c
 
4d94b1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dfce822
 
 
 
4d94b1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dfce822
4d94b1f
 
 
dfce822
4d94b1f
dfce822
4d94b1f
 
 
 
 
 
 
 
 
 
 
dfce822
 
4d94b1f
 
 
 
 
 
 
 
 
dfce822
4d94b1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dfce822
4d94b1f
 
 
dfce822
4d94b1f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>My static Space</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="https://cdn.jsdelivr.net/npm/axios/dist/axios.min.js"></script>
    <link rel="preconnect" href="https://rsms.me/">
    <link rel="stylesheet" href="https://rsms.me/inter/inter.css">
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Noto+Sans+JP:wght@100;200;300;400;500;600;700;800;900&display=swap">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/github-markdown-css/5.5.0/github-markdown.min.css" crossorigin="anonymous" referrerpolicy="no-referrer" />
    <style>
      /* :root { font-family: 'Inter', sans-serif; }
      @supports (font-variation-settings: normal) {
        :root { font-family: 'Inter var', sans-serif; }
      } */
      body {
        margin: 0;
        background-color: #ffffff;
        width: 100%;
      }
      @media (prefers-color-scheme: dark) {
        body {
          background-color: #0d1117;
        }
      }
      body, .markdown-body, button {
        font-family: 'Inter', 'Noto Sans JP', sans-serif !important;
      }
      .markdown-body {
        box-sizing: border-box;
        width: 100%;
        max-width: 960px;
        margin-right: auto;
        margin-left: auto;
        padding: 1rem;
      }
      .markdown-body table {
        /* max-width: 100%; */
      }
      .markdown-body table th, .markdown-body table td {
        padding: 0px 13px;
      }
    </style>
  </head>
  <body>
    <div class="markdown-body">
      <h1>MiHoMo API Test</h1>
      <div class="parsedAPI_input">
        <p><span>Input UID: </span><input type="text" id="uidInputBox" name="uid" required minlength="9" maxlength="9" size="9" value="802415000"/> <button type="button" name="runBtn1" id="runBtn1" onclick="runBtn1Click()">Run</button></p>
        <p class="connectingLabelRoot" id="connectingLabelRoot" style="display: none;"><span class="connectingLabelChild" id="connectingLabelNowProgress" style="display: none;">Connecting... </span><span class="connectingLabelChild" id="connectingLabelOk" style="display: none;"></span></p>
      </div>
      <div id="responseDataPlaceholder">
        <h2>player (truncated)</h2>
        <table>
          <thead>
            <tr>
              <th>key</th>
              <th>value</th>
            </tr>
          </thead>
          <tbody id="responseDataPlaceholderPlayer">
          </tbody>
        </table>
        <h2>characters (truncated)</h2>
        <table>
          <thead>
            <tr>
              <th>key</th>
              <th>value</th>
            </tr>
          </thead>
          <tbody id="responseDataPlaceholderCharacters">
          </tbody>
        </table>
      </div>
    </div>
    <script>
      const StarRailResBaseUrl = 'https://raw.githubusercontent.com/Mar-7th/StarRailRes/master/';
      const apiConnectTimeout = 15000;
      const apiConnectUserAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36';
      const apiConnectDefaultHeader = {
        'Cache-Control': apiConnectUserAgent,
        'Access-Control-Allow-Origin': '*',
      };

      window.addEventListener('load', async function(){
        // some process
      });

      async function runBtn1Click () {
        // 入力されたUID読んでAPIへ接続
        const uidInputValue = document.querySelector('#uidInputBox').value;
        document.querySelector('#connectingLabelRoot').style.display = 'block';
        document.querySelector('#connectingLabelNowProgress').style.display = 'inline';
        document.querySelector('#connectingLabelOk').style.display = 'inline';
        const apiResponseData = await apiConnect({
          'method': 'get',
          'url': `https://corsproxy.io/?${encodeURIComponent(`https://api.mihomo.me/sr_info_parsed/${uidInputValue}?lang=jp&version=v2`)}`,
          'headers': apiConnectDefaultHeader,
          'timeout': apiConnectTimeout
        });
        console.log(apiResponseData);

        // APIのエラー時分岐処理
        if (apiResponseData) {
          document.querySelector('#connectingLabelOk').innerHTML = `OK (Time: ${Math.ceil(apiResponseData.apiConnectionTime)} ms)`;
          htmlBuildFromApiData(apiResponseData);
        } else {
          document.querySelector('#connectingLabelOk').innerHTML = `Failed`;
        }
      }

      async function apiConnect (axiosObj) {
        // 接続速度記録
        let connectionTimerStart = performance.now();
        try {
          const response = await axios(axiosObj);
          let connectionTimerEnd = performance.now();
          return {
            'apiConnectionTime': connectionTimerEnd - connectionTimerStart,
            'response': response.data
          };
        } catch (error) {
          let connectionTimerEnd = performance.now();
          console.error(`API request failed: ${error.code}`);
          alert(`API request failed: ${error.code}`);
          // console.error(error);
          throw error;
          return null;
        }
      }

      function htmlBuildFromApiData (apiResponseData) {
        const fakeClone01 = document.querySelector('#responseDataPlaceholderPlayer').cloneNode(false);
        const fakeClone02 = document.querySelector('#responseDataPlaceholderCharacters').cloneNode(false);
        document.querySelector('#responseDataPlaceholderPlayer').parentNode.replaceChild(fakeClone01, document.querySelector('#responseDataPlaceholderPlayer'));
        document.querySelector('#responseDataPlaceholderCharacters').parentNode.replaceChild(fakeClone02, document.querySelector('#responseDataPlaceholderCharacters'));
        let inputDataKeyList = [
          'player.uid',
          'player.nickname',
          'player.signature',
          'player.level',
          'player.world_level',
          'player.friend_count',
          'player.avatar.icon',
          'player.is_display',
          'player.space_info.memory_data.level',
          'player.space_info.memory_data.chaos_level',
          'player.space_info.universe_level',
          'player.space_info.challenge_data.maze_group_index',
          'player.space_info.challenge_data.pre_maze_group_index',
          'player.space_info.pass_area_progress',
          'player.space_info.light_cone_count',
          'player.space_info.avatar_count',
          'player.space_info.achievement_count',
        ];
        for (let i = 0; i < inputDataKeyList.length; i++) {
          document.querySelector('#responseDataPlaceholderPlayer').appendChild(trElAddReturn(apiResponseData.response, inputDataKeyList[i]));
        }
        for (let i = 0; i < apiResponseData.response.characters.length; i++) {
          let inputDataKeyList2 = [
            `characters.${i}.name`,
            `characters.${i}.rarity`,
            `characters.${i}.rank`,
            `characters.${i}.level`,
            `characters.${i}.promotion`,
            `characters.${i}.icon`,
            `characters.${i}.preview`,
            `characters.${i}.portrait`,
            `characters.${i}.path.name`,
            `characters.${i}.path.icon`,
            `characters.${i}.element.name`,
            `characters.${i}.element.icon`,
            `characters.${i}.light_cone.name`,
            `characters.${i}.light_cone.rarity`,
            `characters.${i}.light_cone.rank`,
            `characters.${i}.light_cone.level`,
            `characters.${i}.light_cone.promotion`,
            `characters.${i}.light_cone.icon`,
            `characters.${i}.light_cone.preview`,
            `characters.${i}.light_cone.portrait`,
            `characters.${i}.light_cone.path.name`,
            `characters.${i}.light_cone.path.icon`,
          ];
          for (let j = 0; j < inputDataKeyList2.length; j++) {
            document.querySelector('#responseDataPlaceholderCharacters').appendChild(trElAddReturn(apiResponseData.response, inputDataKeyList2[j]));
          }
        }
      }

      function trElAddReturn (inputData, objKey) {
        const imageDetectRegex = /\.(png|jpg|jpeg|webp|gif|bmp)$/i;
        const tr = document.createElement('tr');
        const tdKey = document.createElement('td');
        const tdValue = document.createElement('td');
        tdKey.innerHTML = objKey;
        // objKeyを分割して各部分を使用してオブジェクトのプロパティにアクセス
        let keyParts = objKey.split('.');
        let value = inputData;
        for (let i = 0; i < keyParts.length; i++) {
            // 配列のインデックスを表す文字列を数値に変換
            if (!isNaN(keyParts[i])) {
                keyParts[i] = parseInt(keyParts[i], 10);
            }
            value = value[keyParts[i]];
        }
        if (imageDetectRegex.test(value)) {
          const imageEl = document.createElement('img');
          imageEl.src = StarRailResBaseUrl + value;
          tdValue.appendChild(imageEl);
        } else {
          tdValue.innerHTML = value;
        }
        tr.appendChild(tdKey);
        tr.appendChild(tdValue);
        return tr;
      }
    </script>
  </body>
</html>