2022-05-05 21:46:59 -04:00
|
|
|
'use strict';
|
|
|
|
// Contains only auxiliary methods
|
|
|
|
// May be included and executed unlimited number of times without any consequences
|
|
|
|
|
|
|
|
// Polyfills for IE11
|
|
|
|
Array.prototype.find = Array.prototype.find || function (condition) {
|
|
|
|
return this.filter(condition)[0];
|
|
|
|
};
|
|
|
|
Array.from = Array.from || function (source) {
|
|
|
|
return Array.prototype.slice.call(source);
|
|
|
|
};
|
|
|
|
NodeList.prototype.forEach = NodeList.prototype.forEach || function (callback) {
|
|
|
|
Array.from(this).forEach(callback);
|
|
|
|
};
|
|
|
|
String.prototype.includes = String.prototype.includes || function (searchString) {
|
|
|
|
return this.indexOf(searchString) >= 0;
|
|
|
|
};
|
|
|
|
String.prototype.startsWith = String.prototype.startsWith || function (prefix) {
|
|
|
|
return this.substr(0, prefix.length) === prefix;
|
|
|
|
};
|
|
|
|
Math.sign = Math.sign || function(x) {
|
|
|
|
x = +x;
|
|
|
|
if (!x) return x; // 0 and NaN
|
|
|
|
return x > 0 ? 1 : -1;
|
|
|
|
};
|
2022-05-16 06:13:00 -04:00
|
|
|
if (!window.hasOwnProperty('HTMLDetailsElement') && !window.hasOwnProperty('mockHTMLDetailsElement')) {
|
|
|
|
window.mockHTMLDetailsElement = true;
|
|
|
|
const style = 'details:not([open]) > :not(summary) {display: none}';
|
|
|
|
document.head.appendChild(document.createElement('style')).textContent = style;
|
|
|
|
|
|
|
|
addEventListener('click', function (e) {
|
|
|
|
if (e.target.nodeName !== 'SUMMARY') return;
|
|
|
|
const details = e.target.parentElement;
|
|
|
|
if (details.hasAttribute('open'))
|
|
|
|
details.removeAttribute('open');
|
|
|
|
else
|
|
|
|
details.setAttribute('open', '');
|
|
|
|
});
|
|
|
|
}
|
2022-05-05 21:46:59 -04:00
|
|
|
|
|
|
|
// Monstrous global variable for handy code
|
2022-05-21 06:35:41 -04:00
|
|
|
// Includes: clamp, xhr, storage.{get,set,remove}
|
2022-05-05 23:16:41 -04:00
|
|
|
window.helpers = window.helpers || {
|
2022-05-05 21:46:59 -04:00
|
|
|
/**
|
|
|
|
* https://en.wikipedia.org/wiki/Clamping_(graphics)
|
|
|
|
* @param {Number} num Source number
|
|
|
|
* @param {Number} min Low border
|
|
|
|
* @param {Number} max High border
|
|
|
|
* @returns {Number} Clamped value
|
|
|
|
*/
|
|
|
|
clamp: function (num, min, max) {
|
|
|
|
if (max < min) {
|
|
|
|
var t = max; max = min; min = t; // swap max and min
|
|
|
|
}
|
|
|
|
|
2022-05-05 23:16:41 -04:00
|
|
|
if (max < num)
|
2022-05-05 21:46:59 -04:00
|
|
|
return max;
|
2022-05-05 23:16:41 -04:00
|
|
|
if (min > num)
|
2022-05-05 21:46:59 -04:00
|
|
|
return min;
|
|
|
|
return num;
|
|
|
|
},
|
|
|
|
|
|
|
|
/** @private */
|
|
|
|
_xhr: function (method, url, options, callbacks) {
|
|
|
|
const xhr = new XMLHttpRequest();
|
|
|
|
xhr.open(method, url);
|
|
|
|
|
|
|
|
// Default options
|
|
|
|
xhr.responseType = 'json';
|
|
|
|
xhr.timeout = 10000;
|
|
|
|
// Default options redefining
|
|
|
|
if (options.responseType)
|
|
|
|
xhr.responseType = options.responseType;
|
|
|
|
if (options.timeout)
|
|
|
|
xhr.timeout = options.timeout;
|
|
|
|
|
|
|
|
if (method === 'POST')
|
|
|
|
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
|
|
|
|
|
2022-05-05 23:16:41 -04:00
|
|
|
// better than onreadystatechange because of 404 codes https://stackoverflow.com/a/36182963
|
|
|
|
xhr.onloadend = function () {
|
|
|
|
if (xhr.status === 200) {
|
2022-05-16 06:13:00 -04:00
|
|
|
if (callbacks.on200) {
|
|
|
|
// fix for IE11. It doesn't convert response to JSON
|
|
|
|
if (xhr.responseType === '' && typeof(xhr.response) === 'string')
|
|
|
|
callbacks.on200(JSON.parse(xhr.response));
|
|
|
|
else
|
|
|
|
callbacks.on200(xhr.response);
|
|
|
|
}
|
2022-05-05 23:16:41 -04:00
|
|
|
} else {
|
|
|
|
// handled by onerror
|
|
|
|
if (xhr.status === 0) return;
|
|
|
|
|
|
|
|
if (callbacks.onNon200)
|
|
|
|
callbacks.onNon200(xhr);
|
2022-05-05 21:46:59 -04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.ontimeout = function () {
|
|
|
|
if (callbacks.onTimeout)
|
|
|
|
callbacks.onTimeout(xhr);
|
|
|
|
};
|
|
|
|
|
|
|
|
xhr.onerror = function () {
|
|
|
|
if (callbacks.onError)
|
|
|
|
callbacks.onError(xhr);
|
|
|
|
};
|
|
|
|
|
|
|
|
if (options.payload)
|
|
|
|
xhr.send(options.payload);
|
|
|
|
else
|
|
|
|
xhr.send();
|
|
|
|
},
|
|
|
|
/** @private */
|
2022-05-05 23:16:41 -04:00
|
|
|
_xhrRetry: function(method, url, options, callbacks) {
|
2022-05-05 21:46:59 -04:00
|
|
|
if (options.retries <= 0) {
|
|
|
|
console.warn('Failed to pull', options.entity_name);
|
|
|
|
if (callbacks.onTotalFail)
|
|
|
|
callbacks.onTotalFail();
|
|
|
|
return;
|
|
|
|
}
|
2022-05-05 23:16:41 -04:00
|
|
|
helpers._xhr(method, url, options, callbacks);
|
2022-05-05 21:46:59 -04:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* @callback callbackXhrOn200
|
|
|
|
* @param {Object} response - xhr.response
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @callback callbackXhrError
|
|
|
|
* @param {XMLHttpRequest} xhr
|
|
|
|
*/
|
|
|
|
/**
|
|
|
|
* @param {'GET'|'POST'} method - 'GET' or 'POST'
|
|
|
|
* @param {String} url - URL to send request to
|
|
|
|
* @param {Object} options - other XHR options
|
|
|
|
* @param {XMLHttpRequestBodyInit} [options.payload=null] - payload for POST-requests
|
|
|
|
* @param {'arraybuffer'|'blob'|'document'|'json'|'text'} [options.responseType=json]
|
|
|
|
* @param {Number} [options.timeout=10000]
|
|
|
|
* @param {Number} [options.retries=1]
|
|
|
|
* @param {String} [options.entity_name='unknown'] - string to log
|
|
|
|
* @param {Number} [options.retry_timeout=1000]
|
|
|
|
* @param {Object} callbacks - functions to execute on events fired
|
|
|
|
* @param {callbackXhrOn200} [callbacks.on200]
|
|
|
|
* @param {callbackXhrError} [callbacks.onNon200]
|
|
|
|
* @param {callbackXhrError} [callbacks.onTimeout]
|
|
|
|
* @param {callbackXhrError} [callbacks.onError]
|
|
|
|
* @param {callbackXhrError} [callbacks.onTotalFail] - if failed after all retries
|
|
|
|
*/
|
2022-05-05 23:16:41 -04:00
|
|
|
xhr: function(method, url, options, callbacks) {
|
|
|
|
if (!options.retries || options.retries <= 1) {
|
2022-05-05 21:46:59 -04:00
|
|
|
helpers._xhr(method, url, options, callbacks);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!options.entity_name) options.entity_name = 'unknown';
|
2022-05-05 23:16:41 -04:00
|
|
|
if (!options.retry_timeout) options.retry_timeout = 1000;
|
2022-05-05 21:46:59 -04:00
|
|
|
const retries_total = options.retries;
|
2022-05-05 23:16:41 -04:00
|
|
|
let currentTry = 1;
|
2022-05-05 21:46:59 -04:00
|
|
|
|
|
|
|
const retry = function () {
|
2022-05-05 23:16:41 -04:00
|
|
|
console.warn('Pulling ' + options.entity_name + ' failed... ' + (currentTry++) + '/' + retries_total);
|
2022-05-05 21:46:59 -04:00
|
|
|
setTimeout(function () {
|
|
|
|
options.retries--;
|
|
|
|
helpers._xhrRetry(method, url, options, callbacks);
|
|
|
|
}, options.retry_timeout);
|
|
|
|
};
|
|
|
|
|
2022-05-21 06:35:41 -04:00
|
|
|
// Pack retry() call into error handlers
|
2022-05-15 01:38:46 -04:00
|
|
|
callbacks._onError = callbacks.onError;
|
2022-05-05 21:46:59 -04:00
|
|
|
callbacks.onError = function (xhr) {
|
|
|
|
if (callbacks._onError)
|
2022-05-21 06:35:41 -04:00
|
|
|
callbacks._onError(xhr);
|
2022-05-05 21:46:59 -04:00
|
|
|
retry();
|
|
|
|
};
|
2022-05-15 01:38:46 -04:00
|
|
|
callbacks._onTimeout = callbacks.onTimeout;
|
2022-05-05 21:46:59 -04:00
|
|
|
callbacks.onTimeout = function (xhr) {
|
|
|
|
if (callbacks._onTimeout)
|
2022-05-21 06:35:41 -04:00
|
|
|
callbacks._onTimeout(xhr);
|
2022-05-05 21:46:59 -04:00
|
|
|
retry();
|
2022-05-21 06:35:41 -04:00
|
|
|
};
|
|
|
|
|
2022-05-05 21:46:59 -04:00
|
|
|
helpers._xhrRetry(method, url, options, callbacks);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @typedef {Object} invidiousStorage
|
2022-05-06 00:42:15 -04:00
|
|
|
* @property {(key:String) => Object} get
|
|
|
|
* @property {(key:String, value:Object)} set
|
|
|
|
* @property {(key:String)} remove
|
2022-05-05 21:46:59 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2022-05-06 00:21:19 -04:00
|
|
|
* Universal storage, stores and returns JS objects. Uses inside localStorage or cookies
|
2022-05-05 21:46:59 -04:00
|
|
|
* @type {invidiousStorage}
|
|
|
|
*/
|
|
|
|
storage: (function () {
|
|
|
|
// access to localStorage throws exception in Tor Browser, so try is needed
|
|
|
|
let localStorageIsUsable = false;
|
|
|
|
try{localStorageIsUsable = !!localStorage.setItem;}catch(e){}
|
|
|
|
|
|
|
|
if (localStorageIsUsable) {
|
|
|
|
return {
|
2022-05-21 06:35:41 -04:00
|
|
|
get: function (key) {
|
|
|
|
if (!localStorage[key]) return;
|
|
|
|
try {
|
|
|
|
return JSON.parse(decodeURIComponent(localStorage[key]));
|
|
|
|
} catch(e) {
|
|
|
|
// Erase non parsable value
|
|
|
|
helpers.storage.remove(key);
|
|
|
|
}
|
|
|
|
},
|
2022-05-06 00:21:19 -04:00
|
|
|
set: function (key, value) { localStorage[key] = encodeURIComponent(JSON.stringify(value)); },
|
2022-05-05 21:46:59 -04:00
|
|
|
remove: function (key) { localStorage.removeItem(key); }
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2022-05-21 06:35:41 -04:00
|
|
|
// TODO: fire 'storage' event for cookies
|
|
|
|
console.info('Storage: localStorage is disabled or unaccessible. Cookies used as fallback');
|
2022-05-05 21:46:59 -04:00
|
|
|
return {
|
|
|
|
get: function (key) {
|
|
|
|
const cookiePrefix = key + '=';
|
|
|
|
function findCallback(cookie) {return cookie.startsWith(cookiePrefix);}
|
2022-05-06 00:21:19 -04:00
|
|
|
const matchedCookie = document.cookie.split('; ').find(findCallback);
|
|
|
|
if (matchedCookie) {
|
|
|
|
const cookieBody = matchedCookie.replace(cookiePrefix, '');
|
|
|
|
if (cookieBody.length === 0) return;
|
2022-05-21 06:35:41 -04:00
|
|
|
try {
|
|
|
|
return JSON.parse(decodeURIComponent(cookieBody));
|
|
|
|
} catch(e) {
|
|
|
|
// Erase non parsable value
|
|
|
|
helpers.storage.remove(key);
|
|
|
|
}
|
2022-05-06 00:21:19 -04:00
|
|
|
}
|
2022-05-05 21:46:59 -04:00
|
|
|
},
|
|
|
|
set: function (key, value) {
|
|
|
|
const cookie_data = encodeURIComponent(JSON.stringify(value));
|
|
|
|
|
|
|
|
// Set expiration in 2 year
|
|
|
|
const date = new Date();
|
2022-05-06 00:21:19 -04:00
|
|
|
date.setFullYear(date.getFullYear()+2);
|
2022-05-05 21:46:59 -04:00
|
|
|
|
2022-05-06 00:21:19 -04:00
|
|
|
document.cookie = key + '=' + cookie_data + '; expires=' + date.toGMTString();
|
2022-05-05 21:46:59 -04:00
|
|
|
},
|
|
|
|
remove: function (key) {
|
|
|
|
document.cookie = key + '=; Max-Age=0';
|
|
|
|
}
|
|
|
|
};
|
|
|
|
})()
|
|
|
|
};
|