/** Function that can be used to wait for a condition before returning. */ async function wait(condition: () => T | false, timeout = 5000, check = 100): Promise { return await new Promise((resolve, reject) => { setTimeout(() => { clearInterval(interval); reject("TIMEOUT"); }, timeout); const intervalCheck = () => { const result = condition(); if (result) { resolve(result); clearInterval(interval); } }; const interval = setInterval(intervalCheck, check); //run the check once first, this speeds it up a lot intervalCheck(); }); } /** * Gets the error message in a nice string * * @param {int} statusCode * @returns {string} errorMessage */ function getErrorMessage(statusCode: number, responseText: string): string { let errorMessage = ""; const postFix = (responseText ? "\n\n" + responseText : ""); if([400, 429, 409, 502, 503, 0].includes(statusCode)) { //treat them the same if (statusCode == 503) statusCode = 502; errorMessage = chrome.i18n.getMessage(statusCode + "") + " " + chrome.i18n.getMessage("errorCode") + statusCode + "\n\n" + chrome.i18n.getMessage("statusReminder"); } else { errorMessage = chrome.i18n.getMessage("connectionError") + statusCode; } return errorMessage + postFix; } /* Gets percieved luminance of a color */ function getLuminance(color: string): number { const {r, g, b} = hexToRgb(color); return Math.sqrt(0.299 * (r * r) + 0.587 * (g * g) + 0.114 * (b * b)); } /* From https://stackoverflow.com/a/5624139 */ function hexToRgb(hex: string): {r: number, g: number, b: number} { // Expand shorthand form (e.g. "03F") to full form (e.g. "0033FF") const shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i; hex = hex.replace(shorthandRegex, function(m, r, g, b) { return r + r + g + g + b + b; }); const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex); return result ? { r: parseInt(result[1], 16), g: parseInt(result[2], 16), b: parseInt(result[3], 16) } : null; } export const GenericUtils = { wait, getErrorMessage, getLuminance }