Piped/src/main.js

306 lines
9.2 KiB
JavaScript
Raw Normal View History

import { createApp } from "vue";
import { library } from "@fortawesome/fontawesome-svg-core";
2021-07-03 19:33:59 +00:00
import {
faEye,
faThumbtack,
faCheck,
faHeart,
faHeadphones,
2021-07-04 18:57:57 +00:00
faRss,
faChevronLeft,
2021-11-01 20:23:04 +00:00
faLevelDownAlt,
faTv,
faLevelUpAlt,
faBroadcastTower,
2022-04-07 02:33:25 +00:00
faCirclePlus,
faCircleMinus,
faXmark,
2022-07-21 20:51:41 +00:00
faClone,
2022-08-28 14:29:11 +00:00
faShare,
2022-09-09 18:13:56 +00:00
faBook,
faServer,
faDonate,
2023-01-06 18:30:28 +00:00
faBookmark,
2021-07-03 19:33:59 +00:00
} from "@fortawesome/free-solid-svg-icons";
2022-11-02 16:10:27 +00:00
import { faGithub, faBitcoin, faYoutube } from "@fortawesome/free-brands-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome";
2021-07-03 19:33:59 +00:00
library.add(
faEye,
faGithub,
faBitcoin,
faThumbtack,
faCheck,
faHeart,
faHeadphones,
2022-11-02 16:10:27 +00:00
faYoutube,
2021-07-04 18:57:57 +00:00
faRss,
faChevronLeft,
2021-11-01 20:23:04 +00:00
faLevelDownAlt,
faLevelUpAlt,
faTv,
faBroadcastTower,
2022-04-07 02:33:25 +00:00
faCirclePlus,
faCircleMinus,
faXmark,
2022-07-21 20:51:41 +00:00
faClone,
2022-08-28 14:29:11 +00:00
faShare,
2022-09-09 18:13:56 +00:00
faBook,
faServer,
faDonate,
2023-01-06 18:30:28 +00:00
faBookmark,
2021-07-03 19:33:59 +00:00
);
2020-11-17 05:15:35 +00:00
2021-12-28 14:39:20 +00:00
import router from "@/router/router.js";
import App from "./App.vue";
2020-11-17 05:15:35 +00:00
2021-07-03 19:33:59 +00:00
import DOMPurify from "dompurify";
import TimeAgo from "javascript-time-ago";
import en from "javascript-time-ago/locale/en";
TimeAgo.addDefaultLocale(en);
import { createI18n } from "vue-i18n";
import enLocale from "@/locales/en.json";
2022-08-17 13:34:57 +00:00
import "@unocss/reset/tailwind.css";
import "uno.css";
const timeAgo = new TimeAgo("en-US");
2021-05-10 18:14:28 +00:00
import("./registerServiceWorker");
2020-11-17 05:15:35 +00:00
2020-11-27 06:46:36 +00:00
const mixin = {
methods: {
timeFormat: function (duration) {
var pad = function (num, size) {
2020-11-27 06:46:36 +00:00
return ("000" + num).slice(size * -1);
};
var time = parseFloat(duration).toFixed(3),
hours = Math.floor(time / 60 / 60),
minutes = Math.floor(time / 60) % 60,
seconds = Math.floor(time - minutes * 60);
var str = "";
2021-01-07 08:03:10 +00:00
if (hours > 0) str += hours + ":";
2020-11-27 06:46:36 +00:00
str += pad(minutes, 2) + ":" + pad(seconds, 2);
return str;
2021-02-24 09:35:41 +00:00
},
2021-05-30 22:06:35 +00:00
numberFormat(num) {
var loc = `${this.getPreferenceString("hl")}-${this.getPreferenceString("region")}`;
try {
Intl.getCanonicalLocales(loc);
} catch {
loc = undefined;
}
const formatter = Intl.NumberFormat(loc, {
notation: "compact",
});
return formatter.format(num);
2021-05-30 22:06:35 +00:00
},
2021-06-02 18:52:58 +00:00
addCommas(num) {
2021-07-03 19:33:59 +00:00
num = parseInt(num);
return num.toLocaleString("en-US");
2021-06-02 18:52:58 +00:00
},
fetchJson: function (url, params, options) {
if (params) {
url = new URL(url);
2021-07-03 19:33:59 +00:00
for (var param in params) url.searchParams.set(param, params[param]);
}
return fetch(url, options).then(response => {
return response.json();
});
},
purifyHTML(original) {
return DOMPurify.sanitize(original);
},
2022-11-16 18:51:56 +00:00
setPreference(key, value, disableAlert = false) {
try {
localStorage.setItem(key, value);
} catch {
2022-11-16 18:51:56 +00:00
if (!disableAlert) alert(this.$t("info.local_storage"));
}
},
getPreferenceBoolean(key, defaultVal) {
var value;
2021-07-03 19:33:59 +00:00
if (
(value = new URLSearchParams(window.location.search).get(key)) !== null ||
(this.testLocalStorage && (value = localStorage.getItem(key)) !== null)
2021-07-03 19:33:59 +00:00
) {
switch (String(value).toLowerCase()) {
case "true":
case "1":
case "on":
case "yes":
return true;
default:
return false;
}
} else return defaultVal;
},
getPreferenceString(key, defaultVal) {
var value;
2021-07-03 19:33:59 +00:00
if (
(value = new URLSearchParams(window.location.search).get(key)) !== null ||
(this.testLocalStorage && (value = localStorage.getItem(key)) !== null)
2021-07-03 19:33:59 +00:00
) {
return value;
} else return defaultVal;
},
getPreferenceNumber(key, defaultVal) {
var value;
if (
(value = new URLSearchParams(window.location.search).get(key)) !== null ||
(this.testLocalStorage && (value = localStorage.getItem(key)) !== null)
) {
return Number(value);
} else return defaultVal;
},
apiUrl() {
return this.getPreferenceString("instance", "https://pipedapi.kavin.rocks");
},
authApiUrl() {
if (this.getPreferenceBoolean("authInstance", false)) {
return this.getPreferenceString("auth_instance_url", this.apiUrl());
} else return this.apiUrl();
},
getAuthToken() {
return this.getPreferenceString("authToken" + this.hashCode(this.authApiUrl()));
},
hashCode(s) {
return s.split("").reduce(function (a, b) {
a = (a << 5) - a + b.charCodeAt(0);
return a & a;
}, 0);
},
timeAgo(time) {
return timeAgo.format(time);
},
urlify(string) {
if (!string) return "";
const urlRegex = /(((https?:\/\/)|(www\.))[^\s]+)/g;
const emailRegex = /([\w-\\.]+@(?:[\w-]+\.)+[\w-]{2,4})/g;
return string
.replace(urlRegex, url => {
2022-10-06 08:54:01 +00:00
if (url.endsWith("</a>") || url.endsWith("<a")) return url;
return `<a href="${url}" target="_blank">${url}</a>`;
})
.replace(emailRegex, email => {
return `<a href="mailto:${email}">${email}</a>`;
});
},
async updateWatched(videos) {
if (window.db && this.getPreferenceBoolean("watchHistory", false)) {
var tx = window.db.transaction("watch_history", "readonly");
var store = tx.objectStore("watch_history");
videos.map(async video => {
var request = store.get(video.url.substr(-11));
request.onsuccess = function (event) {
if (event.target.result) {
video.watched = true;
}
};
});
}
},
getLocalSubscriptions() {
try {
return JSON.parse(localStorage.getItem("localSubscriptions"));
} catch {
return [];
}
},
isSubscribedLocally(channelId) {
const localSubscriptions = this.getLocalSubscriptions();
if (localSubscriptions == null) return false;
return localSubscriptions.includes(channelId);
},
handleLocalSubscriptions(channelId) {
var localSubscriptions = this.getLocalSubscriptions() ?? [];
if (localSubscriptions.includes(channelId))
2023-01-10 18:30:16 +00:00
localSubscriptions.splice(localSubscriptions.indexOf(channelId), 1);
else localSubscriptions.push(channelId);
// Sort for better cache hits
localSubscriptions.sort();
try {
localStorage.setItem("localSubscriptions", JSON.stringify(localSubscriptions));
return true;
} catch {
alert(this.$t("info.local_storage"));
}
return false;
},
getUnauthenticatedChannels() {
2022-08-04 06:08:04 +00:00
const localSubscriptions = this.getLocalSubscriptions() ?? [];
return localSubscriptions.join(",");
},
2022-08-02 08:14:03 +00:00
/* generate a temporary file and ask the user to download it */
download(text, filename, type) {
var file = new Blob([text], { type: type });
const elem = document.createElement("a");
elem.href = URL.createObjectURL(file);
elem.download = filename;
elem.click();
elem.remove();
2022-08-02 08:08:26 +00:00
},
},
computed: {
authenticated(_this) {
return _this.getAuthToken() !== undefined;
},
testLocalStorage() {
try {
if (window.localStorage !== undefined) localStorage;
return true;
} catch {
return false;
}
},
async defaultLanguage() {
2021-09-23 22:57:26 +00:00
const languages = window.navigator.languages;
for (let i = 0; i < languages.length; i++) {
try {
await import(`./locales/${languages[i]}.json`);
2021-09-23 22:57:26 +00:00
return languages[i];
} catch {
continue;
}
}
return "en";
},
2021-07-03 19:33:59 +00:00
},
data() {
return {
TimeAgo: TimeAgo,
TimeAgoConfig: timeAgo,
};
},
};
const i18n = createI18n({
globalInjection: true,
legacy: false,
locale: "en",
fallbackLocale: "en",
messages: {
en: enLocale,
},
});
window.i18n = i18n;
const app = createApp(App);
app.use(i18n);
app.use(router);
app.mixin(mixin);
app.component("FontAwesomeIcon", FontAwesomeIcon);
app.mount("#app");