1
0
mirror of https://codeberg.org/nobody/LocalCDN.git synced 2024-12-14 04:14:06 +01:00
LocalCDN-Firefox-Chrome-Brave/modules/internal/helpers.js

212 lines
5.5 KiB
JavaScript
Raw Normal View History

2020-02-27 13:45:29 +01:00
/**
* Internal Helper Module
2020-06-30 18:41:58 +02:00
* Belongs to LocalCDN (since 2020-02-26)
* (Origin: Decentraleyes)
2020-02-27 13:45:29 +01:00
*
* @author Thomas Rientjes
* @since 2017-10-26
2020-04-14 07:43:25 +02:00
*
2020-06-30 18:41:58 +02:00
* @author nobody
2020-04-14 07:43:25 +02:00
* @since 2020-02-26
*
2020-02-27 13:45:29 +01:00
* @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) {
2020-06-21 08:18:18 +02:00
let scriptDirection, i18nElements, translationComplete;
2020-02-27 13:45:29 +01:00
2020-04-07 12:19:32 +02:00
translationComplete = true;
2020-02-27 13:45:29 +01:00
scriptDirection = helpers.determineScriptDirection(navigator.language);
i18nElements = document.querySelectorAll('[data-i18n-content]');
i18nElements.forEach(function (i18nElement) {
let i18nMessageName = i18nElement.getAttribute('data-i18n-content');
2020-08-13 08:07:04 +02:00
if (chrome.i18n.getMessage(i18nMessageName) !== '') {
if (i18nElement.type === 'button') {
2020-06-03 17:50:24 +02:00
i18nElement.value = chrome.i18n.getMessage(i18nMessageName);
} else {
i18nElement.innerText = chrome.i18n.getMessage(i18nMessageName);
}
i18nElement.setAttribute('dir', scriptDirection);
2020-04-07 12:19:32 +02:00
} else {
translationComplete = false;
}
2020-02-27 13:45:29 +01:00
});
2020-04-07 12:19:32 +02:00
return translationComplete;
2020-02-27 13:45:29 +01:00
};
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 = [
2020-11-06 06:27:56 +01:00
'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'
2020-02-27 13:45:29 +01:00
];
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;
};
2020-06-21 07:35:52 +02:00
helpers.extractDomainFromUrl = function (url, normalize) {
2020-05-30 18:00:48 +02:00
if (/^(?!(http[s]?|file):\/\/).*/.test(url)) {
2020-05-29 18:06:49 +02:00
return null;
}
2020-02-27 13:45:29 +01:00
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
2020-08-13 08:07:04 +02:00
let hexValue = (0 ^ (value & (15 >> (0 / 4)))).toString(16);
2020-02-27 13:45:29 +01:00
randomHexString += hexValue;
}
return randomHexString;
};
helpers.determineCdnName = function (domainName) {
2020-11-05 07:07:52 +01:00
return CDNs[domainName] || 'Unknown';
2020-02-27 13:45:29 +01:00
};
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';
}
};
2020-03-16 14:50:13 +01:00
2020-10-23 17:25:26 +02:00
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;
};
2020-03-20 08:14:31 +01:00
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);
2020-08-13 08:07:04 +02:00
for (let i = 0; i < k; ++i) {
2020-03-20 08:14:31 +01:00
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;
}
2020-08-13 08:07:04 +02:00
return v1.length == v2.length ? true : v1.length < v2.length ? false : true;
2020-06-23 19:32:53 +02:00
};