From fe1c7a1bf53dd19a40f54b3f0fe2cf7c496979ce Mon Sep 17 00:00:00 2001 From: neobooru <50623835+neobooru@users.noreply.github.com> Date: Tue, 31 Dec 2019 02:13:05 +0100 Subject: [PATCH] Almost done, except for the fact that we can't seem to use async computed properties --- src/popup/App.vue | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/src/popup/App.vue b/src/popup/App.vue index 669dc21..5c0d3a0 100644 --- a/src/popup/App.vue +++ b/src/popup/App.vue @@ -76,7 +76,7 @@ @@ -102,6 +102,7 @@ import { ScrapedPost, ScrapedTag, ScrapeResults } from "neo-scraper"; import SzuruWrapper from "../SzuruWrapper"; import { Post, SzuruError } from "../SzuruTypes"; import { Config, SzuruSiteConfig } from "../Config"; +import axios from "axios"; type MessageType = "error" | "info" | "success"; @@ -332,6 +333,33 @@ export default Vue.extend({ ); } } + }, + formatByteCount(bytes: number, decimals = 2) { + if (bytes === 0) return "0 Bytes"; + + const k = 1024; + const dm = decimals < 0 ? 0 : decimals; + const sizes = ["Bytes", "KiB", "MiB"]; + + const i = Math.floor(Math.log(bytes) / Math.log(k)); + + return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + " " + sizes[i]; + } + }, + computed: { + // Get post tooltip which contains the image size and dimensions + async postTooltip() { + // HACK: Workaround + // Probably has to do with https://github.com/vuejs/vue/issues/8721 + const _this = this as any; + + const img = document.getElementById("imgFull") as HTMLImageElement; + if (!img || !_this.post) return ""; + + const resp = await axios.head(_this.post.imageUrl); + const byteSize = resp.headers["Content-Length"]; + + return `${_this.formatByteCount(byteSize)} (${img.naturalWidth}x${img.naturalHeight})`; } }, async mounted() {