Add tag auto-complete feature

next
neobooru 2021-06-26 16:27:55 +02:00
parent 4381b4ebbf
commit bd8780e2ba
5 changed files with 1022 additions and 871 deletions

View File

@ -1,43 +1,43 @@
{ {
"name": "szuru-chrome", "name": "szuru-chrome",
"version": "0.7.0", "version": "0.7.1",
"description": "Import media from various sites into szurubooru.", "description": "Import media from various sites into szurubooru.",
"author": "neobooru", "author": "neobooru",
"license": "MIT", "license": "MIT",
"repository": { "repository": {
"url": "https://github.com/neobooru/SzuruChrome" "url": "https://github.com/neobooru/SzuruChrome"
}, },
"scripts": { "scripts": {
"build": "cross-env NODE_ENV=production webpack --hide-modules", "build": "cross-env NODE_ENV=production webpack --hide-modules",
"build:dev": "cross-env NODE_ENV=development webpack --hide-modules", "build:dev": "cross-env NODE_ENV=development webpack --hide-modules",
"build-zip": "node scripts/build-zip.js", "build-zip": "node scripts/build-zip.js",
"watch": "npm run build -- --watch", "watch": "npm run build -- --watch",
"watch:dev": "cross-env HMR=true npm run build:dev -- --watch" "watch:dev": "cross-env HMR=true npm run build:dev -- --watch"
}, },
"dependencies": { "dependencies": {
"axios": "^0.21.1", "axios": "^0.21.1",
"neo-scraper": "^0.4.4", "neo-scraper": "^0.4.4",
"vue": "^2.6.11", "vue": "^2.6.11",
"webextension-polyfill": "^0.3.1" "webextension-polyfill": "^0.3.1"
}, },
"devDependencies": { "devDependencies": {
"archiver": "^3.1.1", "archiver": "^3.1.1",
"copy-webpack-plugin": "^5.1.1", "copy-webpack-plugin": "^5.1.1",
"core-js": "^3.6.5", "core-js": "^3.6.5",
"cross-env": "^5.2.1", "cross-env": "^5.2.1",
"css-loader": "^2.1.1", "css-loader": "^2.1.1",
"file-loader": "^1.1.11", "file-loader": "^1.1.11",
"mini-css-extract-plugin": "^0.4.4", "mini-css-extract-plugin": "^0.4.4",
"node-sass": "^4.14.1", "node-sass": "^4.14.1",
"sass-loader": "^7.3.1", "sass-loader": "^7.3.1",
"ts-loader": "^6.2.2", "ts-loader": "^6.2.2",
"typescript": "^3.9.7", "typescript": "^3.9.7",
"vue-loader": "^15.9.3", "vue-loader": "^15.9.3",
"vue-template-compiler": "^2.6.11", "vue-template-compiler": "^2.6.11",
"web-ext-types": "^2.1.0", "web-ext-types": "^2.1.0",
"webextension-polyfill-ts": "^0.9.1", "webextension-polyfill-ts": "^0.9.1",
"webpack": "^4.43.0", "webpack": "^4.43.0",
"webpack-cli": "^3.3.12", "webpack-cli": "^3.3.12",
"webpack-extension-reloader": "^1.1.4" "webpack-extension-reloader": "^1.1.4"
} }
} }

View File

@ -1,107 +1,108 @@
/** /**
* Generated based on my setup, so some fields might be missing (but they also are unused so far so it doesn't really matter). * Generated based on my setup, so some fields might be missing (but they also are unused so far so it doesn't really matter).
*/ */
export type Safety = "safe" | "sketchy" | "unsafe"; export type Safety = "safe" | "sketchy" | "unsafe";
export type TagFields = "names" | "category" | "usages" | "implications";
export type TagsResult = PagedSearchResult<Tag>;
export type TagCategoriesResult = UnpagedSearchResult<TagCategory>; export type TagsResult = PagedSearchResult<Tag>;
export type TagCategoriesResult = UnpagedSearchResult<TagCategory>;
export interface SzuruError {
name: string; export interface SzuruError {
title: string; name: string;
description: string; title: string;
} description: string;
}
export interface PostAlreadyUploadedError extends SzuruError {
otherPostId: number; export interface PostAlreadyUploadedError extends SzuruError {
otherPostUrl: string; otherPostId: number;
} otherPostUrl: string;
}
export interface PagedSearchResult<T> {
query: string; export interface PagedSearchResult<T> {
offset: number; query: string;
limit: number; offset: number;
total: number; limit: number;
results: T[]; total: number;
} results: T[];
}
export interface UnpagedSearchResult<T> {
results: T[]; export interface UnpagedSearchResult<T> {
} results: T[];
}
export interface MicroTag {
names: string[]; export interface MicroTag {
category: string; names: string[];
usages: number; category: string;
} usages: number;
}
export interface Tag {
names: string[]; export interface Tag {
category: string; names: string[];
version: number; category: string;
description?: string; // Markdown version: number;
creationTime: Date; description?: string; // Markdown
lastEditTime?: Date; creationTime: Date;
usages: number; lastEditTime?: Date;
suggestions: MicroTag[]; usages: number;
implications: MicroTag[]; suggestions: MicroTag[];
} implications: MicroTag[];
}
export interface TagCategory {
name: string; export interface TagCategory {
version: number; name: string;
color: string; version: number;
usages: number; color: string;
default: boolean; usages: number;
} default: boolean;
}
export interface MicroUser {
name: string; export interface MicroUser {
avatarUrl: string; name: string;
} avatarUrl: string;
}
export interface Post {
id: number; export interface Post {
version: number; id: number;
creationTime: Date; version: number;
lastEditTime?: Date; creationTime: Date;
safety: Safety; lastEditTime?: Date;
source: string; safety: Safety;
type: string; source: string;
mimeType: string; type: string;
checksum: string; mimeType: string;
fileSize: number; checksum: string;
canvasWidth: number; fileSize: number;
canvasHeight: number; canvasWidth: number;
contentUrl: string; canvasHeight: number;
thumbnailUrl: string; contentUrl: string;
flags: string[]; thumbnailUrl: string;
tags: MicroTag[]; flags: string[];
relations: any[]; // MicroPost resource tags: MicroTag[];
user: MicroUser; relations: any[]; // MicroPost resource
score: number; user: MicroUser;
ownScore: number; score: number;
ownFavorite: boolean; ownScore: number;
tagCount: number; ownFavorite: boolean;
favoriteCount: number; tagCount: number;
commentCount: number; favoriteCount: number;
noteCount: number; commentCount: number;
relationCount: number; noteCount: number;
featureCount: number; relationCount: number;
lastFeatureTime?: Date; featureCount: number;
favoritedBy: MicroUser[]; lastFeatureTime?: Date;
hasCustomThumbnail: boolean; favoritedBy: MicroUser[];
notes: any[]; // Note resource hasCustomThumbnail: boolean;
comments: any[]; // Comment resource notes: any[]; // Note resource
} comments: any[]; // Comment resource
}
export interface SimilarPost {
distance: number; export interface SimilarPost {
post: Post; distance: number;
} post: Post;
}
export interface ImageSearchResult {
exactPost: Post | null; export interface ImageSearchResult {
similarPosts: SimilarPost[]; exactPost: Post | null;
} similarPosts: SimilarPost[];
}

View File

@ -1,130 +1,143 @@
import axios, { AxiosRequestConfig } from "axios"; import axios, { AxiosRequestConfig, CancelToken } 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, ImageSearchResult } from "./SzuruTypes"; import { TagsResult, TagCategoriesResult, Post, Tag, SzuruError, ImageSearchResult, TagFields } from "./SzuruTypes";
/** /**
* A 1:1 wrapper around the szurubooru API. * A 1:1 wrapper around the szurubooru API.
* *
* @class SzuruWrapper * @class SzuruWrapper
*/ */
export default class SzuruWrapper { export default class SzuruWrapper {
baseUrl: string; baseUrl: string;
apiUrl: string; apiUrl: string;
username: string; username: string;
authToken: string; authToken: string;
private readonly baseHeaders = { private readonly baseHeaders = {
Accept: "application/json", Accept: "application/json",
"Content-Type": "application/json" "Content-Type": "application/json"
}; };
/** /**
* Creates an instance of SzuruWrapper. * Creates an instance of SzuruWrapper.
* @param {string} baseUrl * @param {string} baseUrl
* @memberof SzuruWrapper * @memberof SzuruWrapper
*/ */
constructor(baseUrl: string, username: string, authToken: string) { constructor(baseUrl: string, username: string, authToken: string) {
const x = baseUrl.replace(/\/+$/, ""); // Trim trailing slashes, to make sure we only have one const x = baseUrl.replace(/\/+$/, ""); // Trim trailing slashes, to make sure we only have one
this.baseUrl = x + "/"; this.baseUrl = x + "/";
this.apiUrl = x + "/api/"; this.apiUrl = x + "/api/";
this.username = username; this.username = username;
this.authToken = authToken; this.authToken = authToken;
} }
async getInfo(): Promise<any> { async getInfo(): Promise<any> {
return (await this.apiGet("info")).data; return (await this.apiGet("info")).data;
} }
async updateTag(tag: Tag): Promise<any> { async updateTag(tag: Tag): Promise<any> {
return (await this.apiPut("tag/" + tag.names[0], tag)).data; return (await this.apiPut("tag/" + tag.names[0], tag)).data;
} }
async getTags(query: string, offset: number = 0): Promise<TagsResult> { async getTags(
const params = new URLSearchParams(); query: string,
params.append("offset", offset.toString()); offset: number = 0,
if (query) { limit: number = 100,
params.append("query", query); fields: TagFields[] | undefined = undefined,
} cancelToken: CancelToken | undefined = undefined
return (await this.apiGet("tags?" + params.toString())).data; ): Promise<TagsResult> {
} const params = new URLSearchParams();
params.append("offset", offset.toString());
async getTagCategories(): Promise<TagCategoriesResult> { params.append("limit", limit.toString());
return (await this.apiGet("tag-categories")).data;
} if (fields && fields.length > 0) params.append("fields", fields.join());
if (query) params.append("query", query);
async createPost(post: ScrapedPost): Promise<Post> {
var obj = { return (await this.apiGet("tags?" + params.toString(), {}, cancelToken)).data;
tags: post.tags.map(x => x.name), }
safety: post.rating,
source: post.source, async getTagCategories(): Promise<TagCategoriesResult> {
contentUrl: post.contentUrl return (await this.apiGet("tag-categories")).data;
}; }
console.log("Create new post object"); async createPost(post: ScrapedPost): Promise<Post> {
console.dir(obj); var obj = {
tags: post.tags.map(x => x.name),
return (await this.apiPost("posts", obj)).data; safety: post.rating,
} source: post.source,
contentUrl: post.contentUrl
async reverseSearch(contentUrl: string): Promise<ImageSearchResult> { };
var obj = {
contentUrl: contentUrl console.log("Create new post object");
}; console.dir(obj);
return (await this.apiPost("posts/reverse-search", obj)).data; return (await this.apiPost("posts", obj)).data;
} }
private async apiGet(url: string, additionalHeaders: any = {}): Promise<any> { async reverseSearch(contentUrl: string): Promise<ImageSearchResult> {
const fullUrl = this.apiUrl + url; var obj = {
const config: AxiosRequestConfig = { contentUrl: contentUrl
method: "GET", };
url: fullUrl
}; return (await this.apiPost("posts/reverse-search", obj)).data;
}
config.headers = { ...this.baseHeaders, ...additionalHeaders };
return this.execute(config); private async apiGet(
} url: string,
additionalHeaders: any = {},
private async apiPost(url: string, data: any, additionalHeaders: any = {}): Promise<any> { cancelToken: CancelToken | undefined = undefined
const fullUrl = this.apiUrl + url; ): Promise<any> {
const config: AxiosRequestConfig = { const fullUrl = this.apiUrl + url;
method: "POST", const config: AxiosRequestConfig = {
url: fullUrl, method: "GET",
data: data url: fullUrl,
}; cancelToken
};
config.headers = { ...this.baseHeaders, ...additionalHeaders };
return this.execute(config); config.headers = { ...this.baseHeaders, ...additionalHeaders };
} return this.execute(config);
}
private async apiPut(url: string, data: any, additionalHeaders: any = {}): Promise<any> {
const fullUrl = this.apiUrl + url; private async apiPost(url: string, data: any, additionalHeaders: any = {}): Promise<any> {
const config: AxiosRequestConfig = { const fullUrl = this.apiUrl + url;
method: "PUT", const config: AxiosRequestConfig = {
url: fullUrl, method: "POST",
data: data url: fullUrl,
}; data: data
};
config.headers = { ...this.baseHeaders, ...additionalHeaders };
return this.execute(config); config.headers = { ...this.baseHeaders, ...additionalHeaders };
} return this.execute(config);
}
private async execute(config: AxiosRequestConfig): Promise<any> {
if (this.username && this.authToken) { private async apiPut(url: string, data: any, additionalHeaders: any = {}): Promise<any> {
const token = "Token " + btoa(`${this.username}:${this.authToken}`); const fullUrl = this.apiUrl + url;
config.headers["Authorization"] = token; const config: AxiosRequestConfig = {
} method: "PUT",
url: fullUrl,
try { data: data
return await axios(config); };
} catch (ex) {
const error = ex.response.data as SzuruError; config.headers = { ...this.baseHeaders, ...additionalHeaders };
throw error ? error : ex; return this.execute(config);
} }
}
private async execute(config: AxiosRequestConfig): Promise<any> {
static async createFromConfig(siteConfig: SzuruSiteConfig): Promise<SzuruWrapper | null> { if (this.username && this.authToken) {
return new SzuruWrapper(siteConfig.domain, siteConfig.username, siteConfig.authToken); const token = "Token " + btoa(`${this.username}:${this.authToken}`);
} config.headers["Authorization"] = token;
} }
try {
return await axios(config);
} catch (ex) {
const error = ex.response.data as SzuruError;
throw error ? error : ex;
}
}
static async createFromConfig(siteConfig: SzuruSiteConfig): Promise<SzuruWrapper | null> {
return new SzuruWrapper(siteConfig.domain, siteConfig.username, siteConfig.authToken);
}
}

View File

@ -1,127 +1,132 @@
body { body {
margin: 0px; margin: 0px;
font-family: "Open Sans", sans-serif; font-family: "Open Sans", sans-serif;
display: block; display: block;
font-size: 100%; font-size: 100%;
} }
button { button {
background-color: #a2a2a2; background-color: #a2a2a2;
border: none; border: none;
color: white; color: white;
font-family: "Open Sans", sans-serif; font-family: "Open Sans", sans-serif;
height: 30px; height: 30px;
cursor: pointer; cursor: pointer;
padding: 0 10px; padding: 0 10px;
white-space: nowrap; white-space: nowrap;
} }
button.primary { button.primary {
background-color: #24aadd; background-color: #24aadd;
} }
input[type="text"] { input[type="text"] {
background-color: #fafafa; background-color: #fafafa;
border: 1px solid #eee; border: 1px solid #eee;
height: 26px; height: 26px;
width: 100%; width: 100%;
} }
img { input[type="text"]:focus {
max-width: 100%; outline: #24aadd solid 2px;
max-height: 100%; box-sizing: content-box;
} }
.full { img {
width: 100%; max-width: 100%;
} max-height: 100%;
}
select {
background-color: #fafafa; .full {
border: 1px solid #eee; width: 100%;
height: 30px; }
width: 100%;
} select {
background-color: #fafafa;
.compact-tags { border: 1px solid #eee;
list-style-type: none; height: 30px;
padding: 0; width: 100%;
display: inline; }
}
.compact-tags {
.tag-general { list-style-type: none;
color: #24aadd; padding: 0;
} display: inline;
}
.tag-copyright {
color: rgb(170, 0, 170); .tag-general {
} color: #24aadd;
}
.tag-character {
color: rgb(0, 170, 0); .tag-copyright {
} color: rgb(170, 0, 170);
}
.tag-artist {
color: rgb(170, 0, 0); .tag-character {
} color: rgb(0, 170, 0);
}
.tag-meta {
color: rgb(255, 128, 0); .tag-artist {
} color: rgb(170, 0, 0);
}
.tag-species {
color: rgb(17, 14, 247); .tag-meta {
} color: rgb(255, 128, 0);
}
.tag-lore {
color: rgb(48, 48, 48); .tag-species {
} color: rgb(17, 14, 247);
}
.remove-tag,
.tag-usages { .tag-lore {
color: #888; color: rgb(48, 48, 48);
} }
a.remove-tag { .remove-tag,
text-decoration: none; .tag-usages {
cursor: pointer; color: #888;
} }
.tag-usages { a.remove-tag {
font-size: 90%; text-decoration: none;
margin-left: 0.7em; cursor: pointer;
} }
.messages { .tag-usages {
list-style-type: none; font-size: 90%;
padding: 0; margin-left: 0.7em;
display: inline; }
}
.messages {
.messages > li { list-style-type: none;
padding: 5px; padding: 0;
border-radius: 2px; display: inline;
background: #24aadd; }
color: white;
} .messages > li {
padding: 5px;
.messages > li > a { border-radius: 2px;
color: white; background: #24aadd;
text-decoration: none; color: white;
} }
.messages > li:first-child { .messages > li > a {
margin-top: 10px; color: white;
} text-decoration: none;
}
.messages > li:not(:last-child) {
margin-bottom: 5px; .messages > li:first-child {
} margin-top: 10px;
}
.message-error {
background: #f44336 !important; .messages > li:not(:last-child) {
} margin-bottom: 5px;
}
.message-success {
background: #4caf50 !important; .message-error {
} background: #f44336 !important;
}
.message-success {
background: #4caf50 !important;
}

File diff suppressed because it is too large Load Diff