MCPcopy Create free account
hub / github.com/dajie111/nodeseek-userscript / tryFetch

Function tryFetch

nodeImage.js:328–371  ·  view source on GitHub ↗
(url, asJson, next)

Source from the content-addressed store, hash-verified

326 return '';
327 }
328 function tryFetch(url, asJson, next) {
329 GM_xmlhttpRequest({
330 method: 'GET',
331 url: url,
332 headers: {
333 Referer: refWww,
334 Accept: asJson ? 'application/json' : 'text/html,*/*',
335 },
336 timeout: 20000,
337 anonymous: false,
338 onload: function (r) {
339 var text = r.responseText || '';
340 if (asJson) {
341 var body;
342 try {
343 body = text ? JSON.parse(text) : null;
344 } catch (e) {
345 body = null;
346 }
347 var key = extractApiKeyFromJson(body);
348 if (key) {
349 onDone(key, '');
350 return;
351 }
352 } else {
353 var keyHtml = extractApiKeyFromHtml(text);
354 if (keyHtml) {
355 onDone(keyHtml, '');
356 return;
357 }
358 }
359 if (typeof next === 'function') next();
360 else onDone('', parseLastMsg(text, asJson));
361 },
362 onerror: function () {
363 if (typeof next === 'function') next();
364 else onDone('', '');
365 },
366 ontimeout: function () {
367 if (typeof next === 'function') next();
368 else onDone('', '');
369 },
370 });
371 }
372 tryFetch(WWW_BASE + '/api/user/api-key', true, function () {
373 tryFetch(API_BASE + '/api/user/api-key', true, function () {
374 tryFetch(refWww, false, null);

Callers 1

pullApiKeyWithGmXhrFunction · 0.85

Calls 3

extractApiKeyFromJsonFunction · 0.85
extractApiKeyFromHtmlFunction · 0.85
parseLastMsgFunction · 0.85

Tested by

no test coverage detected