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",
|
"name": "szuru-chrome",
|
||||||
"version": "0.1.4",
|
"version": "0.2.0",
|
||||||
"description": "Import media from various sites into szurubooru",
|
"description": "Import media from various sites into szurubooru",
|
||||||
"author": "neobooru",
|
"author": "neobooru",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ export class Config {
|
||||||
importExistingTags: boolean = true;
|
importExistingTags: boolean = true;
|
||||||
importAllTags: boolean = false;
|
importAllTags: boolean = false;
|
||||||
importCommentary: boolean = false;
|
importCommentary: boolean = false;
|
||||||
|
autoSearchSimilar: boolean = false;
|
||||||
sites: Array<SzuruSiteConfig> = [];
|
sites: Array<SzuruSiteConfig> = [];
|
||||||
|
|
||||||
public async save() {
|
public async save() {
|
||||||
|
|
|
||||||
|
|
@ -90,3 +90,13 @@ export interface Post {
|
||||||
notes: any[]; // Note resource
|
notes: any[]; // Note resource
|
||||||
comments: any[]; // Comment 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 axios, { AxiosRequestConfig } from "axios";
|
||||||
import { ScrapedPost } from "neo-scraper";
|
import { ScrapedPost } from "neo-scraper";
|
||||||
import { SzuruSiteConfig } from "./Config";
|
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.
|
* A 1:1 wrapper around the szurubooru API.
|
||||||
|
|
@ -66,6 +66,14 @@ export default class SzuruWrapper {
|
||||||
return (await this.apiPost("posts", obj)).data;
|
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> {
|
private async apiGet(url: string, additionalHeaders: any = {}): Promise<any> {
|
||||||
const fullUrl = this.apiUrl + url;
|
const fullUrl = this.apiUrl + url;
|
||||||
const config: AxiosRequestConfig = {
|
const config: AxiosRequestConfig = {
|
||||||
|
|
|
||||||
|
|
@ -12,6 +12,8 @@ button {
|
||||||
font-family: "Open Sans", sans-serif;
|
font-family: "Open Sans", sans-serif;
|
||||||
height: 30px;
|
height: 30px;
|
||||||
cursor: pointer;
|
cursor: pointer;
|
||||||
|
padding: 0 10px;
|
||||||
|
white-space: nowrap;
|
||||||
}
|
}
|
||||||
|
|
||||||
button.primary {
|
button.primary {
|
||||||
|
|
|
||||||
|
|
@ -22,6 +22,15 @@
|
||||||
<input type="text" name="token" v-model="authToken" />
|
<input type="text" name="token" v-model="authToken" />
|
||||||
</label>
|
</label>
|
||||||
<br />
|
<br />
|
||||||
|
<label>
|
||||||
|
Automatically search for similar posts
|
||||||
|
<input
|
||||||
|
type="checkbox"
|
||||||
|
name="autoSearchSimilar"
|
||||||
|
v-model="autoSearchSimilar"
|
||||||
|
/>
|
||||||
|
</label>
|
||||||
|
<br />
|
||||||
<div class="settings-footer-buttons">
|
<div class="settings-footer-buttons">
|
||||||
<button @click="testConnection">Test connection</button>
|
<button @click="testConnection">Test connection</button>
|
||||||
<button @click="saveSettings" class="primary">Save settings</button>
|
<button @click="saveSettings" class="primary">Save settings</button>
|
||||||
|
|
@ -32,7 +41,7 @@
|
||||||
|
|
||||||
<script lang="ts">
|
<script lang="ts">
|
||||||
import Vue from "vue";
|
import Vue from "vue";
|
||||||
import { oc } from 'ts-optchain';
|
import { oc } from "ts-optchain";
|
||||||
import SzuruWrapper from "../SzuruWrapper";
|
import SzuruWrapper from "../SzuruWrapper";
|
||||||
import { Config, SzuruSiteConfig } from "../Config";
|
import { Config, SzuruSiteConfig } from "../Config";
|
||||||
|
|
||||||
|
|
@ -41,7 +50,8 @@ export default Vue.extend({
|
||||||
return {
|
return {
|
||||||
domain: "",
|
domain: "",
|
||||||
username: "",
|
username: "",
|
||||||
authToken: ""
|
authToken: "",
|
||||||
|
autoSearchSimilar: false
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
|
|
@ -75,6 +85,7 @@ export default Vue.extend({
|
||||||
siteConfig.username = this.username;
|
siteConfig.username = this.username;
|
||||||
siteConfig.authToken = this.authToken;
|
siteConfig.authToken = this.authToken;
|
||||||
config.sites = [siteConfig];
|
config.sites = [siteConfig];
|
||||||
|
config.autoSearchSimilar = this.autoSearchSimilar;
|
||||||
|
|
||||||
await config.save();
|
await config.save();
|
||||||
}
|
}
|
||||||
|
|
@ -89,6 +100,7 @@ export default Vue.extend({
|
||||||
this.domain = config.sites[0].domain;
|
this.domain = config.sites[0].domain;
|
||||||
this.username = config.sites[0].username;
|
this.username = config.sites[0].username;
|
||||||
this.authToken = config.sites[0].authToken;
|
this.authToken = config.sites[0].authToken;
|
||||||
|
this.autoSearchSimilar = config.autoSearchSimilar;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -87,8 +87,9 @@
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="popup-column2">
|
<div class="popup-column2" style="display: flex;">
|
||||||
<button class="primary full" @click="upload">Import</button>
|
<button class="primary" @click="findSimilar">Find similar</button>
|
||||||
|
<button class="primary full" style="margin-left: 5px;" @click="upload">Import</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
@ -125,6 +126,7 @@ export default Vue.extend({
|
||||||
};
|
};
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
|
// 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({
|
||||||
|
|
@ -146,6 +148,7 @@ export default Vue.extend({
|
||||||
this.pushError("Couldn't grab post");
|
this.pushError("Couldn't grab post");
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
// Try to upload the post to the selected szurubooru instance
|
||||||
async upload() {
|
async upload() {
|
||||||
this.clearMessages();
|
this.clearMessages();
|
||||||
|
|
||||||
|
|
@ -167,7 +170,7 @@ export default Vue.extend({
|
||||||
const createdPost = await this.szuru.createPost(this.post);
|
const createdPost = await this.szuru.createPost(this.post);
|
||||||
// TODO: Clicking a link doesn't actually open it in a new tab,
|
// TODO: Clicking a link doesn't actually open it in a new tab,
|
||||||
// see https://stackoverflow.com/questions/8915845
|
// 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";
|
uploadMsg.type = "success";
|
||||||
|
|
||||||
// Find tags with "default" category and update it
|
// Find tags with "default" category and update it
|
||||||
|
|
@ -206,10 +209,9 @@ export default Vue.extend({
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (this.messages.length > 0) {
|
this.scrollToMessages();
|
||||||
window.scrollTo(0, 0);
|
|
||||||
}
|
|
||||||
},
|
},
|
||||||
|
// 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");
|
||||||
window.open(url);
|
window.open(url);
|
||||||
|
|
@ -225,6 +227,7 @@ export default Vue.extend({
|
||||||
|
|
||||||
return classes;
|
return classes;
|
||||||
},
|
},
|
||||||
|
// Remove tag from post's taglist
|
||||||
removeTag(tag: ScrapedTag) {
|
removeTag(tag: ScrapedTag) {
|
||||||
if (this.post) {
|
if (this.post) {
|
||||||
const idx = this.post.tags.indexOf(tag);
|
const idx = this.post.tags.indexOf(tag);
|
||||||
|
|
@ -247,9 +250,11 @@ export default Vue.extend({
|
||||||
|
|
||||||
return classes;
|
return classes;
|
||||||
},
|
},
|
||||||
|
// Add info message
|
||||||
pushInfo(message: string) {
|
pushInfo(message: string) {
|
||||||
this.messages.push(new Message(message));
|
this.messages.push(new Message(message));
|
||||||
},
|
},
|
||||||
|
// Add error message
|
||||||
pushError(message: string) {
|
pushError(message: string) {
|
||||||
this.messages.push(new Message(message, "error"));
|
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.
|
// Searching for posts with re\:zero will show posts tagged with re:zero.
|
||||||
return encodeURIComponent(tagName.replace(/\:/g, "\\:"));
|
return encodeURIComponent(tagName.replace(/\:/g, "\\:"));
|
||||||
},
|
},
|
||||||
|
// Add tag to the post's taglist
|
||||||
addTag() {
|
addTag() {
|
||||||
|
if (this.post) {
|
||||||
const tagName = this.addTagInput;
|
const tagName = this.addTagInput;
|
||||||
this.addTagInput = "";
|
this.addTagInput = "";
|
||||||
if (this.post.tags.find(x => x.name == tagName) == undefined) {
|
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() {
|
async mounted() {
|
||||||
const cfg = await Config.load();
|
const cfg = await Config.load();
|
||||||
this.activeSite = cfg.sites.length > 0 ? cfg.sites[0] : null;
|
this.activeSite = cfg.sites.length > 0 ? cfg.sites[0] : null;
|
||||||
|
|
||||||
if (this.activeSite == null) {
|
if (!this.activeSite) {
|
||||||
this.pushError("No szurubooru server configured!");
|
this.pushError("No szurubooru server configured!");
|
||||||
} else {
|
} else {
|
||||||
this.szuru = await SzuruWrapper.createFromConfig(this.activeSite);
|
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>
|
</script>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue