2019-01-24 00:10:09 +01:00
|
|
|
/* global Android */
|
2019-11-06 05:37:44 +01:00
|
|
|
let html;
|
|
|
|
try {
|
|
|
|
html = require('choo/html');
|
|
|
|
} catch (e) {
|
|
|
|
// running in the service worker
|
|
|
|
}
|
2017-08-31 18:43:36 +02:00
|
|
|
const b64 = require('base64-js');
|
|
|
|
|
|
|
|
function arrayToB64(array) {
|
|
|
|
return b64
|
|
|
|
.fromByteArray(array)
|
|
|
|
.replace(/\+/g, '-')
|
|
|
|
.replace(/\//g, '_')
|
|
|
|
.replace(/=/g, '');
|
2017-06-02 05:59:27 +02:00
|
|
|
}
|
|
|
|
|
2017-08-31 18:43:36 +02:00
|
|
|
function b64ToArray(str) {
|
2018-03-29 07:03:46 +02:00
|
|
|
return b64.toByteArray(str + '==='.slice((str.length + 3) % 4));
|
2017-06-02 05:59:27 +02:00
|
|
|
}
|
|
|
|
|
2019-04-26 22:30:33 +02:00
|
|
|
function locale() {
|
|
|
|
return document.querySelector('html').lang;
|
|
|
|
}
|
|
|
|
|
2022-09-04 12:26:12 +02:00
|
|
|
function loadShim(polyfill) {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const shim = document.createElement('script');
|
|
|
|
shim.src = polyfill;
|
|
|
|
shim.addEventListener('load', () => resolve(true));
|
|
|
|
shim.addEventListener('error', () => resolve(false));
|
|
|
|
document.head.appendChild(shim);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-07-21 01:06:06 +02:00
|
|
|
function isFile(id) {
|
2019-03-26 17:58:04 +01:00
|
|
|
return /^[0-9a-fA-F]{10,16}$/.test(id);
|
2017-07-21 01:06:06 +02:00
|
|
|
}
|
|
|
|
|
2022-09-04 12:26:12 +02:00
|
|
|
function copyToClipboard(str) {
|
|
|
|
const aux = document.createElement('input');
|
|
|
|
aux.setAttribute('value', str);
|
|
|
|
aux.contentEditable = true;
|
|
|
|
aux.readOnly = true;
|
|
|
|
document.body.appendChild(aux);
|
|
|
|
if (navigator.userAgent.match(/iphone|ipad|ipod/i)) {
|
|
|
|
const range = document.createRange();
|
|
|
|
range.selectNodeContents(aux);
|
|
|
|
const sel = getSelection();
|
|
|
|
sel.removeAllRanges();
|
|
|
|
sel.addRange(range);
|
|
|
|
aux.setSelectionRange(0, str.length);
|
|
|
|
} else {
|
|
|
|
aux.select();
|
2017-08-04 05:13:17 +02:00
|
|
|
}
|
2022-09-04 12:26:12 +02:00
|
|
|
const result = document.execCommand('copy');
|
|
|
|
document.body.removeChild(aux);
|
|
|
|
return result;
|
2017-08-04 05:13:17 +02:00
|
|
|
}
|
|
|
|
|
2017-08-06 03:06:43 +02:00
|
|
|
const LOCALIZE_NUMBERS = !!(
|
|
|
|
typeof Intl === 'object' &&
|
|
|
|
Intl &&
|
2017-08-24 23:54:02 +02:00
|
|
|
typeof Intl.NumberFormat === 'function' &&
|
|
|
|
typeof navigator === 'object'
|
2017-08-06 03:06:43 +02:00
|
|
|
);
|
|
|
|
|
2019-03-12 03:28:22 +01:00
|
|
|
const UNITS = ['bytes', 'kb', 'mb', 'gb'];
|
2019-03-01 01:31:37 +01:00
|
|
|
function bytes(num) {
|
2017-08-31 18:43:36 +02:00
|
|
|
if (num < 1) {
|
|
|
|
return '0B';
|
|
|
|
}
|
2017-08-06 03:06:43 +02:00
|
|
|
const exponent = Math.min(Math.floor(Math.log10(num) / 3), UNITS.length - 1);
|
2018-09-24 21:01:39 +02:00
|
|
|
const n = Number(num / Math.pow(1024, exponent));
|
2019-03-01 01:31:37 +01:00
|
|
|
const decimalDigits = Math.floor(n) === n ? 0 : 1;
|
|
|
|
let nStr = n.toFixed(decimalDigits);
|
2017-11-09 22:58:20 +01:00
|
|
|
if (LOCALIZE_NUMBERS) {
|
|
|
|
try {
|
2019-04-26 22:30:33 +02:00
|
|
|
nStr = n.toLocaleString(locale(), {
|
2019-02-22 19:37:52 +01:00
|
|
|
minimumFractionDigits: decimalDigits,
|
|
|
|
maximumFractionDigits: decimalDigits
|
2017-11-09 22:58:20 +01:00
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
2019-03-12 03:28:22 +01:00
|
|
|
return translate('fileSize', {
|
|
|
|
num: nStr,
|
|
|
|
units: translate(UNITS[exponent])
|
|
|
|
});
|
2017-08-06 03:06:43 +02:00
|
|
|
}
|
|
|
|
|
2017-08-09 04:46:15 +02:00
|
|
|
function percent(ratio) {
|
2017-11-09 22:58:20 +01:00
|
|
|
if (LOCALIZE_NUMBERS) {
|
|
|
|
try {
|
2019-04-26 22:30:33 +02:00
|
|
|
return ratio.toLocaleString(locale(), { style: 'percent' });
|
2017-11-09 22:58:20 +01:00
|
|
|
} catch (e) {
|
|
|
|
// fall through
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return `${Math.floor(ratio * 100)}%`;
|
2017-08-09 04:46:15 +02:00
|
|
|
}
|
|
|
|
|
2018-02-08 04:46:18 +01:00
|
|
|
function number(n) {
|
|
|
|
if (LOCALIZE_NUMBERS) {
|
2019-04-26 22:30:33 +02:00
|
|
|
return n.toLocaleString(locale());
|
2018-02-08 04:46:18 +01:00
|
|
|
}
|
|
|
|
return n.toString();
|
|
|
|
}
|
|
|
|
|
2017-08-11 04:01:39 +02:00
|
|
|
function allowedCopy() {
|
|
|
|
const support = !!document.queryCommandSupported;
|
|
|
|
return support ? document.queryCommandSupported('copy') : false;
|
|
|
|
}
|
|
|
|
|
2017-08-24 23:54:02 +02:00
|
|
|
function delay(delay = 100) {
|
|
|
|
return new Promise(resolve => setTimeout(resolve, delay));
|
|
|
|
}
|
|
|
|
|
2018-02-13 20:32:59 +01:00
|
|
|
function fadeOut(selector) {
|
|
|
|
const classes = document.querySelector(selector).classList;
|
|
|
|
classes.remove('effect--fadeIn');
|
|
|
|
classes.add('effect--fadeOut');
|
2017-08-24 23:54:02 +02:00
|
|
|
return delay(300);
|
|
|
|
}
|
|
|
|
|
2018-01-24 19:23:13 +01:00
|
|
|
function openLinksInNewTab(links, should = true) {
|
|
|
|
links = links || Array.from(document.querySelectorAll('a:not([target])'));
|
|
|
|
if (should) {
|
|
|
|
links.forEach(l => {
|
|
|
|
l.setAttribute('target', '_blank');
|
|
|
|
l.setAttribute('rel', 'noopener noreferrer');
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
links.forEach(l => {
|
|
|
|
l.removeAttribute('target');
|
|
|
|
l.removeAttribute('rel');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return links;
|
|
|
|
}
|
2017-07-21 01:06:06 +02:00
|
|
|
|
2018-08-03 21:24:41 +02:00
|
|
|
function browserName() {
|
|
|
|
try {
|
2020-07-31 17:36:27 +02:00
|
|
|
// order of these matters
|
2018-08-03 21:24:41 +02:00
|
|
|
if (/firefox/i.test(navigator.userAgent)) {
|
|
|
|
return 'firefox';
|
2018-07-31 20:09:18 +02:00
|
|
|
}
|
2018-08-03 21:24:41 +02:00
|
|
|
if (/edge/i.test(navigator.userAgent)) {
|
|
|
|
return 'edge';
|
|
|
|
}
|
2020-07-31 17:36:27 +02:00
|
|
|
if (/edg/i.test(navigator.userAgent)) {
|
|
|
|
return 'edgium';
|
|
|
|
}
|
2018-08-03 21:24:41 +02:00
|
|
|
if (/trident/i.test(navigator.userAgent)) {
|
|
|
|
return 'ie';
|
|
|
|
}
|
|
|
|
if (/chrome/i.test(navigator.userAgent)) {
|
|
|
|
return 'chrome';
|
|
|
|
}
|
|
|
|
if (/safari/i.test(navigator.userAgent)) {
|
|
|
|
return 'safari';
|
|
|
|
}
|
2019-01-08 22:50:47 +01:00
|
|
|
if (/send android/i.test(navigator.userAgent)) {
|
|
|
|
return 'android-app';
|
|
|
|
}
|
2018-08-03 21:24:41 +02:00
|
|
|
return 'other';
|
|
|
|
} catch (e) {
|
|
|
|
return 'unknown';
|
2018-07-31 20:09:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-08 00:40:17 +02:00
|
|
|
async function streamToArrayBuffer(stream, size) {
|
|
|
|
const reader = stream.getReader();
|
|
|
|
let state = await reader.read();
|
|
|
|
|
|
|
|
if (size) {
|
|
|
|
const result = new Uint8Array(size);
|
|
|
|
let offset = 0;
|
|
|
|
while (!state.done) {
|
|
|
|
result.set(state.value, offset);
|
|
|
|
offset += state.value.length;
|
|
|
|
state = await reader.read();
|
|
|
|
}
|
|
|
|
return result.buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
const parts = [];
|
|
|
|
let len = 0;
|
|
|
|
while (!state.done) {
|
|
|
|
parts.push(state.value);
|
|
|
|
len += state.value.length;
|
|
|
|
state = await reader.read();
|
|
|
|
}
|
|
|
|
let offset = 0;
|
|
|
|
const result = new Uint8Array(len);
|
|
|
|
for (const part of parts) {
|
|
|
|
result.set(part, offset);
|
|
|
|
offset += part.length;
|
|
|
|
}
|
|
|
|
return result.buffer;
|
|
|
|
}
|
|
|
|
|
2018-10-25 04:07:10 +02:00
|
|
|
function list(items, ulStyle = '', liStyle = '') {
|
2018-11-16 21:30:15 +01:00
|
|
|
const lis = items.map(
|
|
|
|
i =>
|
|
|
|
html`
|
|
|
|
<li class="${liStyle}">${i}</li>
|
|
|
|
`
|
|
|
|
);
|
|
|
|
return html`
|
|
|
|
<ul class="${ulStyle}">
|
|
|
|
${lis}
|
|
|
|
</ul>
|
|
|
|
`;
|
2018-10-25 04:07:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function secondsToL10nId(seconds) {
|
|
|
|
if (seconds < 3600) {
|
|
|
|
return { id: 'timespanMinutes', num: Math.floor(seconds / 60) };
|
|
|
|
} else if (seconds < 86400) {
|
|
|
|
return { id: 'timespanHours', num: Math.floor(seconds / 3600) };
|
|
|
|
} else {
|
|
|
|
return { id: 'timespanDays', num: Math.floor(seconds / 86400) };
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function timeLeft(milliseconds) {
|
2018-11-16 21:30:15 +01:00
|
|
|
if (milliseconds < 1) {
|
|
|
|
return { id: 'linkExpiredAlt' };
|
|
|
|
}
|
2018-10-25 04:07:10 +02:00
|
|
|
const minutes = Math.floor(milliseconds / 1000 / 60);
|
|
|
|
const hours = Math.floor(minutes / 60);
|
|
|
|
const days = Math.floor(hours / 24);
|
|
|
|
if (days >= 1) {
|
|
|
|
return {
|
|
|
|
id: 'expiresDaysHoursMinutes',
|
|
|
|
days,
|
|
|
|
hours: hours % 24,
|
|
|
|
minutes: minutes % 60
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if (hours >= 1) {
|
|
|
|
return {
|
|
|
|
id: 'expiresHoursMinutes',
|
|
|
|
hours,
|
|
|
|
minutes: minutes % 60
|
|
|
|
};
|
|
|
|
} else if (hours === 0) {
|
|
|
|
if (minutes === 0) {
|
|
|
|
return { id: 'expiresMinutes', minutes: '< 1' };
|
|
|
|
}
|
|
|
|
return { id: 'expiresMinutes', minutes };
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2019-01-24 00:10:09 +01:00
|
|
|
function platform() {
|
|
|
|
if (typeof Android === 'object') {
|
|
|
|
return 'android';
|
|
|
|
}
|
|
|
|
return 'web';
|
|
|
|
}
|
|
|
|
|
2019-03-06 19:31:50 +01:00
|
|
|
const ECE_RECORD_SIZE = 1024 * 64;
|
|
|
|
const TAG_LENGTH = 16;
|
|
|
|
function encryptedSize(size, rs = ECE_RECORD_SIZE, tagLength = TAG_LENGTH) {
|
|
|
|
const chunk_meta = tagLength + 1; // Chunk metadata, tag and delimiter
|
|
|
|
return 21 + size + chunk_meta * Math.ceil(size / (rs - chunk_meta));
|
|
|
|
}
|
|
|
|
|
2019-03-12 03:28:22 +01:00
|
|
|
let translate = function() {
|
|
|
|
throw new Error('uninitialized translate function. call setTranslate first');
|
|
|
|
};
|
|
|
|
function setTranslate(t) {
|
|
|
|
translate = t;
|
|
|
|
}
|
|
|
|
|
2017-08-24 23:54:02 +02:00
|
|
|
module.exports = {
|
2019-04-26 22:30:33 +02:00
|
|
|
locale,
|
2017-08-24 23:54:02 +02:00
|
|
|
fadeOut,
|
|
|
|
delay,
|
2017-08-11 04:01:39 +02:00
|
|
|
allowedCopy,
|
2017-08-06 03:06:43 +02:00
|
|
|
bytes,
|
2017-08-09 04:46:15 +02:00
|
|
|
percent,
|
2018-02-08 04:46:18 +01:00
|
|
|
number,
|
2017-08-04 05:13:17 +02:00
|
|
|
copyToClipboard,
|
2017-08-31 18:43:36 +02:00
|
|
|
arrayToB64,
|
|
|
|
b64ToArray,
|
2022-09-04 12:26:12 +02:00
|
|
|
loadShim,
|
2017-07-21 01:06:06 +02:00
|
|
|
isFile,
|
2018-07-31 20:09:18 +02:00
|
|
|
openLinksInNewTab,
|
2018-08-08 00:40:17 +02:00
|
|
|
browserName,
|
2018-10-25 04:07:10 +02:00
|
|
|
streamToArrayBuffer,
|
|
|
|
list,
|
|
|
|
secondsToL10nId,
|
2019-01-24 00:10:09 +01:00
|
|
|
timeLeft,
|
2019-03-06 19:31:50 +01:00
|
|
|
platform,
|
2019-03-12 03:28:22 +01:00
|
|
|
encryptedSize,
|
|
|
|
setTranslate
|
2017-06-02 05:59:27 +02:00
|
|
|
};
|