2020-08-13 18:28:47 +02:00
|
|
|
export const URL_STATUS = `/status`;
|
|
|
|
export const URL_CHAT_HISTORY = `/chat`;
|
|
|
|
export const URL_CUSTOM_EMOJIS = `/emoji`;
|
2020-08-20 21:59:07 +02:00
|
|
|
export const URL_CONFIG = `/config`;
|
|
|
|
|
2020-08-13 06:56:41 +02:00
|
|
|
// TODO: This directory is customizable in the config. So we should expose this via the config API.
|
2020-08-13 18:28:47 +02:00
|
|
|
export const URL_STREAM = `/hls/stream.m3u8`;
|
|
|
|
export const URL_WEBSOCKET = `${location.protocol === 'https:' ? 'wss' : 'ws'}://${location.host}/entry`;
|
2020-07-05 10:08:22 +02:00
|
|
|
|
2020-08-21 00:29:15 +02:00
|
|
|
export const TIMER_STATUS_UPDATE = 5000; // ms
|
|
|
|
export const TIMER_DISABLE_CHAT_AFTER_OFFLINE = 5 * 60 * 1000; // 5 mins
|
|
|
|
export const TIMER_STREAM_DURATION_COUNTER = 1000;
|
|
|
|
export const TEMP_IMAGE = 'data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7';
|
|
|
|
|
|
|
|
export const MESSAGE_OFFLINE = 'Stream is offline.';
|
|
|
|
export const MESSAGE_ONLINE = 'Stream is online';
|
|
|
|
|
2020-08-13 10:28:25 +02:00
|
|
|
export const URL_OWNCAST = 'https://github.com/gabek/owncast'; // used in footer
|
2020-07-16 21:17:05 +02:00
|
|
|
|
2020-08-13 10:28:25 +02:00
|
|
|
export function getLocalStorage(key) {
|
2020-06-14 09:24:26 +02:00
|
|
|
try {
|
|
|
|
return localStorage.getItem(key);
|
|
|
|
} catch (e) {
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-08-13 10:28:25 +02:00
|
|
|
export function setLocalStorage(key, value) {
|
2020-06-14 09:24:26 +02:00
|
|
|
try {
|
2020-06-14 10:10:26 +02:00
|
|
|
if (value !== "" && value !== null) {
|
|
|
|
localStorage.setItem(key, value);
|
|
|
|
} else {
|
|
|
|
localStorage.removeItem(key);
|
|
|
|
}
|
2020-06-14 09:24:26 +02:00
|
|
|
return true;
|
|
|
|
} catch (e) {}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-08-13 10:28:25 +02:00
|
|
|
export function clearLocalStorage(key) {
|
2020-06-14 10:10:26 +02:00
|
|
|
localStorage.removeItem(key);
|
|
|
|
}
|
|
|
|
|
2020-06-18 09:06:10 +02:00
|
|
|
// jump down to the max height of a div, with a slight delay
|
2020-08-13 10:28:25 +02:00
|
|
|
export function jumpToBottom(element) {
|
2020-06-18 09:06:10 +02:00
|
|
|
if (!element) return;
|
|
|
|
|
|
|
|
setTimeout(() => {
|
|
|
|
element.scrollTo({
|
|
|
|
top: element.scrollHeight,
|
|
|
|
left: 0,
|
|
|
|
behavior: 'smooth'
|
|
|
|
});
|
|
|
|
}, 50, element);
|
2020-06-14 09:24:26 +02:00
|
|
|
}
|
|
|
|
|
2020-06-18 09:06:10 +02:00
|
|
|
// convert newlines to <br>s
|
2020-08-13 10:28:25 +02:00
|
|
|
export function addNewlines(str) {
|
2020-06-18 09:06:10 +02:00
|
|
|
return str.replace(/(?:\r\n|\r|\n)/g, '<br />');
|
2020-06-15 06:14:42 +02:00
|
|
|
}
|
2020-06-15 22:43:41 +02:00
|
|
|
|
2020-08-13 10:28:25 +02:00
|
|
|
export function pluralize(string, count) {
|
2020-06-18 19:24:54 +02:00
|
|
|
if (count === 1) {
|
|
|
|
return string;
|
|
|
|
} else {
|
|
|
|
return string + "s";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-06-16 00:45:55 +02:00
|
|
|
// Trying to determine if browser is mobile/tablet.
|
|
|
|
// Source: https://developer.mozilla.org/en-US/docs/Web/HTTP/Browser_detection_using_the_user_agent
|
2020-08-13 10:28:25 +02:00
|
|
|
export function hasTouchScreen() {
|
2020-08-21 00:29:15 +02:00
|
|
|
let hasTouch = false;
|
2020-08-13 10:28:25 +02:00
|
|
|
if ("maxTouchPoints" in navigator) {
|
2020-08-21 00:29:15 +02:00
|
|
|
hasTouch = navigator.maxTouchPoints > 0;
|
2020-06-16 00:45:55 +02:00
|
|
|
} else if ("msMaxTouchPoints" in navigator) {
|
2020-08-21 00:29:15 +02:00
|
|
|
hasTouch = navigator.msMaxTouchPoints > 0;
|
2020-06-16 00:45:55 +02:00
|
|
|
} else {
|
|
|
|
var mQ = window.matchMedia && matchMedia("(pointer:coarse)");
|
|
|
|
if (mQ && mQ.media === "(pointer:coarse)") {
|
2020-08-21 00:29:15 +02:00
|
|
|
hasTouch = !!mQ.matches;
|
2020-06-16 00:45:55 +02:00
|
|
|
} else if ('orientation' in window) {
|
2020-08-21 00:29:15 +02:00
|
|
|
hasTouch = true; // deprecated, but good fallback
|
2020-06-16 00:45:55 +02:00
|
|
|
} else {
|
|
|
|
// Only as a last resort, fall back to user agent sniffing
|
|
|
|
var UA = navigator.userAgent;
|
2020-08-21 00:29:15 +02:00
|
|
|
hasTouch = (
|
2020-06-16 00:45:55 +02:00
|
|
|
/\b(BlackBerry|webOS|iPhone|IEMobile)\b/i.test(UA) ||
|
|
|
|
/\b(Android|Windows Phone|iPad|iPod)\b/i.test(UA)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2020-08-21 00:29:15 +02:00
|
|
|
return hasTouch;
|
2020-06-16 00:45:55 +02:00
|
|
|
}
|
2020-06-15 22:43:41 +02:00
|
|
|
|
2020-07-05 07:34:00 +02:00
|
|
|
// generate random avatar from https://robohash.org
|
2020-08-13 10:28:25 +02:00
|
|
|
export function generateAvatar(hash) {
|
2020-07-05 07:34:00 +02:00
|
|
|
const avatarSource = 'https://robohash.org/';
|
|
|
|
const optionSize = '?size=80x80';
|
2020-08-13 10:28:25 +02:00
|
|
|
const optionSet = '&set=set3';
|
2020-07-05 07:34:00 +02:00
|
|
|
const optionBg = ''; // or &bgset=bg1 or bg2
|
|
|
|
|
|
|
|
return avatarSource + hash + optionSize + optionSet + optionBg;
|
|
|
|
}
|
|
|
|
|
2020-08-13 10:28:25 +02:00
|
|
|
export function generateUsername() {
|
2020-07-05 07:34:00 +02:00
|
|
|
return `User ${(Math.floor(Math.random() * 42) + 1)}`;
|
2020-07-05 10:08:22 +02:00
|
|
|
}
|
|
|
|
|
2020-08-13 10:28:25 +02:00
|
|
|
export function secondsToHMMSS(seconds = 0) {
|
2020-07-19 00:06:54 +02:00
|
|
|
const finiteSeconds = Number.isFinite(+seconds) ? Math.abs(seconds) : 0;
|
|
|
|
|
|
|
|
const hours = Math.floor(finiteSeconds / 3600);
|
|
|
|
const hoursString = hours ? `${hours}:` : '';
|
|
|
|
|
|
|
|
const mins = Math.floor((finiteSeconds / 60) % 60);
|
|
|
|
const minString = mins < 10 ? `0${mins}:` : `${mins}:`;
|
|
|
|
|
|
|
|
const secs = Math.floor(finiteSeconds % 60);
|
|
|
|
const secsString = secs < 10 ? `0${secs}` : `${secs}`;
|
|
|
|
|
|
|
|
return hoursString + minString + secsString;
|
2020-07-20 00:17:03 +02:00
|
|
|
}
|
|
|
|
|
2020-08-13 10:28:25 +02:00
|
|
|
export function setVHvar() {
|
2020-07-20 00:12:57 +02:00
|
|
|
var vh = window.innerHeight * 0.01;
|
|
|
|
// Then we set the value in the --vh custom property to the root of the document
|
|
|
|
document.documentElement.style.setProperty('--vh', `${vh}px`);
|
|
|
|
console.log("== new vh", vh)
|
|
|
|
}
|
2020-08-06 19:55:33 +02:00
|
|
|
|
2020-08-13 10:28:25 +02:00
|
|
|
export function doesObjectSupportFunction(object, functionName) {
|
2020-08-06 19:55:33 +02:00
|
|
|
return typeof object[functionName] === "function";
|
2020-08-13 10:28:25 +02:00
|
|
|
}
|