fix: рынок тянет живую базу с фолбэками и кнопкой обновить

#2
by Iosteps - opened
Files changed (1) hide show
  1. assets/market.js +42 -10
assets/market.js CHANGED
@@ -165,20 +165,52 @@
165
  if (lot) lot.classList.toggle('open');
166
  });
167
 
168
- fetch('assets/vendors.json', { cache: 'no-store' })
169
- .then(function (r) { if (!r.ok) throw new Error('HTTP ' + r.status); return r.json(); })
170
- .then(function (json) {
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
171
  data = json;
172
  buildControls();
173
  var upd = data.updatedAt ? new Date(data.updatedAt) : null;
174
  document.getElementById('mk-updated').innerHTML =
175
- 'Снимок рынка: ' + (upd ? upd.toLocaleString('ru-RU', { day: 'numeric', month: 'long', hour: '2-digit', minute: '2-digit' }) : '—') +
176
- ' · обновляется каждые ' + (data.refreshMinutes || 60) + ' мин на ' +
177
  '<a href="https://dfo.spaceghosts.com/#vendors" target="_blank" rel="noopener">Daggerfall Watch</a>';
 
 
178
  update();
179
- })
180
- .catch(function (err) {
181
- listEl.innerHTML = '<p class="note">Не удалось загрузить снимок рынка (' + esc(err.message) +
182
- '). Живые данные — на <a href="https://dfo.spaceghosts.com/#vendors" target="_blank" rel="noopener">dfo.spaceghosts.com</a>.</p>';
183
- });
 
 
 
 
 
 
 
 
 
 
 
 
184
  })();
 
165
  if (lot) lot.classList.toggle('open');
166
  });
167
 
168
+ var SOURCES = [
169
+ 'assets/vendors.json',
170
+ 'https://dfo.spaceghosts.com/vendors.json',
171
+ 'https://api.allorigins.win/raw?url=' + encodeURIComponent('https://dfo.spaceghosts.com/vendors.json'),
172
+ 'https://api.codetabs.com/v1/proxy?quest=' + encodeURIComponent('https://dfo.spaceghosts.com/vendors.json')
173
+ ];
174
+
175
+ function fetchFirst(urls) {
176
+ var p = Promise.reject(new Error('no sources'));
177
+ urls.forEach(function (u) {
178
+ p = p.catch(function () {
179
+ return fetch(u, { cache: 'no-store' }).then(function (r) {
180
+ if (!r.ok) throw new Error('HTTP ' + r.status);
181
+ return r.json();
182
+ });
183
+ });
184
+ });
185
+ return p;
186
+ }
187
+
188
+ function startData(json, live) {
189
  data = json;
190
  buildControls();
191
  var upd = data.updatedAt ? new Date(data.updatedAt) : null;
192
  document.getElementById('mk-updated').innerHTML =
193
+ (live ? 'Рынок онлайн' : 'Снимок рынка') + ': ' + (upd ? upd.toLocaleString('ru-RU', { day: 'numeric', month: 'long', hour: '2-digit', minute: '2-digit' }) : '—') +
194
+ ' · <button class="mk-refresh" id="mk-refresh" type="button">обновить</button> · данные ' +
195
  '<a href="https://dfo.spaceghosts.com/#vendors" target="_blank" rel="noopener">Daggerfall Watch</a>';
196
+ var rb = document.getElementById('mk-refresh');
197
+ if (rb) rb.addEventListener('click', function () { load(true); });
198
  update();
199
+ if (data.refreshMinutes && !load._t) {
200
+ load._t = setInterval(function () { load(true); }, Math.max(5, data.refreshMinutes) * 60000);
201
+ }
202
+ }
203
+
204
+ function load(manual) {
205
+ var note = document.getElementById('mk-updated');
206
+ if (manual && note) note.textContent = 'Обновляю рынок…';
207
+ fetchFirst(SOURCES)
208
+ .then(function (json) { startData(json, true); })
209
+ .catch(function (err) {
210
+ listEl.innerHTML = '<p class="note">Не удалось загрузить рынок (' + esc(err.message) +
211
+ '). Живые данные — на <a href="https://dfo.spaceghosts.com/#vendors" target="_blank" rel="noopener">dfo.spaceghosts.com</a>.</p>';
212
+ });
213
+ }
214
+
215
+ load(false);
216
  })();