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

297 lines
6.9 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';
2021-02-17 07:01:08 +01:00
2020-02-27 13:45:29 +01:00
/**
* Helpers
*/
var helpers = {};
2021-02-17 07:01:08 +01:00
2020-02-27 13:45:29 +01:00
/**
* 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.textContent = chrome.i18n.getMessage(i18nMessageName);
2020-06-03 17:50:24 +02:00
}
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 = [
2021-10-13 17:48:23 +02:00
'ar',
'bg',
'ca',
'cs',
'da',
'de',
'el',
'en',
'en_CA',
'en_US',
'eo',
'es',
'et',
2021-11-08 06:17:05 +01:00
'fa',
2021-10-13 17:48:23 +02:00
'fi',
'fr',
'he',
'hr',
'hu',
'id',
'is',
'it',
'ja',
'kn',
'ko',
'lb',
'lt',
'ml',
'nb_NO',
'nl',
'pl',
'pt',
'pt_BR',
'pt_PT',
'ro',
'ru',
'sco',
'si',
'sk',
'sl',
'sr',
'sv',
2022-05-31 06:05:26 +02:00
'te',
2021-10-13 17:48:23 +02:00
'tl',
'tr',
'uk',
2021-11-08 06:17:20 +01:00
'vi',
2021-10-13 17:48:23 +02:00
'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) {
2021-01-18 19:05:41 +01:00
return domain.toLowerCase().trim();
2020-02-27 13:45:29 +01:00
};
2020-06-21 07:35:52 +02:00
helpers.extractDomainFromUrl = function (url, normalize) {
2021-11-28 09:28:38 +01: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;
};
2021-02-17 07:01:08 +01:00
helpers.getWildcard = function (initiatorDomain) {
let domain = initiatorDomain.split('.');
2021-01-17 09:52:49 +01:00
if (domain.length > 2) {
domain[0] = '*';
domain = domain.join().replace(/,/g, '.');
return domain;
}
};
helpers.checkAllowlisted = function (domain, list) {
let domainWithoutPrefix, wildcard;
2021-01-18 19:05:41 +01:00
2021-07-15 06:59:09 +02:00
if (domain === null) {
return false;
}
2021-01-18 19:05:41 +01:00
if (domain.startsWith(Address.WWW_PREFIX)) {
domainWithoutPrefix = domain.slice(Address.WWW_PREFIX.length);
}
wildcard = helpers.getWildcard(domain);
2021-01-30 06:45:30 +01:00
return Boolean(list[domain] || list[domainWithoutPrefix] || list[wildcard] || list[domainWithoutPrefix]);
2021-01-18 19:05:41 +01:00
};
2020-02-27 13:45:29 +01:00
helpers.extractFilenameFromPath = function (path) {
let pathSegments, filename;
pathSegments = path.split('/');
filename = pathSegments[pathSegments.length - 1];
return filename === '' ? pathSegments[1] : filename;
2020-02-27 13:45:29 +01:00
};
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.determineActiveTab = function () {
return new Promise((resolve) => {
let opt = {
'active': true,
'currentWindow': true
};
chrome.tabs.query(opt, function (tabs) {
if (tabs[0]) {
resolve(tabs[0]);
} else {
opt = {'active': true};
2021-08-02 06:27:02 +02:00
chrome.tabs.query(opt, function () {
resolve(tabs[0]);
});
}
});
});
};
2020-02-27 13:45:29 +01:00
helpers.formatNumber = function (number) {
if (typeof number === 'number') {
return number.toLocaleString();
}
};
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) {
/**
2021-04-28 05:30:25 +02:00
* compareVersion( '1.5.7' , '1.5.8' ) is FALSE
* compareVersion( '1.5.8' , '1.5.7' ) is TRUE
2020-03-20 08:14:31 +01:00
* 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);
2021-02-17 07:01:08 +01:00
if (v1[i] > v2[i]) {
return true;
}
if (v1[i] < v2[i]) {
return false;
}
2020-03-20 08:14:31 +01:00
}
2021-02-17 07:01:08 +01:00
return v1.length === v2.length ? true : v1.length < v2.length ? false : true;
2020-06-23 19:32:53 +02:00
};