1
0
mirror of https://github.com/TeamPiped/Piped.git synced 2024-12-15 14:50:28 +05:30
Piped/src/main.js

255 lines
7.6 KiB
JavaScript
Raw Normal View History

import { createApp } from "vue";
import { library } from "@fortawesome/fontawesome-svg-core";
2021-07-04 01:03:59 +05:30
import {
faThumbsUp,
faThumbsDown,
faEye,
faThumbtack,
faCheck,
faHeart,
faHeadphones,
2021-07-05 00:27:57 +05:30
faRss,
faChevronLeft,
2021-11-02 01:53:04 +05:30
faLevelDownAlt,
faTv,
2021-07-04 01:03:59 +05:30
} from "@fortawesome/free-solid-svg-icons";
2021-07-01 01:56:04 +05:30
import { faGithub, faBitcoin, faYoutube } from "@fortawesome/free-brands-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome";
2021-07-04 01:03:59 +05:30
library.add(
faThumbsUp,
faThumbsDown,
faEye,
faGithub,
faBitcoin,
faThumbtack,
faCheck,
faHeart,
faHeadphones,
faYoutube,
2021-07-05 00:27:57 +05:30
faRss,
faChevronLeft,
2021-11-02 01:53:04 +05:30
faLevelDownAlt,
faTv,
2021-07-04 01:03:59 +05:30
);
2020-11-17 10:45:35 +05:30
import("uikit/dist/css/uikit-core.css");
2020-11-17 10:45:35 +05:30
import router from "@/router/router";
import App from "./App.vue";
2020-11-17 10:45:35 +05:30
2021-07-04 01:03:59 +05:30
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";
const timeAgo = new TimeAgo("en-US");
2021-05-10 23:44:28 +05:30
import("./registerServiceWorker");
2020-11-17 10:45:35 +05:30
2020-11-27 12:16:36 +05:30
const mixin = {
methods: {
2021-07-04 01:03:59 +05:30
timeFormat: function(duration) {
var pad = function(num, size) {
2020-11-27 12:16:36 +05:30
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 13:33:10 +05:30
if (hours > 0) str += hours + ":";
2020-11-27 12:16:36 +05:30
str += pad(minutes, 2) + ":" + pad(seconds, 2);
return str;
2021-02-24 15:05:41 +05:30
},
2021-05-31 03:36:35 +05:30
numberFormat(num) {
const digits = 2;
const si = [
{ value: 1, symbol: "" },
2021-07-04 01:03:59 +05:30
{ value: 1e3, symbol: "k" },
{ value: 1e6, symbol: "M" },
{ value: 1e9, symbol: "B" },
2021-05-31 03:36:35 +05:30
];
const rx = /\.0+$|(\.[0-9]*[1-9])0+$/;
for (var i = si.length - 1; i > 0; i--) {
if (num >= si[i].value) {
break;
}
}
return (num / si[i].value).toFixed(digits).replace(rx, "$1") + si[i].symbol;
},
2021-06-03 00:22:58 +05:30
addCommas(num) {
2021-07-04 01:03:59 +05:30
num = parseInt(num);
return num.toLocaleString("en-US");
2021-06-03 00:22:58 +05:30
},
2021-07-04 01:03:59 +05:30
fetchJson: function(url, params, options) {
if (params) {
url = new URL(url);
2021-07-04 01:03:59 +05:30
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);
},
setPreference(key, value) {
2021-07-04 01:03:59 +05:30
if (localStorage) localStorage.setItem(key, value);
},
getPreferenceBoolean(key, defaultVal) {
var value;
2021-07-04 01:03:59 +05:30
if (
(value = new URLSearchParams(window.location.search).get(key)) !== null ||
(this.testLocalStorage && (value = localStorage.getItem(key)) !== null)
2021-07-04 01:03:59 +05:30
) {
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-04 01:03:59 +05:30
if (
(value = new URLSearchParams(window.location.search).get(key)) !== null ||
(this.testLocalStorage && (value = localStorage.getItem(key)) !== null)
2021-07-04 01:03:59 +05:30
) {
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");
},
2021-07-06 00:03:00 +05:30
getEffectiveTheme() {
var theme = this.getPreferenceString("theme", "dark");
if (theme === "auto")
theme =
window.matchMedia && window.matchMedia("(prefers-color-scheme: dark)").matches ? "dark" : "light";
return theme;
},
getAuthToken() {
return this.getPreferenceString("authToken" + this.hashCode(this.apiUrl()));
},
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) {
const regex = /(((https?:\/\/)|(www\.))[^\s]+)/g;
if (!string) return "";
return string.replace(regex, url => {
return `<a class="uk-button uk-button-text" href="${url}" target="_blank">${url}</a>`;
});
},
async updateWatched(videos) {
if (window.db) {
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;
}
};
});
}
},
},
computed: {
backgroundColor() {
2021-07-06 00:03:00 +05:30
return this.getEffectiveTheme() === "light" ? "#fff" : "#0b0e0f";
},
secondaryBackgroundColor() {
2021-07-06 00:03:00 +05:30
return this.getEffectiveTheme() === "light" ? "#e5e5e5" : "#242727";
},
foregroundColor() {
2021-07-06 00:03:00 +05:30
return this.getEffectiveTheme() === "light" ? "#15191a" : "#0b0e0f";
},
secondaryForegroundColor() {
2021-07-06 00:03:00 +05:30
return this.getEffectiveTheme() === "light" ? "#666" : "#393d3d";
},
darkMode() {
2021-07-06 00:03:00 +05:30
return this.getEffectiveTheme() !== "light";
2021-07-04 01:03:59 +05:30
},
authenticated(_this) {
return _this.getAuthToken() !== undefined;
},
testLocalStorage() {
try {
if (window.localStorage !== undefined) localStorage;
return true;
} catch {
return false;
}
},
2021-09-24 04:27:26 +05:30
defaultLangage() {
const languages = window.navigator.languages;
for (let i = 0; i < languages.length; i++) {
try {
require("@/locales/" + languages[i] + ".json");
return languages[i];
} catch {
continue;
}
}
return "en";
},
2021-07-04 01:03:59 +05:30
},
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");