Change how messages and errors work under the hood
parent
f7849b42c8
commit
f57166fbc0
|
|
@ -1,13 +1,13 @@
|
||||||
import { BooruTypes, ContentType, ScrapedNote, ScrapedPost, ScrapedTag } from "neo-scraper";
|
import { BooruTypes, ContentType, ScrapedNote, ScrapedPost, ScrapedTag } from "neo-scraper";
|
||||||
import { MicroTag, Tag } from "~/api/models";
|
import { ImageSearchResult, MicroTag, Tag } from "~/api/models";
|
||||||
|
|
||||||
export class TagDetails {
|
export class TagDetails {
|
||||||
public implications: TagDetails[] = [];
|
public implications: TagDetails[] = [];
|
||||||
|
|
||||||
constructor(public name: string, public category?: BooruTypes.TagCategory, public usages?: number) {}
|
constructor(public name: string, public category?: string, public usages?: number) {}
|
||||||
|
|
||||||
static fromTag(tag: Tag) {
|
static fromTag(tag: Tag) {
|
||||||
const x = new TagDetails(tag.names[0], <BooruTypes.TagCategory>tag.category, tag.usages);
|
const x = new TagDetails(tag.names[0], tag.category, tag.usages);
|
||||||
|
|
||||||
if (tag.implications) {
|
if (tag.implications) {
|
||||||
x.implications = tag.implications.map((y) => TagDetails.fromMicroTag(y));
|
x.implications = tag.implications.map((y) => TagDetails.fromMicroTag(y));
|
||||||
|
|
@ -17,7 +17,7 @@ export class TagDetails {
|
||||||
}
|
}
|
||||||
|
|
||||||
static fromMicroTag(tag: MicroTag) {
|
static fromMicroTag(tag: MicroTag) {
|
||||||
return new TagDetails(tag.names[0], <BooruTypes.TagCategory>tag.category, tag.usages);
|
return new TagDetails(tag.names[0], tag.category, tag.usages);
|
||||||
}
|
}
|
||||||
|
|
||||||
static fromScapedTag(tag: ScrapedTag): TagDetails {
|
static fromScapedTag(tag: ScrapedTag): TagDetails {
|
||||||
|
|
@ -37,6 +37,7 @@ export class ScrapedPostDetails {
|
||||||
source = "";
|
source = "";
|
||||||
referrer?: string;
|
referrer?: string;
|
||||||
resolution?: [number, number];
|
resolution?: [number, number];
|
||||||
|
reverseSearchResult: ImageSearchResult | undefined;
|
||||||
|
|
||||||
constructor(post: ScrapedPost) {
|
constructor(post: ScrapedPost) {
|
||||||
this.contentUrl = post.contentUrl;
|
this.contentUrl = post.contentUrl;
|
||||||
|
|
@ -51,6 +52,10 @@ export class ScrapedPostDetails {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class SimilarPostInfo {
|
||||||
|
constructor(public readonly id: number, public readonly percentage: number) {}
|
||||||
|
}
|
||||||
|
|
||||||
export type MessageType = "error" | "info" | "success";
|
export type MessageType = "error" | "info" | "success";
|
||||||
export type MessageCategory = "none" | "persistent" | "find_similar";
|
export type MessageCategory = "none" | "persistent" | "find_similar";
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -32,7 +32,7 @@ async function testConnection() {
|
||||||
|
|
||||||
const api = new SzuruWrapper(selectedSite.value.domain, selectedSite.value.username, selectedSite.value.authToken);
|
const api = new SzuruWrapper(selectedSite.value.domain, selectedSite.value.username, selectedSite.value.authToken);
|
||||||
try {
|
try {
|
||||||
const info = (await api.getInfo()) as any;
|
const info = (await api.getInfo());
|
||||||
const instanceName = info?.config.name;
|
const instanceName = info?.config.name;
|
||||||
|
|
||||||
if (instanceName == undefined) {
|
if (instanceName == undefined) {
|
||||||
|
|
|
||||||
|
|
@ -8,11 +8,11 @@ import {
|
||||||
Message,
|
Message,
|
||||||
ScrapedPostDetails,
|
ScrapedPostDetails,
|
||||||
TagDetails,
|
TagDetails,
|
||||||
MessageCategory,
|
|
||||||
PostUploadCommandData,
|
PostUploadCommandData,
|
||||||
|
SimilarPostInfo,
|
||||||
} from "~/models";
|
} from "~/models";
|
||||||
import SzuruWrapper from "~/api";
|
import SzuruWrapper from "~/api";
|
||||||
import { ImageSearchResult, Post } from "~/api/models";
|
import { ImageSearchResult } from "~/api/models";
|
||||||
import { isChrome } from "~/env";
|
import { isChrome } from "~/env";
|
||||||
import { Runtime, WebRequest } from "webextension-polyfill";
|
import { Runtime, WebRequest } from "webextension-polyfill";
|
||||||
import { Ref } from "vue";
|
import { Ref } from "vue";
|
||||||
|
|
@ -28,6 +28,8 @@ let autocompleteTags = ref<TagDetails[]>([]);
|
||||||
let cancelSource = ref<CancelTokenSource | undefined>(undefined);
|
let cancelSource = ref<CancelTokenSource | undefined>(undefined);
|
||||||
let autocompleteIndex = ref(-1);
|
let autocompleteIndex = ref(-1);
|
||||||
let selectedSiteId = ref<string | undefined>(undefined);
|
let selectedSiteId = ref<string | undefined>(undefined);
|
||||||
|
let isSearchingForSimilarPosts = ref<boolean>(false);
|
||||||
|
let genericError = ref<string | undefined>(undefined);
|
||||||
|
|
||||||
const activeSite = computed(() => {
|
const activeSite = computed(() => {
|
||||||
if (selectedSiteId.value) {
|
if (selectedSiteId.value) {
|
||||||
|
|
@ -44,21 +46,23 @@ watch(selectedPost, () => {
|
||||||
});
|
});
|
||||||
|
|
||||||
watch(selectedSiteId, async (x) => {
|
watch(selectedSiteId, async (x) => {
|
||||||
|
if (config.selectedSiteId != x) {
|
||||||
config.selectedSiteId = x;
|
config.selectedSiteId = x;
|
||||||
await config.save();
|
await config.save();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
function openOptionsPage() {
|
function openOptionsPage() {
|
||||||
browser.runtime.openOptionsPage();
|
browser.runtime.openOptionsPage();
|
||||||
}
|
}
|
||||||
|
|
||||||
async function getActiveTabId() {
|
async function getActiveTabId(): Promise<number> {
|
||||||
const activeTabs = await browser.tabs.query({
|
const activeTabs = await browser.tabs.query({
|
||||||
active: true,
|
active: true,
|
||||||
currentWindow: true,
|
currentWindow: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
if (activeTabs.length > 0) return activeTabs[0].id!;
|
if (activeTabs.length > 0 && activeTabs[0].id) return activeTabs[0].id;
|
||||||
throw new Error("No active tab.");
|
throw new Error("No active tab.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -100,15 +104,12 @@ async function grabPost() {
|
||||||
loadTagCounts();
|
loadTagCounts();
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
pushInfo("No posts found.");
|
// pushInfo("No posts found.");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function upload() {
|
async function upload() {
|
||||||
if (!selectedSiteId.value) {
|
if (!selectedSiteId.value) return;
|
||||||
pushError("No site selected!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
window.scrollTo(0, 0);
|
window.scrollTo(0, 0);
|
||||||
|
|
||||||
|
|
@ -125,7 +126,7 @@ async function upload() {
|
||||||
function getTagClasses(tag: TagDetails): string[] {
|
function getTagClasses(tag: TagDetails): string[] {
|
||||||
let classes: string[] = [];
|
let classes: string[] = [];
|
||||||
|
|
||||||
if (tag.category && <any>tag.category != "default") {
|
if (tag.category && tag.category != "default") {
|
||||||
classes.push("tag-" + tag.category);
|
classes.push("tag-" + tag.category);
|
||||||
} else {
|
} else {
|
||||||
classes.push("tag-general");
|
classes.push("tag-general");
|
||||||
|
|
@ -136,7 +137,7 @@ function getTagClasses(tag: TagDetails): string[] {
|
||||||
|
|
||||||
function breakTagName(tagName: string) {
|
function breakTagName(tagName: string) {
|
||||||
// Based on https://stackoverflow.com/a/6316913
|
// Based on https://stackoverflow.com/a/6316913
|
||||||
return tagName.replace(/\_/g, "_<wbr>");
|
return tagName.replace(/_/g, "_<wbr>");
|
||||||
}
|
}
|
||||||
|
|
||||||
function resolutionToString(resolution: [number, number]) {
|
function resolutionToString(resolution: [number, number]) {
|
||||||
|
|
@ -170,45 +171,25 @@ function getMessageClasses(message: Message) {
|
||||||
return classes;
|
return classes;
|
||||||
}
|
}
|
||||||
|
|
||||||
function pushInfo(message: string, category: MessageCategory = "none"): Message {
|
function getActiveSitePostUrl(postId: number): string {
|
||||||
let msg = new Message(message, "info", category);
|
|
||||||
messages.push(msg);
|
|
||||||
return msg;
|
|
||||||
}
|
|
||||||
|
|
||||||
function pushError(message: string, category: MessageCategory = "none"): Message {
|
|
||||||
let msg = new Message(message, "error", category);
|
|
||||||
messages.push(msg);
|
|
||||||
return msg;
|
|
||||||
}
|
|
||||||
|
|
||||||
function removeMessage(message: Message) {
|
|
||||||
for (let i = 0; i < messages.length; i++) {
|
|
||||||
if (messages[i].id == message.id) {
|
|
||||||
messages.splice(i, 1);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function clearMessages(category?: MessageCategory) {
|
|
||||||
if (category) {
|
|
||||||
for (let i = 0; i < messages.length; i++) {
|
|
||||||
if (messages[i].category == category) {
|
|
||||||
// Decrement i to negate the i++, because if we remove one object the items
|
|
||||||
// shift one place to the left, aka index--
|
|
||||||
// If we don't do i-- we'll skip the next item in our for loop
|
|
||||||
messages.splice(i--, 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
messages.splice(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function getPostUrl(post: Post): string {
|
|
||||||
if (!activeSite.value) return "";
|
if (!activeSite.value) return "";
|
||||||
return getUrl(activeSite.value.domain, "post", post.id.toString());
|
return getUrl(activeSite.value.domain, "post", postId.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
function getSimilarPosts(data: ImageSearchResult | undefined): SimilarPostInfo[] {
|
||||||
|
if (!data) return [];
|
||||||
|
|
||||||
|
const lst: SimilarPostInfo[] = [];
|
||||||
|
|
||||||
|
for (const similarPost of data.similarPosts) {
|
||||||
|
if (data.exactPost && data.exactPost.id == similarPost.post.id) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
lst.push(new SimilarPostInfo(similarPost.post.id, Math.round(100 - similarPost.distance * 100)));
|
||||||
|
}
|
||||||
|
|
||||||
|
return lst;
|
||||||
}
|
}
|
||||||
|
|
||||||
function addTag(tag: TagDetails) {
|
function addTag(tag: TagDetails) {
|
||||||
|
|
@ -228,13 +209,12 @@ async function clickFindSimilar() {
|
||||||
}
|
}
|
||||||
|
|
||||||
async function findSimilar(post: Ref<ScrapedPostDetails | undefined>) {
|
async function findSimilar(post: Ref<ScrapedPostDetails | undefined>) {
|
||||||
if (!post.value || !szuru.value) {
|
if (!post.value || !szuru.value) return;
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
clearMessages("find_similar");
|
// Don't load this again if it is already loaded.
|
||||||
|
if (post.value.reverseSearchResult) return;
|
||||||
|
|
||||||
const msg = pushInfo("Searching for similar posts...", "find_similar");
|
isSearchingForSimilarPosts.value = true;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
let res: ImageSearchResult | undefined;
|
let res: ImageSearchResult | undefined;
|
||||||
|
|
@ -250,40 +230,16 @@ async function findSimilar(post: Ref<ScrapedPostDetails | undefined>) {
|
||||||
res = await szuru.value.reverseSearch(post.value.contentUrl);
|
res = await szuru.value.reverseSearch(post.value.contentUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!res.exactPost && res.similarPosts.length == 0) {
|
post.value.reverseSearchResult = res;
|
||||||
msg.content = "No similar posts found";
|
|
||||||
} else {
|
|
||||||
if (res.exactPost) {
|
|
||||||
msg.content = `<a href='${getPostUrl(res.exactPost)}' target='_blank'>Post already uploaded (${
|
|
||||||
res.exactPost.id
|
|
||||||
})</a>`;
|
|
||||||
msg.level = "error";
|
|
||||||
}
|
|
||||||
|
|
||||||
for (const similarPost of res.similarPosts) {
|
|
||||||
// Don't show this message for the exactPost (because we have a different message for that)
|
|
||||||
if (res.exactPost && res.exactPost.id == similarPost.post.id) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
pushInfo(
|
|
||||||
`<a href='${getPostUrl(similarPost.post)}' target='_blank'>Post ${similarPost.post.id}
|
|
||||||
looks ${Math.round(100 - similarPost.distance * 100)}% similar</a>`,
|
|
||||||
"find_similar"
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
clearMessages();
|
genericError.value = "Couldn't reverse search";
|
||||||
pushError("Error: couldn't reverse search.");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
isSearchingForSimilarPosts.value = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
function getPostForUrl(contentUrl: string): ScrapedPostDetails | undefined {
|
function getPostForUrl(contentUrl: string): ScrapedPostDetails | undefined {
|
||||||
for (const post of posts) {
|
return posts.find((x) => x.contentUrl == contentUrl);
|
||||||
if (post.contentUrl == contentUrl) return post;
|
|
||||||
}
|
|
||||||
return undefined;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async function loadTagCounts() {
|
async function loadTagCounts() {
|
||||||
|
|
@ -378,7 +334,7 @@ async function autocompletePopulator(input: string) {
|
||||||
cancelSource.value = axios.CancelToken.source();
|
cancelSource.value = axios.CancelToken.source();
|
||||||
|
|
||||||
const query = decodeURIComponent(`*${encodeTagName(input)}* sort:usages`);
|
const query = decodeURIComponent(`*${encodeTagName(input)}* sort:usages`);
|
||||||
const resp = await szuru.value?.getTags(
|
const res = await szuru.value?.getTags(
|
||||||
query,
|
query,
|
||||||
0,
|
0,
|
||||||
100,
|
100,
|
||||||
|
|
@ -386,20 +342,21 @@ async function autocompletePopulator(input: string) {
|
||||||
cancelSource.value.token
|
cancelSource.value.token
|
||||||
);
|
);
|
||||||
|
|
||||||
autocompleteTags.value = resp!.results.map((x) => TagDetails.fromTag(x));
|
if (res) {
|
||||||
|
autocompleteTags.value = res.results.map((x) => TagDetails.fromTag(x));
|
||||||
if (autocompleteTags.value.length > 0) autocompleteShown.value = true;
|
if (autocompleteTags.value.length > 0) autocompleteShown.value = true;
|
||||||
|
} else {
|
||||||
|
autocompleteTags.value = [];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
onMounted(async () => {
|
onMounted(async () => {
|
||||||
config = await Config.load();
|
config = await Config.load();
|
||||||
selectedSiteId.value = config.selectedSiteId;
|
selectedSiteId.value = config.selectedSiteId;
|
||||||
|
|
||||||
if (config.sites.length == 0) {
|
|
||||||
pushError("No szurubooru server configured!", "persistent");
|
|
||||||
}
|
|
||||||
|
|
||||||
browser.runtime.onMessage.addListener((cmd: BrowserCommand, _sender: Runtime.MessageSender) => {
|
browser.runtime.onMessage.addListener((cmd: BrowserCommand, _sender: Runtime.MessageSender) => {
|
||||||
switch (cmd.name) {
|
switch (cmd.name) {
|
||||||
|
// TODO: Replace with set_upload_state (or something like that)
|
||||||
case "push_message":
|
case "push_message":
|
||||||
messages.push(cmd.data);
|
messages.push(cmd.data);
|
||||||
break;
|
break;
|
||||||
|
|
@ -413,7 +370,7 @@ onMounted(async () => {
|
||||||
|
|
||||||
let extraInfoSpec: WebRequest.OnBeforeSendHeadersOptions[] = ["requestHeaders", "blocking"];
|
let extraInfoSpec: WebRequest.OnBeforeSendHeadersOptions[] = ["requestHeaders", "blocking"];
|
||||||
if (isChrome) {
|
if (isChrome) {
|
||||||
extraInfoSpec.push("extraHeaders" as any);
|
extraInfoSpec.push("extraHeaders");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Add "Referer" header to all requests if post.referrer is set.
|
// Add "Referer" header to all requests if post.referrer is set.
|
||||||
|
|
@ -451,6 +408,36 @@ useDark();
|
||||||
<div class="popup-container">
|
<div class="popup-container">
|
||||||
<div class="popup-messages">
|
<div class="popup-messages">
|
||||||
<ul class="messages">
|
<ul class="messages">
|
||||||
|
<!--
|
||||||
|
Error messages
|
||||||
|
-->
|
||||||
|
|
||||||
|
<li v-if="config.sites.length == 0" class="bg-danger">No szurubooru server configured!</li>
|
||||||
|
|
||||||
|
<li v-if="!activeSite" class="bg-danger">No target server selected!</li>
|
||||||
|
|
||||||
|
<li v-if="selectedPost.reverseSearchResult?.exactPost" class="bg-danger">
|
||||||
|
<a :href="getActiveSitePostUrl(selectedPost.reverseSearchResult?.exactPost.id)"
|
||||||
|
>Post already uploaded ({{ selectedPost.reverseSearchResult.exactPost.id }})</a
|
||||||
|
>
|
||||||
|
</li>
|
||||||
|
|
||||||
|
<li v-if="genericError" class="bg-danger">{{ genericError }}</li>
|
||||||
|
|
||||||
|
<!--
|
||||||
|
Info messages
|
||||||
|
-->
|
||||||
|
|
||||||
|
<li v-if="isSearchingForSimilarPosts">Searching for similar posts...</li>
|
||||||
|
|
||||||
|
<li v-if="selectedPost.reverseSearchResult?.similarPosts.length == 0">No similar posts found</li>
|
||||||
|
|
||||||
|
<li v-for="similarPost in getSimilarPosts(selectedPost.reverseSearchResult)" :key="similarPost.id">
|
||||||
|
<a :href="getActiveSitePostUrl(similarPost.id)"
|
||||||
|
>Post {{ similarPost.id }} looks {{ similarPost.percentage }}% similar</a
|
||||||
|
>
|
||||||
|
</li>
|
||||||
|
|
||||||
<li v-for="msg in messages" :key="msg.content" :class="getMessageClasses(msg)" v-html="msg.content"></li>
|
<li v-for="msg in messages" :key="msg.content" :class="getMessageClasses(msg)" v-html="msg.content"></li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue