LocalCDN-Firefox-Chrome-Brave/modules/internal/helpers.js

212 lines
5.5 KiB
JavaScript

/**
* Internal Helper Module
* Belongs to LocalCDN (since 2020-02-26)
* (Origin: Decentraleyes)
*
* @author Thomas Rientjes
* @since 2017-10-26
*
* @author nobody
* @since 2020-02-26
*
* @license MPL 2.0
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/.
*/
'use strict';
/**
* Helpers
*/
var helpers = {};
/**
* Public Methods
*/
helpers.insertI18nContentIntoDocument = function (document) {
let scriptDirection, i18nElements, translationComplete;
translationComplete = true;
scriptDirection = helpers.determineScriptDirection(navigator.language);
i18nElements = document.querySelectorAll('[data-i18n-content]');
i18nElements.forEach(function (i18nElement) {
let i18nMessageName = i18nElement.getAttribute('data-i18n-content');
if (chrome.i18n.getMessage(i18nMessageName) !== '') {
if (i18nElement.type === 'button') {
i18nElement.value = chrome.i18n.getMessage(i18nMessageName);
} else {
i18nElement.innerText = chrome.i18n.getMessage(i18nMessageName);
}
i18nElement.setAttribute('dir', scriptDirection);
} else {
translationComplete = false;
}
});
return translationComplete;
};
helpers.insertI18nTitlesIntoDocument = function (document) {
let scriptDirection, i18nElements;
scriptDirection = helpers.determineScriptDirection(navigator.language);
i18nElements = document.querySelectorAll('[data-i18n-title]');
i18nElements.forEach(function (i18nElement) {
let i18nMessageName = i18nElement.getAttribute('data-i18n-title');
i18nElement.setAttribute('title', chrome.i18n.getMessage(i18nMessageName));
i18nElement.setAttribute('dir', scriptDirection);
});
};
helpers.languageIsFullySupported = function (language) {
let languageSupported, supportedLanguages;
languageSupported = false;
supportedLanguages = [
'ar','bg','cs','da','de','el','en','en_CA','en_US','eo','es','et','fi',
'fr','fr_CA','he','hr','hu','id','is','it','ja','kn','ko','lb','lt',
'nb_NO','nl','pl','pt','pt_BR','pt_PT','ro','ru','sl','sr','sv','tl',
'tr','zh_Hans','zh_Hant'
];
for (let supportedLanguage of supportedLanguages) {
if (language.search(supportedLanguage) !== -1) {
languageSupported = true;
}
}
return languageSupported;
};
helpers.normalizeDomain = function (domain) {
domain = domain.toLowerCase().trim();
if (domain.startsWith(Address.WWW_PREFIX)) {
domain = domain.slice(Address.WWW_PREFIX.length);
}
return domain;
};
helpers.extractDomainFromUrl = function (url, normalize) {
if (/^(?!(http[s]?|file):\/\/).*/.test(url)) {
return null;
}
let extractedDomain;
try {
extractedDomain = new URL(url).host;
} catch (exception) {
extractedDomain = null;
}
if (url.startsWith(Address.CHROME)) {
extractedDomain = null;
}
if (extractedDomain === '') {
extractedDomain = null;
}
if (extractedDomain !== null && normalize === true) {
extractedDomain = helpers.normalizeDomain(extractedDomain);
}
return extractedDomain;
};
helpers.extractFilenameFromPath = function (path) {
let pathSegments, filename;
pathSegments = path.split('/');
filename = pathSegments[pathSegments.length - 1];
return filename;
};
helpers.generateRandomHexString = function (length) {
let randomValues, randomHexString;
randomValues = crypto.getRandomValues(new Uint8Array(length));
randomHexString = '';
for (let value of randomValues) {
// eslint-disable-next-line no-bitwise
let hexValue = (0 ^ (value & (15 >> (0 / 4)))).toString(16);
randomHexString += hexValue;
}
return randomHexString;
};
helpers.determineCdnName = function (domainName) {
return CDNs[domainName] || 'Unknown';
};
helpers.determineScriptDirection = function (language) {
let rightToLeftLanguages, scriptDirection;
rightToLeftLanguages = ['ar', 'he'];
if (rightToLeftLanguages.indexOf(language) === -1) {
scriptDirection = 'ltr';
} else {
scriptDirection = 'rtl';
}
return scriptDirection;
};
helpers.formatNumber = function (number) {
if (typeof number === 'number') {
return number.toLocaleString();
}
};
helpers.formatVersion = function (version) {
if (version.indexOf('beta') === -1) {
return version;
} else {
return 'BETA';
}
};
helpers.formatFilename = function (targetPath) {
if (targetPath.startsWith('resources/element-ui/')) {
targetPath = targetPath.toLowerCase();
if (!targetPath.endsWith('.min.jsm')) {
targetPath = targetPath.replace('.jsm', '.min.jsm');
}
}
return targetPath;
};
helpers.compareVersion = function (v1, v2) {
/**
* compareVersion( '1.5.7' , '1.5.8' ) is TRUE
* compareVersion( '1.5.8' , '1.5.7' ) is FALSE
* compareVersion( '1.5.7' , '1.5.7' ) is TRUE
*/
v1 = v1.split('.');
v2 = v2.split('.');
const k = Math.min(v1.length, v2.length);
for (let i = 0; i < k; ++i) {
v1[i] = parseInt(v1[i], 10);
v2[i] = parseInt(v2[i], 10);
if (v1[i] > v2[i]) return true;
if (v1[i] < v2[i]) return false;
}
return v1.length == v2.length ? true : v1.length < v2.length ? false : true;
};