Add 'find similar posts' feature
Can be configured to always run when opening popup on the extension's options page.feature-show_image_resolution v0.2.0
parent
695a842cd3
commit
7f90dbca56
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "szuru-chrome",
|
||||
"version": "0.1.4",
|
||||
"version": "0.2.0",
|
||||
"description": "Import media from various sites into szurubooru",
|
||||
"author": "neobooru",
|
||||
"license": "MIT",
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ export class Config {
|
|||
importExistingTags: boolean = true;
|
||||
importAllTags: boolean = false;
|
||||
importCommentary: boolean = false;
|
||||
autoSearchSimilar: boolean = false;
|
||||
sites: Array<SzuruSiteConfig> = [];
|
||||
|
||||
public async save() {
|
||||
|
|
|
|||
|
|
@ -90,3 +90,13 @@ export interface Post {
|
|||
notes: any[]; // Note resource
|
||||
comments: any[]; // Comment resource
|
||||
}
|
||||
|
||||
export interface SimilarPost {
|
||||
distance: number;
|
||||
post: Post;
|
||||
}
|
||||
|
||||
export interface ImageSearchResult {
|
||||
exactPost: Post | null;
|
||||
similarPosts: SimilarPost[];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,7 +1,7 @@
|
|||
import axios, { AxiosRequestConfig } from "axios";
|
||||
import { ScrapedPost } from "neo-scraper";
|
||||
import { SzuruSiteConfig } from "./Config";
|
||||
import { TagsResult, TagCategoriesResult, Post, Tag, SzuruError } from "./SzuruTypes";
|
||||
import { TagsResult, TagCategoriesResult, Post, Tag, SzuruError, ImageSearchResult } from "./SzuruTypes";
|
||||
|
||||
/**
|
||||
* A 1:1 wrapper around the szurubooru API.
|
||||
|
|
@ -66,6 +66,14 @@ export default class SzuruWrapper {
|
|||
return (await this.apiPost("posts", obj)).data;
|
||||
}
|
||||
|
||||
async reverseSearch(imageUrl: string): Promise<ImageSearchResult> {
|
||||
var obj = {
|
||||
contentUrl: imageUrl
|
||||
};
|
||||
|
||||
return (await this.apiPost("posts/reverse-search", obj)).data;
|
||||
}
|
||||
|
||||
private async apiGet(url: string, additionalHeaders: any = {}): Promise<any> {
|
||||
const fullUrl = this.apiUrl + url;
|
||||
const config: AxiosRequestConfig = {
|
||||
|
|
|
|||
|
|
@ -12,6 +12,8 @@ button {
|
|||
font-family: "Open Sans", sans-serif;
|
||||
height: 30px;
|
||||
cursor: pointer;
|
||||
padding: 0 10px;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
button.primary {
|
||||
|
|
|
|||
|
|
@ -22,6 +22,15 @@
|
|||
<input type="text" name="token" v-model="authToken" />
|
||||
</label>
|
||||
<br />
|
||||
<label>
|
||||
Automatically search for similar posts
|
||||
<input
|
||||
type="checkbox"
|
||||
name="autoSearchSimilar"
|
||||
v-model="autoSearchSimilar"
|
||||
/>
|
||||
</label>
|
||||
<br />
|
||||
<div class="settings-footer-buttons">
|
||||
<button @click="testConnection">Test connection</button>
|
||||
<button @click="saveSettings" class="primary">Save settings</button>
|
||||
|
|
@ -32,7 +41,7 @@
|
|||
|
||||
<script lang="ts">
|
||||
import Vue from "vue";
|
||||
import { oc } from 'ts-optchain';
|
||||
import { oc } from "ts-optchain";
|
||||
import SzuruWrapper from "../SzuruWrapper";
|
||||
import { Config, SzuruSiteConfig } from "../Config";
|
||||
|
||||
|
|
@ -41,7 +50,8 @@ export default Vue.extend({
|
|||
return {
|
||||
domain: "",
|
||||
username: "",
|
||||
authToken: ""
|
||||
authToken: "",
|
||||
autoSearchSimilar: false
|
||||
};
|
||||
},
|
||||
methods: {
|
||||
|
|
@ -75,6 +85,7 @@ export default Vue.extend({
|
|||
siteConfig.username = this.username;
|
||||
siteConfig.authToken = this.authToken;
|
||||
config.sites = [siteConfig];
|
||||
config.autoSearchSimilar = this.autoSearchSimilar;
|
||||
|
||||
await config.save();
|
||||
}
|
||||
|
|
@ -89,6 +100,7 @@ export default Vue.extend({
|
|||
this.domain = config.sites[0].domain;
|
||||
this.username = config.sites[0].username;
|
||||
this.authToken = config.sites[0].authToken;
|
||||
this.autoSearchSimilar = config.autoSearchSimilar;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
|
|||
|
|
@ -87,8 +87,9 @@
|
|||
</select>
|
||||
</div>
|
||||
|
||||
<div class="popup-column2">
|
||||
<button class="primary full" @click="upload">Import</button>
|
||||
<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>
|
||||
|
|
@ -125,6 +126,7 @@ export default Vue.extend({
|
|||
};
|
||||
},
|
||||
methods: {
|
||||
// Try to scrape the post from the page
|
||||
async grabPost() {
|
||||
// Get active tab
|
||||
const activeTab = (await browser.tabs.query({
|
||||
|
|
@ -146,6 +148,7 @@ export default Vue.extend({
|
|||
this.pushError("Couldn't grab post");
|
||||
}
|
||||
},
|
||||
// Try to upload the post to the selected szurubooru instance
|
||||
async upload() {
|
||||
this.clearMessages();
|
||||
|
||||
|
|
@ -167,7 +170,7 @@ export default Vue.extend({
|
|||
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)}'>Uploaded post</a>`;
|
||||
uploadMsg.content = `<a href='${this.getPostUrl(createdPost)}' target='_blank'>Uploaded post</a>`;
|
||||
uploadMsg.type = "success";
|
||||
|
||||
// Find tags with "default" category and update it
|
||||
|
|
@ -206,10 +209,9 @@ export default Vue.extend({
|
|||
}
|
||||
}
|
||||
|
||||
if (this.messages.length > 0) {
|
||||
window.scrollTo(0, 0);
|
||||
}
|
||||
this.scrollToMessages();
|
||||
},
|
||||
// Open extension settings page in new tab
|
||||
async openSettings() {
|
||||
const url = browser.extension.getURL("options/options.html");
|
||||
window.open(url);
|
||||
|
|
@ -225,6 +227,7 @@ export default Vue.extend({
|
|||
|
||||
return classes;
|
||||
},
|
||||
// Remove tag from post's taglist
|
||||
removeTag(tag: ScrapedTag) {
|
||||
if (this.post) {
|
||||
const idx = this.post.tags.indexOf(tag);
|
||||
|
|
@ -247,9 +250,11 @@ export default Vue.extend({
|
|||
|
||||
return classes;
|
||||
},
|
||||
// Add info message
|
||||
pushInfo(message: string) {
|
||||
this.messages.push(new Message(message));
|
||||
},
|
||||
// Add error message
|
||||
pushError(message: string) {
|
||||
this.messages.push(new Message(message, "error"));
|
||||
},
|
||||
|
|
@ -265,7 +270,9 @@ export default Vue.extend({
|
|||
// 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) {
|
||||
|
|
@ -273,17 +280,65 @@ export default Vue.extend({
|
|||
}
|
||||
}
|
||||
},
|
||||
// Find similar posts to the grabbed posts (this.post)
|
||||
async findSimilar() {
|
||||
if (!this.post || !this.szuru) {
|
||||
return;
|
||||
}
|
||||
|
||||
this.pushInfo("Searching for similar posts...");
|
||||
const res = await this.szuru.reverseSearch(this.post.imageUrl);
|
||||
this.clearMessages();
|
||||
|
||||
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>`
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
this.scrollToMessages();
|
||||
},
|
||||
// Scroll to top if there are any messages
|
||||
scrollToMessages() {
|
||||
if (this.messages.length > 0) {
|
||||
window.scrollTo(0, 0);
|
||||
}
|
||||
}
|
||||
},
|
||||
async mounted() {
|
||||
const cfg = await Config.load();
|
||||
this.activeSite = cfg.sites.length > 0 ? cfg.sites[0] : null;
|
||||
|
||||
if (this.activeSite == null) {
|
||||
if (!this.activeSite) {
|
||||
this.pushError("No szurubooru server configured!");
|
||||
} else {
|
||||
this.szuru = await SzuruWrapper.createFromConfig(this.activeSite);
|
||||
}
|
||||
|
||||
this.grabPost();
|
||||
// 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>
|
||||
|
|
|
|||
Loading…
Reference in New Issue