SzuruChrome/src/popup/App.vue

471 lines
14 KiB
Vue

<template>
<div class="popup-container">
<div class="popup-messages">
<ul class="messages">
<li v-for="msg in messages" :key="msg.content" :class="getMessageClasses(msg)" v-html="msg.content"></li>
</ul>
</div>
<div class="popup-left">
<div class="popup-section">
<div class="section-header">
<span>Basic info</span>
<i class="fas fa-cog" @click="openSettings"></i>
</div>
<div class="section-row">
<span class="section-label">Safety</span>
<label class="container">
<input type="radio" value="safe" v-model="selectedPost.rating" />
<span class="checkmark"></span>
Safe
</label>
<label class="container">
<input type="radio" value="sketchy" v-model="selectedPost.rating" />
<span class="checkmark"></span>
Sketchy
</label>
<label class="container">
<input type="radio" value="unsafe" v-model="selectedPost.rating" />
<span class="checkmark"></span>
Unsafe
</label>
</div>
<div class="section-row">
<span class="section-label">Source</span>
<input type="text" v-model="selectedPost.source" />
</div>
</div>
<div class="popup-section">
<div class="section-header">
<span>Tags</span>
</div>
<div class="section-row" style="display: flex">
<input type="text" v-model="addTagInput" v-on:keyup.enter="addTag" />
<button class="primary" style="margin-left: 5px; height: auto" @click="addTag">Add</button>
</div>
<div class="section-row">
<ul class="compact-tags">
<li v-for="tag in selectedPost.tags" :key="tag.name">
<a class="remove-tag" @click="removeTag(tag)">x</a>
<span :class="getTagClasses(tag)">{{ tag.name }}</span>
<span class="tag-usages">{{ tag.usages ? tag.usages : "" }}</span>
</li>
</ul>
</div>
</div>
<div class="popup-section">
<div class="section-row">
<select disabled>
<option>{{ activeSite != null ? activeSite.domain : "(no site available)" }}</option>
</select>
</div>
</div>
</div>
<div class="popup-right">
<div class="popup-section">
<select v-model="selectedPost">
<option v-for="post in posts" v-bind:key="post.name" v-bind:value="post">{{ post.name }}</option>
</select>
</div>
<div class="popup-section">
<img :src="selectedPost.contentUrl" v-if="selectedPost.contentType == 'image'" />
<video v-if="selectedPost.contentType == 'video'" controls>
<source :src="selectedPost.contentUrl" />
</video>
<div style="display: flex">
<button class="primary" @click="findSimilar">Find similar</button>
<button class="primary full" style="margin-left: 5px" @click="upload">Import</button>
</div>
</div>
</div>
</div>
</template>
<script lang="ts">
import Vue from "vue";
import { browser, Runtime, WebRequest } from "webextension-polyfill-ts";
import { ContentType, ScrapedPost, ScrapedTag, ScrapeResults, BooruTypes } from "neo-scraper";
import SzuruWrapper from "../SzuruWrapper";
import { Post } from "../SzuruTypes";
import { Config, SzuruSiteConfig } from "../Config";
import { BrowserCommand, Message, getUrl, isChrome, encodeTagName } from "../Common";
class TagViewModel {
name: string;
category?: BooruTypes.TagCategory;
usages: number | undefined;
constructor(tag: ScrapedTag) {
this.name = tag.name;
this.category = tag.category;
this.usages = 0;
}
}
class PostViewModel {
name: string = "";
tags: TagViewModel[] = [];
contentUrl: string;
pageUrl: string;
contentType: ContentType;
rating: BooruTypes.SafetyRating;
source: string | undefined;
referrer: string | undefined;
constructor(post: ScrapedPost) {
this.contentUrl = post.contentUrl;
this.pageUrl = post.pageUrl;
this.contentType = post.contentType;
this.rating = post.rating;
this.source = post.source;
this.referrer = post.referrer;
this.tags = post.tags.map((x) => new TagViewModel(x));
}
}
export default Vue.extend({
data() {
return {
config: null as Config | null,
activeSite: null as SzuruSiteConfig | null,
szuru: null as SzuruWrapper | null,
posts: [] as PostViewModel[],
selectedPost: new ScrapedPost(), // Shouldn't be null because VueJS gets a mental breakdown when it sees a null.
messages: [] as Message[],
addTagInput: "",
};
},
watch: {
selectedPost() {
if (this.config && this.config.autoSearchSimilar) {
// No need to check if any vars are unset, findSimilar does that internally
this.findSimilar();
}
},
},
methods: {
async getActiveTabId() {
const activeTabs = await browser.tabs.query({
active: true,
currentWindow: true,
});
if (activeTabs.length > 0) return activeTabs[0].id!;
throw "No active tab.";
},
// Try to scrape the post from the page
async grabPost() {
// Get active tab
const activeTabId = await this.getActiveTabId();
// Send 'grab_post' to the content script on the active tab
const x = await browser.tabs.sendMessage(activeTabId, new BrowserCommand("grab_post"));
// We need to create a new ScrapeResults object and fill it with our data, because the get_posts()
// method gets 'lost' when sent from the contentscript to the popup (it gets JSON.stringified and any prototype defines are lost there)
const res = Object.assign(new ScrapeResults(), x);
// Clear current posts array
this.posts = [];
for (var result of res.results) {
for (var i in result.posts) {
const vm = new PostViewModel(result.posts[i]);
vm.name = `[${result.engine}] Post ${parseInt(i) + 1}`; // parseInt() is required!
if (!this.config?.useOriginalSource || vm.source == undefined) {
vm.source = vm.pageUrl;
}
this.posts.push(vm);
}
}
if (this.posts.length > 0) {
this.selectedPost = this.posts[0];
if (this.config?.loadTagCounts) {
this.loadTagCounts();
}
} else {
this.pushInfo("No posts found.");
}
},
// Try to upload the post to the selected szurubooru instance
async upload() {
window.scrollTo(0, 0);
// Outsource to background.ts
const cmd = new BrowserCommand("upload_post", this.selectedPost);
await browser.runtime.sendMessage(cmd);
// TODO: Handle errors/status update
},
// Open extension settings page in new tab
async openSettings() {
const url = browser.extension.getURL("options/options.html");
window.open(url);
},
getTagClasses(tag: ScrapedTag): string[] {
let classes: string[] = [];
if (tag.category) {
classes.push("tag-" + tag.category);
} else {
classes.push("tag-general");
}
return classes;
},
// Remove tag from post's taglist
removeTag(tag: ScrapedTag) {
if (this.selectedPost) {
const idx = this.selectedPost.tags.indexOf(tag);
if (idx != -1) {
this.selectedPost.tags.splice(idx, 1);
}
}
},
getMessageClasses(message: Message) {
let classes: string[] = [];
switch (message.level) {
case "error":
classes.push("message-error");
break;
case "success":
classes.push("message-success");
break;
}
return classes;
},
// Add info message
pushInfo(message: string, category: string | null = null): Message {
let msg = new Message(message, "info", category);
this.messages.push(msg);
return msg;
},
// Add error message
pushError(message: string, category: string | null = null): Message {
let msg = new Message(message, "error", category);
this.messages.push(msg);
return msg;
},
// Clear messages of given category. When category is empty clear all messages.
clearMessages(category: string | null) {
if (category) {
for (let i = 0; i < this.messages.length; i++) {
if (this.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
this.messages.splice(i--, 1);
}
}
} else {
this.messages = [];
}
},
getPostUrl(post: Post): string {
if (!this.activeSite) return "";
return getUrl(this.activeSite.domain, "post", post.id.toString());
},
// Add tag to the post's taglist
addTag() {
if (this.selectedPost) {
const tagName = this.addTagInput;
this.addTagInput = "";
if (tagName.length > 0 && this.selectedPost.tags.find((x) => x.name == tagName) == undefined) {
this.selectedPost.tags.push(new ScrapedTag(tagName));
}
}
},
// Find posts similar to the grabbed post (this.selectedPost)
async findSimilar() {
if (!this.selectedPost || !this.szuru) {
return;
}
this.clearMessages("FIND_SIMILAR");
const msg = this.pushInfo("Searching for similar posts...", "FIND_SIMILAR");
const res = await this.szuru.reverseSearch(this.selectedPost.contentUrl);
if (!res.exactPost && res.similarPosts.length == 0) {
msg.content = "No similar posts found";
} else {
if (res.exactPost) {
msg.content = `<a href='${this.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;
}
this.pushInfo(
`<a href='${this.getPostUrl(similarPost.post)}' target='_blank'>Post ${similarPost.post.id}
looks ${Math.round(100 - similarPost.distance * 100)}% similar</a>`,
"FIND_SIMILAR"
);
}
}
},
// Returns ScrapedPost for a contentUrl, or undefined if the given contentUrl does not belong to a ScrapedPost.
getPostForUrl(contentUrl: string): PostViewModel | undefined {
for (const post of this.posts) {
if (post.contentUrl == contentUrl) return post;
}
return undefined;
},
async loadTagCounts() {
const allTags = this.posts.flatMap((x) => x.tags);
console.log(allTags);
for (let i = 0; i < allTags.length; i += 100) {
const query = allTags
.slice(i, i + 101)
.map((x) => encodeTagName(x.name))
.join();
const resp = await this.szuru?.getTags(query);
console.dir(resp);
if (resp) {
for (let post of this.posts) {
for (let tag of resp.results) {
for (let postTag of post.tags) {
if (postTag.name == tag.names[0]) {
postTag.usages = tag.usages;
break;
}
}
}
}
}
console.dir(this.posts);
}
},
},
async mounted() {
this.config = await Config.load();
this.activeSite = this.config.sites.length > 0 ? this.config.sites[0] : null;
if (!this.activeSite) {
this.pushError("No szurubooru server configured!");
} else {
this.szuru = await SzuruWrapper.createFromConfig(this.activeSite);
}
browser.runtime.onMessage.addListener((cmd: BrowserCommand, sender: Runtime.MessageSender) => {
switch (cmd.name) {
case "push_message":
this.messages.push(cmd.data);
break;
case "remove_messages":
for (let i = 0; i < cmd.data; i++) {
if (this.messages.length == 0) break;
this.messages.pop();
}
break;
}
});
let extraInfoSpec: WebRequest.OnBeforeSendHeadersOptions[] = ["requestHeaders", "blocking"];
if (isChrome()) {
extraInfoSpec.push("extraHeaders" as any);
}
browser.webRequest.onBeforeSendHeaders.addListener(
(details: WebRequest.OnBeforeSendHeadersDetailsType) => {
let requestHeaders = details.requestHeaders ?? [];
// Find which ScrapedPost this belongs to, if any.
const post = this.getPostForUrl(details.url);
if (post != undefined && post.referrer) {
console.log(`Setting referrer to '${post.referrer}' for request to '${post.contentUrl}'.`);
// TODO: If the headers already have a 'Referer' header this will _not_ override that.
// Though as far as I am aware the 'img' tag doesn't set this header.
requestHeaders.push({ name: "Referer", value: post.referrer });
}
return <WebRequest.BlockingResponse>{
requestHeaders,
};
},
{ urls: ["<all_urls>"] },
extraInfoSpec
);
// Always call grabPost, even when there is no activeSite
this.grabPost();
},
});
</script>
<style scoped>
video {
width: 100%;
}
.popup-container {
width: 700px;
display: grid;
grid-template-columns: 300px 400px;
}
.popup-messages {
grid-column: 1 / 3;
margin: 0 10px;
}
.popup-left {
grid-column: 1;
}
.popup-right {
grid-column: 2;
}
.popup-section {
margin: 10px;
}
.section-header {
height: 30px;
padding: 0 6px 0 10px;
background-color: #eee;
display: flex;
justify-content: space-between;
align-items: center;
}
.section-header > span {
font-size: 1.2em;
vertical-align: middle;
}
.section-row {
margin: 10px 5px 0;
}
.section-label {
display: block;
margin: 0 0 5px 0;
}
</style>