Update
parent
065684899a
commit
5908a5cad5
|
|
@ -41,7 +41,6 @@ select {
|
||||||
border: 1px solid #eee;
|
border: 1px solid #eee;
|
||||||
height: 30px;
|
height: 30px;
|
||||||
width: 100%;
|
width: 100%;
|
||||||
font-family: "Open Sans", sans-serif;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.compact-tags {
|
.compact-tags {
|
||||||
|
|
|
||||||
|
|
@ -28,7 +28,7 @@
|
||||||
],
|
],
|
||||||
"browser_specific_settings": {
|
"browser_specific_settings": {
|
||||||
"gecko": {
|
"gecko": {
|
||||||
"id": "8cc2afc8-6c4a-4d2b-9513-46e46e5b4208"
|
"id": "{8cc2afc8-6c4a-4d2b-9513-46e46e5b4208}"
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -0,0 +1,407 @@
|
||||||
|
<template>
|
||||||
|
<div class="popup-container">
|
||||||
|
<div class="popup-row">
|
||||||
|
<div class="popup-column1" style="margin: 0px 10px;">
|
||||||
|
<ul class="messages">
|
||||||
|
<li
|
||||||
|
v-for="msg in messages"
|
||||||
|
:key="msg.name"
|
||||||
|
:class="getMessageClasses(msg)"
|
||||||
|
v-html="msg.content"
|
||||||
|
></li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="popup-row">
|
||||||
|
<div class="popup-column2">
|
||||||
|
<div class="popup-header">
|
||||||
|
<span>Basic info</span>
|
||||||
|
<i class="fas fa-cog" @click="openSettings"></i>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="popup-content">
|
||||||
|
<div class="popup-block">
|
||||||
|
<span class="block-title">Safety</span>
|
||||||
|
|
||||||
|
<label class="container">
|
||||||
|
<input type="radio" value="safe" v-model="post.rating" />
|
||||||
|
<span class="checkmark"></span>
|
||||||
|
Safe
|
||||||
|
</label>
|
||||||
|
|
||||||
|
<label class="container">
|
||||||
|
<input type="radio" value="sketchy" v-model="post.rating" />
|
||||||
|
<span class="checkmark"></span>
|
||||||
|
Sketchy
|
||||||
|
</label>
|
||||||
|
|
||||||
|
<label class="container">
|
||||||
|
<input type="radio" value="unsafe" v-model="post.rating" />
|
||||||
|
<span class="checkmark"></span>
|
||||||
|
Unsafe
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="popup-block">
|
||||||
|
<span class="block-title">Source</span>
|
||||||
|
<input type="text" v-model="post.source" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="popup-header">
|
||||||
|
<span>Tags</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="popup-content">
|
||||||
|
<div class="popup-block" 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="popup-block">
|
||||||
|
<ul class="compact-tags">
|
||||||
|
<li v-for="tag in post.tags" :key="tag.name">
|
||||||
|
<a class="remove-tag" @click="removeTag(tag)">x</a>
|
||||||
|
<span :class="getTagClasses(tag)">{{ tag.name }}</span>
|
||||||
|
<span class="tag-usages">0</span>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="popup-column2">
|
||||||
|
<img :src="post.imageUrl" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="popup-row">
|
||||||
|
<div class="popup-column2">
|
||||||
|
<select disabled>
|
||||||
|
<option>{{ activeSite != null ? activeSite.domain : "(no site available)" }}</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="popup-column2" 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>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script lang="ts">
|
||||||
|
import Vue from "vue";
|
||||||
|
import { browser } from "webextension-polyfill-ts";
|
||||||
|
import { ScrapedPost, ScrapedTag, ScrapeResults } from "neo-scraper";
|
||||||
|
import SzuruWrapper from "../SzuruWrapper";
|
||||||
|
import { Post, SzuruError } from "../SzuruTypes";
|
||||||
|
import { Config, SzuruSiteConfig } from "../Config";
|
||||||
|
|
||||||
|
type MessageType = "error" | "info" | "success";
|
||||||
|
|
||||||
|
class Message {
|
||||||
|
content: string;
|
||||||
|
type: MessageType;
|
||||||
|
|
||||||
|
constructor(content: string, type: MessageType = "info") {
|
||||||
|
this.content = content;
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default Vue.extend({
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
activeSite: null as SzuruSiteConfig | null,
|
||||||
|
szuru: null as SzuruWrapper | null,
|
||||||
|
post: new ScrapedPost(),
|
||||||
|
messages: [] as Message[],
|
||||||
|
addTagInput: ""
|
||||||
|
};
|
||||||
|
},
|
||||||
|
methods: {
|
||||||
|
// Try to scrape the post from the page
|
||||||
|
async grabPost() {
|
||||||
|
// Get active tab
|
||||||
|
const activeTab = (await browser.tabs.query({
|
||||||
|
active: true,
|
||||||
|
currentWindow: true
|
||||||
|
}))[0];
|
||||||
|
|
||||||
|
// Send 'grab_post' to the content script on the active tab
|
||||||
|
const x = await browser.tabs.sendMessage(activeTab.id!, "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);
|
||||||
|
const post = res.posts.length > 0 ? res.posts[0] : null;
|
||||||
|
|
||||||
|
if (post) {
|
||||||
|
this.post = post;
|
||||||
|
console.dir(post);
|
||||||
|
} else {
|
||||||
|
this.pushError("Couldn't grab post");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
// Try to upload the post to the selected szurubooru instance
|
||||||
|
async upload() {
|
||||||
|
this.clearMessages();
|
||||||
|
window.scrollTo(0, 0);
|
||||||
|
|
||||||
|
if (!this.post || !this.post.imageUrl) {
|
||||||
|
this.pushError("There is no post to upload!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!this.szuru) {
|
||||||
|
this.pushError("No szurubooru server configured!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Create and upload post
|
||||||
|
let uploadMsg = new Message("Uploading...");
|
||||||
|
this.messages.push(uploadMsg);
|
||||||
|
const createdPost = await this.szuru.createPost(this.post);
|
||||||
|
// TODO: Clicking a link doesn't actually open it in a new tab,
|
||||||
|
// see https://stackoverflow.com/questions/8915845
|
||||||
|
uploadMsg.content = `<a href='${this.getPostUrl(createdPost)}' target='_blank'>Uploaded post</a>`;
|
||||||
|
uploadMsg.type = "success";
|
||||||
|
|
||||||
|
// Find tags with "default" category and update it
|
||||||
|
// TODO: Make all these categories configurable
|
||||||
|
const tagsWithCategory = this.post.tags.filter(x => x.category);
|
||||||
|
const unsetCategoryTags = createdPost.tags
|
||||||
|
.filter(x => x.category == "default")
|
||||||
|
.filter(x => tagsWithCategory.some(y => x.names.includes(y.name)));
|
||||||
|
|
||||||
|
if (unsetCategoryTags.length != 0) {
|
||||||
|
let tagsMsg = new Message(`${unsetCategoryTags.length} tags need a different category`);
|
||||||
|
this.messages.push(tagsMsg);
|
||||||
|
|
||||||
|
// unsetCategoryTags is of type MicroTag[] and we need a Tag resource to update it, so let's get those
|
||||||
|
const query = "?query=" + unsetCategoryTags.map(x => this.encodeTagName(x.names[0]));
|
||||||
|
const tags = (await this.szuru.getTags(query)).results;
|
||||||
|
|
||||||
|
for (let i in tags) {
|
||||||
|
tagsMsg.content = `Updating tag ${i}/${unsetCategoryTags.length}`;
|
||||||
|
tags[i].category = tagsWithCategory.find(x => tags[i].names.includes(x.name))!.category!;
|
||||||
|
await this.szuru.updateTag(tags[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
tagsMsg.content = `Updated ${tags.length} tags`;
|
||||||
|
tagsMsg.type = "success";
|
||||||
|
}
|
||||||
|
} catch (ex) {
|
||||||
|
const error = ex as SzuruError;
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
this.clearMessages();
|
||||||
|
this.pushError(error.description);
|
||||||
|
} else {
|
||||||
|
// Probably too long for a message
|
||||||
|
alert(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
// 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.post) {
|
||||||
|
const idx = this.post.tags.indexOf(tag);
|
||||||
|
if (idx != -1) {
|
||||||
|
this.post.tags.splice(idx, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
getMessageClasses(message: Message) {
|
||||||
|
let classes: string[] = [];
|
||||||
|
|
||||||
|
switch (message.type) {
|
||||||
|
case "error":
|
||||||
|
classes.push("message-error");
|
||||||
|
break;
|
||||||
|
case "success":
|
||||||
|
classes.push("message-success");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return classes;
|
||||||
|
},
|
||||||
|
// Add info message
|
||||||
|
pushInfo(message: string): Message {
|
||||||
|
let msg = new Message(message);
|
||||||
|
this.messages.push(msg);
|
||||||
|
return msg;
|
||||||
|
},
|
||||||
|
// Add error message
|
||||||
|
pushError(message: string): Message {
|
||||||
|
let msg = new Message(message, "error");
|
||||||
|
this.messages.push(msg);
|
||||||
|
return msg;
|
||||||
|
},
|
||||||
|
clearMessages() {
|
||||||
|
this.messages = [];
|
||||||
|
},
|
||||||
|
removeMessage(message: Message) {
|
||||||
|
const idx = this.messages.indexOf(message);
|
||||||
|
if (idx != -1) this.messages.splice(idx, 1);
|
||||||
|
},
|
||||||
|
getPostUrl(post: Post): string {
|
||||||
|
if (!this.activeSite) return "";
|
||||||
|
return this.activeSite.domain + "/post/" + post.id;
|
||||||
|
},
|
||||||
|
encodeTagName(tagName: string) {
|
||||||
|
// Searching for posts with re:zero will show an error message about unknown named token.
|
||||||
|
// Searching for posts with re\:zero will show posts tagged with re:zero.
|
||||||
|
return encodeURIComponent(tagName.replace(/\:/g, "\\:"));
|
||||||
|
},
|
||||||
|
// Add tag to the post's taglist
|
||||||
|
addTag() {
|
||||||
|
if (this.post) {
|
||||||
|
const tagName = this.addTagInput;
|
||||||
|
this.addTagInput = "";
|
||||||
|
if (this.post.tags.find(x => x.name == tagName) == undefined) {
|
||||||
|
this.post.tags.push(new ScrapedTag(tagName));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
// Find posts similar to the grabbed post (this.post)
|
||||||
|
async findSimilar() {
|
||||||
|
if (!this.post || !this.szuru) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove messages that contain "Post already uploaded" or "No similar posts found"
|
||||||
|
// but keep the other messages.
|
||||||
|
for (let i = 0; i < this.messages.length; i++) {
|
||||||
|
if (
|
||||||
|
this.messages[i].content.indexOf("No similar posts found") != -1 ||
|
||||||
|
this.messages[i].content.indexOf("Post already uploaded") != -1
|
||||||
|
) {
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const msgSearchSimilar = this.pushInfo("Searching for similar posts...");
|
||||||
|
const res = await this.szuru.reverseSearch(this.post.imageUrl);
|
||||||
|
this.removeMessage(msgSearchSimilar);
|
||||||
|
|
||||||
|
if (!res.exactPost && res.similarPosts.length == 0) {
|
||||||
|
this.pushInfo("No similar posts found");
|
||||||
|
} else {
|
||||||
|
if (res.exactPost) {
|
||||||
|
this.pushError(
|
||||||
|
`<a href='${this.getPostUrl(res.exactPost)}' target='_blank'>
|
||||||
|
Post already uploaded (${res.exactPost.id})</a>`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
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>`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
async mounted() {
|
||||||
|
const cfg = await Config.load();
|
||||||
|
this.activeSite = cfg.sites.length > 0 ? cfg.sites[0] : null;
|
||||||
|
|
||||||
|
if (!this.activeSite) {
|
||||||
|
this.pushError("No szurubooru server configured!");
|
||||||
|
} else {
|
||||||
|
this.szuru = await SzuruWrapper.createFromConfig(this.activeSite);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Always call grabPost, even when there is no activeSite
|
||||||
|
this.grabPost().then(() => {
|
||||||
|
if (cfg.autoSearchSimilar) {
|
||||||
|
// No need to check if any vars are unset, findSimilar does that internally
|
||||||
|
this.findSimilar();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
.popup-container {
|
||||||
|
width: 700px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.popup-row {
|
||||||
|
display: flex;
|
||||||
|
}
|
||||||
|
|
||||||
|
.popup-column1 {
|
||||||
|
flex: 100%;
|
||||||
|
margin: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.popup-column2 {
|
||||||
|
flex: 50%;
|
||||||
|
margin: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.popup-header {
|
||||||
|
height: 30px;
|
||||||
|
padding: 0 0 0 10px;
|
||||||
|
background-color: #eee;
|
||||||
|
margin: 0 0 10px 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.popup-content {
|
||||||
|
margin: 5px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.popup-header > span {
|
||||||
|
vertical-align: middle;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Horrible hack + guesswork */
|
||||||
|
.popup-header > i {
|
||||||
|
float: right;
|
||||||
|
margin: 8px 5px;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.popup-block {
|
||||||
|
margin: 0 0 10px 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.block-title {
|
||||||
|
display: block;
|
||||||
|
margin: 0 0 5px 0;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|
@ -1,7 +1,6 @@
|
||||||
<template>
|
<template>
|
||||||
<div class="popup-container">
|
<div class="popup-container">
|
||||||
<div class="popup-row">
|
<div class="popup-messages">
|
||||||
<div class="popup-column1" style="margin: 0px 10px;">
|
|
||||||
<ul class="messages">
|
<ul class="messages">
|
||||||
<li
|
<li
|
||||||
v-for="msg in messages"
|
v-for="msg in messages"
|
||||||
|
|
@ -11,50 +10,48 @@
|
||||||
></li>
|
></li>
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="popup-row">
|
<div class="popup-left">
|
||||||
<div class="popup-column2">
|
<div class="popup-section">
|
||||||
<div class="popup-header">
|
<div class="section-header">
|
||||||
<span>Basic info</span>
|
<span>Basic info</span>
|
||||||
<i class="fas fa-cog" @click="openSettings"></i>
|
<i class="fas fa-cog" @click="openSettings"></i>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="popup-content">
|
<div class="section-row">
|
||||||
<div class="popup-block">
|
<span class="section-label">Safety</span>
|
||||||
<span class="block-title">Safety</span>
|
|
||||||
|
|
||||||
<label class="container">
|
<label class="container">
|
||||||
<input type="radio" value="safe" v-model="post.rating" />
|
<input type="radio" value="safe" v-model="selectedPost.rating" />
|
||||||
<span class="checkmark"></span>
|
<span class="checkmark"></span>
|
||||||
Safe
|
Safe
|
||||||
</label>
|
</label>
|
||||||
|
|
||||||
<label class="container">
|
<label class="container">
|
||||||
<input type="radio" value="sketchy" v-model="post.rating" />
|
<input type="radio" value="sketchy" v-model="selectedPost.rating" />
|
||||||
<span class="checkmark"></span>
|
<span class="checkmark"></span>
|
||||||
Sketchy
|
Sketchy
|
||||||
</label>
|
</label>
|
||||||
|
|
||||||
<label class="container">
|
<label class="container">
|
||||||
<input type="radio" value="unsafe" v-model="post.rating" />
|
<input type="radio" value="unsafe" v-model="selectedPost.rating" />
|
||||||
<span class="checkmark"></span>
|
<span class="checkmark"></span>
|
||||||
Unsafe
|
Unsafe
|
||||||
</label>
|
</label>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="popup-block">
|
<div class="section-row">
|
||||||
<span class="block-title">Source</span>
|
<span class="section-label">Source</span>
|
||||||
<input type="text" v-model="post.source" />
|
<input type="text" v-model="selectedPost.source" />
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="popup-header">
|
<div class="popup-section">
|
||||||
|
<div class="section-header">
|
||||||
<span>Tags</span>
|
<span>Tags</span>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="popup-content">
|
<div class="section-row" style="display:flex;">
|
||||||
<div class="popup-block" style="display:flex;">
|
|
||||||
<input type="text" v-model="addTagInput" v-on:keyup.enter="addTag" />
|
<input type="text" v-model="addTagInput" v-on:keyup.enter="addTag" />
|
||||||
<button
|
<button
|
||||||
class="primary"
|
class="primary"
|
||||||
|
|
@ -63,9 +60,9 @@
|
||||||
>Add</button>
|
>Add</button>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="popup-block">
|
<div class="section-row">
|
||||||
<ul class="compact-tags">
|
<ul class="compact-tags">
|
||||||
<li v-for="tag in post.tags" :key="tag.name">
|
<li v-for="tag in selectedPost.tags" :key="tag.name">
|
||||||
<a class="remove-tag" @click="removeTag(tag)">x</a>
|
<a class="remove-tag" @click="removeTag(tag)">x</a>
|
||||||
<span :class="getTagClasses(tag)">{{ tag.name }}</span>
|
<span :class="getTagClasses(tag)">{{ tag.name }}</span>
|
||||||
<span class="tag-usages">0</span>
|
<span class="tag-usages">0</span>
|
||||||
|
|
@ -73,26 +70,27 @@
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="popup-column2">
|
<div class="popup-section">
|
||||||
<img :src="post.imageUrl" />
|
<div class="section-row">
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="popup-row">
|
|
||||||
<div class="popup-column2">
|
|
||||||
<select disabled>
|
<select disabled>
|
||||||
<option>{{ activeSite != null ? activeSite.domain : "(no site available)" }}</option>
|
<option>{{ activeSite != null ? activeSite.domain : "(no site available)" }}</option>
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="popup-column2" style="display: flex;">
|
<div class="popup-right">
|
||||||
|
<div class="popup-section">
|
||||||
|
<img :src="selectedPost.imageUrl" />
|
||||||
|
|
||||||
|
<div style="display: flex;">
|
||||||
<button class="primary" @click="findSimilar">Find similar</button>
|
<button class="primary" @click="findSimilar">Find similar</button>
|
||||||
<button class="primary full" style="margin-left: 5px;" @click="upload">Import</button>
|
<button class="primary full" style="margin-left: 5px;" @click="upload">Import</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
|
|
@ -106,10 +104,14 @@ import { Config, SzuruSiteConfig } from "../Config";
|
||||||
type MessageType = "error" | "info" | "success";
|
type MessageType = "error" | "info" | "success";
|
||||||
|
|
||||||
class Message {
|
class Message {
|
||||||
|
readonly id: number;
|
||||||
content: string;
|
content: string;
|
||||||
type: MessageType;
|
type: MessageType;
|
||||||
|
|
||||||
|
private static ids = 0;
|
||||||
|
|
||||||
constructor(content: string, type: MessageType = "info") {
|
constructor(content: string, type: MessageType = "info") {
|
||||||
|
this.id = Message.ids++;
|
||||||
this.content = content;
|
this.content = content;
|
||||||
this.type = type;
|
this.type = type;
|
||||||
}
|
}
|
||||||
|
|
@ -120,7 +122,7 @@ export default Vue.extend({
|
||||||
return {
|
return {
|
||||||
activeSite: null as SzuruSiteConfig | null,
|
activeSite: null as SzuruSiteConfig | null,
|
||||||
szuru: null as SzuruWrapper | null,
|
szuru: null as SzuruWrapper | null,
|
||||||
post: new ScrapedPost(),
|
selectedPost: new ScrapedPost(),
|
||||||
messages: [] as Message[],
|
messages: [] as Message[],
|
||||||
addTagInput: ""
|
addTagInput: ""
|
||||||
};
|
};
|
||||||
|
|
@ -129,10 +131,12 @@ export default Vue.extend({
|
||||||
// Try to scrape the post from the page
|
// Try to scrape the post from the page
|
||||||
async grabPost() {
|
async grabPost() {
|
||||||
// Get active tab
|
// Get active tab
|
||||||
const activeTab = (await browser.tabs.query({
|
const activeTab = (
|
||||||
|
await browser.tabs.query({
|
||||||
active: true,
|
active: true,
|
||||||
currentWindow: true
|
currentWindow: true
|
||||||
}))[0];
|
})
|
||||||
|
)[0];
|
||||||
|
|
||||||
// Send 'grab_post' to the content script on the active tab
|
// Send 'grab_post' to the content script on the active tab
|
||||||
const x = await browser.tabs.sendMessage(activeTab.id!, "grab_post");
|
const x = await browser.tabs.sendMessage(activeTab.id!, "grab_post");
|
||||||
|
|
@ -142,73 +146,14 @@ export default Vue.extend({
|
||||||
const post = res.posts.length > 0 ? res.posts[0] : null;
|
const post = res.posts.length > 0 ? res.posts[0] : null;
|
||||||
|
|
||||||
if (post) {
|
if (post) {
|
||||||
this.post = post;
|
this.selectedPost = post;
|
||||||
console.dir(post);
|
console.dir(post);
|
||||||
} else {
|
} else {
|
||||||
this.pushError("Couldn't grab post");
|
this.pushError("Couldn't grab post");
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
// Try to upload the post to the selected szurubooru instance
|
// Try to upload the post to the selected szurubooru instance
|
||||||
async upload() {
|
async upload() {},
|
||||||
this.clearMessages();
|
|
||||||
window.scrollTo(0, 0);
|
|
||||||
|
|
||||||
if (!this.post || !this.post.imageUrl) {
|
|
||||||
this.pushError("There is no post to upload!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!this.szuru) {
|
|
||||||
this.pushError("No szurubooru server configured!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
// Create and upload post
|
|
||||||
let uploadMsg = new Message("Uploading...");
|
|
||||||
this.messages.push(uploadMsg);
|
|
||||||
const createdPost = await this.szuru.createPost(this.post);
|
|
||||||
// TODO: Clicking a link doesn't actually open it in a new tab,
|
|
||||||
// see https://stackoverflow.com/questions/8915845
|
|
||||||
uploadMsg.content = `<a href='${this.getPostUrl(createdPost)}' target='_blank'>Uploaded post</a>`;
|
|
||||||
uploadMsg.type = "success";
|
|
||||||
|
|
||||||
// Find tags with "default" category and update it
|
|
||||||
// TODO: Make all these categories configurable
|
|
||||||
const tagsWithCategory = this.post.tags.filter(x => x.category);
|
|
||||||
const unsetCategoryTags = createdPost.tags
|
|
||||||
.filter(x => x.category == "default")
|
|
||||||
.filter(x => tagsWithCategory.some(y => x.names.includes(y.name)));
|
|
||||||
|
|
||||||
if (unsetCategoryTags.length != 0) {
|
|
||||||
let tagsMsg = new Message(`${unsetCategoryTags.length} tags need a different category`);
|
|
||||||
this.messages.push(tagsMsg);
|
|
||||||
|
|
||||||
// unsetCategoryTags is of type MicroTag[] and we need a Tag resource to update it, so let's get those
|
|
||||||
const query = "?query=" + unsetCategoryTags.map(x => this.encodeTagName(x.names[0]));
|
|
||||||
const tags = (await this.szuru.getTags(query)).results;
|
|
||||||
|
|
||||||
for (let i in tags) {
|
|
||||||
tagsMsg.content = `Updating tag ${i}/${unsetCategoryTags.length}`;
|
|
||||||
tags[i].category = tagsWithCategory.find(x => tags[i].names.includes(x.name))!.category!;
|
|
||||||
await this.szuru.updateTag(tags[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
tagsMsg.content = `Updated ${tags.length} tags`;
|
|
||||||
tagsMsg.type = "success";
|
|
||||||
}
|
|
||||||
} catch (ex) {
|
|
||||||
const error = ex as SzuruError;
|
|
||||||
|
|
||||||
if (error) {
|
|
||||||
this.clearMessages();
|
|
||||||
this.pushError(error.description);
|
|
||||||
} else {
|
|
||||||
// Probably too long for a message
|
|
||||||
alert(ex);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
// Open extension settings page in new tab
|
// Open extension settings page in new tab
|
||||||
async openSettings() {
|
async openSettings() {
|
||||||
const url = browser.extension.getURL("options/options.html");
|
const url = browser.extension.getURL("options/options.html");
|
||||||
|
|
@ -227,10 +172,10 @@ export default Vue.extend({
|
||||||
},
|
},
|
||||||
// Remove tag from post's taglist
|
// Remove tag from post's taglist
|
||||||
removeTag(tag: ScrapedTag) {
|
removeTag(tag: ScrapedTag) {
|
||||||
if (this.post) {
|
if (this.selectedPost) {
|
||||||
const idx = this.post.tags.indexOf(tag);
|
const idx = this.selectedPost.tags.indexOf(tag);
|
||||||
if (idx != -1) {
|
if (idx != -1) {
|
||||||
this.post.tags.splice(idx, 1);
|
this.selectedPost.tags.splice(idx, 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
@ -278,17 +223,17 @@ export default Vue.extend({
|
||||||
},
|
},
|
||||||
// Add tag to the post's taglist
|
// Add tag to the post's taglist
|
||||||
addTag() {
|
addTag() {
|
||||||
if (this.post) {
|
if (this.selectedPost) {
|
||||||
const tagName = this.addTagInput;
|
const tagName = this.addTagInput;
|
||||||
this.addTagInput = "";
|
this.addTagInput = "";
|
||||||
if (this.post.tags.find(x => x.name == tagName) == undefined) {
|
if (this.selectedPost.tags.find(x => x.name == tagName) == undefined) {
|
||||||
this.post.tags.push(new ScrapedTag(tagName));
|
this.selectedPost.tags.push(new ScrapedTag(tagName));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
// Find posts similar to the grabbed post (this.post)
|
// Find posts similar to the grabbed post (this.selectedPost)
|
||||||
async findSimilar() {
|
async findSimilar() {
|
||||||
if (!this.post || !this.szuru) {
|
if (!this.selectedPost || !this.szuru) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -307,7 +252,7 @@ export default Vue.extend({
|
||||||
}
|
}
|
||||||
|
|
||||||
const msgSearchSimilar = this.pushInfo("Searching for similar posts...");
|
const msgSearchSimilar = this.pushInfo("Searching for similar posts...");
|
||||||
const res = await this.szuru.reverseSearch(this.post.imageUrl);
|
const res = await this.szuru.reverseSearch(this.selectedPost.imageUrl);
|
||||||
this.removeMessage(msgSearchSimilar);
|
this.removeMessage(msgSearchSimilar);
|
||||||
|
|
||||||
if (!res.exactPost && res.similarPosts.length == 0) {
|
if (!res.exactPost && res.similarPosts.length == 0) {
|
||||||
|
|
@ -358,49 +303,47 @@ export default Vue.extend({
|
||||||
<style scoped>
|
<style scoped>
|
||||||
.popup-container {
|
.popup-container {
|
||||||
width: 700px;
|
width: 700px;
|
||||||
|
display: grid;
|
||||||
|
grid-template-columns: 400px 300px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.popup-row {
|
.popup-messages {
|
||||||
display: flex;
|
grid-column: 1 / 3;
|
||||||
|
margin: 0 10px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.popup-column1 {
|
.popup-left {
|
||||||
flex: 100%;
|
grid-column: 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
.popup-right {
|
||||||
|
grid-column: 2;
|
||||||
|
}
|
||||||
|
|
||||||
|
.popup-section {
|
||||||
margin: 10px;
|
margin: 10px;
|
||||||
}
|
}
|
||||||
|
|
||||||
.popup-column2 {
|
.section-header {
|
||||||
flex: 50%;
|
|
||||||
margin: 10px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.popup-header {
|
|
||||||
height: 30px;
|
height: 30px;
|
||||||
padding: 0 0 0 10px;
|
padding: 0 6px 0 10px;
|
||||||
background-color: #eee;
|
background-color: #eee;
|
||||||
margin: 0 0 10px 0;
|
|
||||||
|
display: flex;
|
||||||
|
justify-content: space-between;
|
||||||
|
align-items: center;
|
||||||
}
|
}
|
||||||
|
|
||||||
.popup-content {
|
.section-header > span {
|
||||||
margin: 5px;
|
font-size: 1.2em;
|
||||||
}
|
|
||||||
|
|
||||||
.popup-header > span {
|
|
||||||
vertical-align: middle;
|
vertical-align: middle;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Horrible hack + guesswork */
|
.section-row {
|
||||||
.popup-header > i {
|
margin: 10px 5px 0;
|
||||||
float: right;
|
|
||||||
margin: 8px 5px;
|
|
||||||
cursor: pointer;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.popup-block {
|
.section-label {
|
||||||
margin: 0 0 10px 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
.block-title {
|
|
||||||
display: block;
|
display: block;
|
||||||
margin: 0 0 5px 0;
|
margin: 0 0 5px 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,141 +0,0 @@
|
||||||
<template>
|
|
||||||
<div class="popup-container">
|
|
||||||
<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="post.rating" />
|
|
||||||
<span class="checkmark"></span>
|
|
||||||
Safe
|
|
||||||
</label>
|
|
||||||
|
|
||||||
<label class="container">
|
|
||||||
<input type="radio" value="sketchy" v-model="post.rating" />
|
|
||||||
<span class="checkmark"></span>
|
|
||||||
Sketchy
|
|
||||||
</label>
|
|
||||||
|
|
||||||
<label class="container">
|
|
||||||
<input type="radio" value="unsafe" v-model="post.rating" />
|
|
||||||
<span class="checkmark"></span>
|
|
||||||
Unsafe
|
|
||||||
</label>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="section-row">
|
|
||||||
<span class="section-label">Source</span>
|
|
||||||
<input type="text" v-model="post.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 post.tags" :key="tag.name">
|
|
||||||
<a class="remove-tag" @click="removeTag(tag)">x</a>
|
|
||||||
<span :class="getTagClasses(tag)">{{ tag.name }}</span>
|
|
||||||
<span class="tag-usages">0</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">
|
|
||||||
<img :src="post.imageUrl" />
|
|
||||||
|
|
||||||
<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 { ScrapedPost, ScrapedTag, ScrapeResults } from "neo-scraper";
|
|
||||||
|
|
||||||
export default Vue.extend({
|
|
||||||
data() {
|
|
||||||
return {
|
|
||||||
post: new ScrapedPost()
|
|
||||||
};
|
|
||||||
},
|
|
||||||
methods: {},
|
|
||||||
async mounted() {}
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<style scoped>
|
|
||||||
.popup-container {
|
|
||||||
width: 700px;
|
|
||||||
display: grid;
|
|
||||||
grid-template-columns: 400px 300px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.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 {
|
|
||||||
vertical-align: middle;
|
|
||||||
}
|
|
||||||
|
|
||||||
.section-row {
|
|
||||||
margin: 10px 5px 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
.section-label {
|
|
||||||
display: block;
|
|
||||||
margin: 0 0 5px 0;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
Loading…
Reference in New Issue