diff --git a/package.json b/package.json index cd8e493..c7ffc5b 100644 --- a/package.json +++ b/package.json @@ -1,43 +1,43 @@ -{ - "name": "szuru-chrome", - "version": "0.7.0", - "description": "Import media from various sites into szurubooru.", - "author": "neobooru", - "license": "MIT", - "repository": { - "url": "https://github.com/neobooru/SzuruChrome" - }, - "scripts": { - "build": "cross-env NODE_ENV=production webpack --hide-modules", - "build:dev": "cross-env NODE_ENV=development webpack --hide-modules", - "build-zip": "node scripts/build-zip.js", - "watch": "npm run build -- --watch", - "watch:dev": "cross-env HMR=true npm run build:dev -- --watch" - }, - "dependencies": { - "axios": "^0.21.1", - "neo-scraper": "^0.4.4", - "vue": "^2.6.11", - "webextension-polyfill": "^0.3.1" - }, - "devDependencies": { - "archiver": "^3.1.1", - "copy-webpack-plugin": "^5.1.1", - "core-js": "^3.6.5", - "cross-env": "^5.2.1", - "css-loader": "^2.1.1", - "file-loader": "^1.1.11", - "mini-css-extract-plugin": "^0.4.4", - "node-sass": "^4.14.1", - "sass-loader": "^7.3.1", - "ts-loader": "^6.2.2", - "typescript": "^3.9.7", - "vue-loader": "^15.9.3", - "vue-template-compiler": "^2.6.11", - "web-ext-types": "^2.1.0", - "webextension-polyfill-ts": "^0.9.1", - "webpack": "^4.43.0", - "webpack-cli": "^3.3.12", - "webpack-extension-reloader": "^1.1.4" - } -} +{ + "name": "szuru-chrome", + "version": "0.7.1", + "description": "Import media from various sites into szurubooru.", + "author": "neobooru", + "license": "MIT", + "repository": { + "url": "https://github.com/neobooru/SzuruChrome" + }, + "scripts": { + "build": "cross-env NODE_ENV=production webpack --hide-modules", + "build:dev": "cross-env NODE_ENV=development webpack --hide-modules", + "build-zip": "node scripts/build-zip.js", + "watch": "npm run build -- --watch", + "watch:dev": "cross-env HMR=true npm run build:dev -- --watch" + }, + "dependencies": { + "axios": "^0.21.1", + "neo-scraper": "^0.4.4", + "vue": "^2.6.11", + "webextension-polyfill": "^0.3.1" + }, + "devDependencies": { + "archiver": "^3.1.1", + "copy-webpack-plugin": "^5.1.1", + "core-js": "^3.6.5", + "cross-env": "^5.2.1", + "css-loader": "^2.1.1", + "file-loader": "^1.1.11", + "mini-css-extract-plugin": "^0.4.4", + "node-sass": "^4.14.1", + "sass-loader": "^7.3.1", + "ts-loader": "^6.2.2", + "typescript": "^3.9.7", + "vue-loader": "^15.9.3", + "vue-template-compiler": "^2.6.11", + "web-ext-types": "^2.1.0", + "webextension-polyfill-ts": "^0.9.1", + "webpack": "^4.43.0", + "webpack-cli": "^3.3.12", + "webpack-extension-reloader": "^1.1.4" + } +} diff --git a/src/SzuruTypes.ts b/src/SzuruTypes.ts index 70e8350..27479cc 100644 --- a/src/SzuruTypes.ts +++ b/src/SzuruTypes.ts @@ -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). - */ - -export type Safety = "safe" | "sketchy" | "unsafe"; - -export type TagsResult = PagedSearchResult; -export type TagCategoriesResult = UnpagedSearchResult; - -export interface SzuruError { - name: string; - title: string; - description: string; -} - -export interface PostAlreadyUploadedError extends SzuruError { - otherPostId: number; - otherPostUrl: string; -} - -export interface PagedSearchResult { - query: string; - offset: number; - limit: number; - total: number; - results: T[]; -} - -export interface UnpagedSearchResult { - results: T[]; -} - -export interface MicroTag { - names: string[]; - category: string; - usages: number; -} - -export interface Tag { - names: string[]; - category: string; - version: number; - description?: string; // Markdown - creationTime: Date; - lastEditTime?: Date; - usages: number; - suggestions: MicroTag[]; - implications: MicroTag[]; -} - -export interface TagCategory { - name: string; - version: number; - color: string; - usages: number; - default: boolean; -} - -export interface MicroUser { - name: string; - avatarUrl: string; -} - -export interface Post { - id: number; - version: number; - creationTime: Date; - lastEditTime?: Date; - safety: Safety; - source: string; - type: string; - mimeType: string; - checksum: string; - fileSize: number; - canvasWidth: number; - canvasHeight: number; - contentUrl: string; - thumbnailUrl: string; - flags: string[]; - tags: MicroTag[]; - relations: any[]; // MicroPost resource - user: MicroUser; - score: number; - ownScore: number; - ownFavorite: boolean; - tagCount: number; - favoriteCount: number; - commentCount: number; - noteCount: number; - relationCount: number; - featureCount: number; - lastFeatureTime?: Date; - favoritedBy: MicroUser[]; - hasCustomThumbnail: boolean; - notes: any[]; // Note resource - comments: any[]; // Comment resource -} - -export interface SimilarPost { - distance: number; - post: Post; -} - -export interface ImageSearchResult { - exactPost: Post | null; - similarPosts: SimilarPost[]; -} +/** + * 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 TagFields = "names" | "category" | "usages" | "implications"; + +export type TagsResult = PagedSearchResult; +export type TagCategoriesResult = UnpagedSearchResult; + +export interface SzuruError { + name: string; + title: string; + description: string; +} + +export interface PostAlreadyUploadedError extends SzuruError { + otherPostId: number; + otherPostUrl: string; +} + +export interface PagedSearchResult { + query: string; + offset: number; + limit: number; + total: number; + results: T[]; +} + +export interface UnpagedSearchResult { + results: T[]; +} + +export interface MicroTag { + names: string[]; + category: string; + usages: number; +} + +export interface Tag { + names: string[]; + category: string; + version: number; + description?: string; // Markdown + creationTime: Date; + lastEditTime?: Date; + usages: number; + suggestions: MicroTag[]; + implications: MicroTag[]; +} + +export interface TagCategory { + name: string; + version: number; + color: string; + usages: number; + default: boolean; +} + +export interface MicroUser { + name: string; + avatarUrl: string; +} + +export interface Post { + id: number; + version: number; + creationTime: Date; + lastEditTime?: Date; + safety: Safety; + source: string; + type: string; + mimeType: string; + checksum: string; + fileSize: number; + canvasWidth: number; + canvasHeight: number; + contentUrl: string; + thumbnailUrl: string; + flags: string[]; + tags: MicroTag[]; + relations: any[]; // MicroPost resource + user: MicroUser; + score: number; + ownScore: number; + ownFavorite: boolean; + tagCount: number; + favoriteCount: number; + commentCount: number; + noteCount: number; + relationCount: number; + featureCount: number; + lastFeatureTime?: Date; + favoritedBy: MicroUser[]; + hasCustomThumbnail: boolean; + notes: any[]; // Note resource + comments: any[]; // Comment resource +} + +export interface SimilarPost { + distance: number; + post: Post; +} + +export interface ImageSearchResult { + exactPost: Post | null; + similarPosts: SimilarPost[]; +} diff --git a/src/SzuruWrapper.ts b/src/SzuruWrapper.ts index 4c6f984..d88c407 100644 --- a/src/SzuruWrapper.ts +++ b/src/SzuruWrapper.ts @@ -1,130 +1,143 @@ -import axios, { AxiosRequestConfig } from "axios"; -import { ScrapedPost } from "neo-scraper"; -import { SzuruSiteConfig } from "./Config"; -import { TagsResult, TagCategoriesResult, Post, Tag, SzuruError, ImageSearchResult } from "./SzuruTypes"; - -/** - * A 1:1 wrapper around the szurubooru API. - * - * @class SzuruWrapper - */ -export default class SzuruWrapper { - baseUrl: string; - apiUrl: string; - username: string; - authToken: string; - - private readonly baseHeaders = { - Accept: "application/json", - "Content-Type": "application/json" - }; - - /** - * Creates an instance of SzuruWrapper. - * @param {string} baseUrl - * @memberof SzuruWrapper - */ - constructor(baseUrl: string, username: string, authToken: string) { - const x = baseUrl.replace(/\/+$/, ""); // Trim trailing slashes, to make sure we only have one - this.baseUrl = x + "/"; - this.apiUrl = x + "/api/"; - this.username = username; - this.authToken = authToken; - } - - async getInfo(): Promise { - return (await this.apiGet("info")).data; - } - - async updateTag(tag: Tag): Promise { - return (await this.apiPut("tag/" + tag.names[0], tag)).data; - } - - async getTags(query: string, offset: number = 0): Promise { - const params = new URLSearchParams(); - params.append("offset", offset.toString()); - if (query) { - params.append("query", query); - } - return (await this.apiGet("tags?" + params.toString())).data; - } - - async getTagCategories(): Promise { - return (await this.apiGet("tag-categories")).data; - } - - async createPost(post: ScrapedPost): Promise { - var obj = { - tags: post.tags.map(x => x.name), - safety: post.rating, - source: post.source, - contentUrl: post.contentUrl - }; - - console.log("Create new post object"); - console.dir(obj); - - return (await this.apiPost("posts", obj)).data; - } - - async reverseSearch(contentUrl: string): Promise { - var obj = { - contentUrl: contentUrl - }; - - return (await this.apiPost("posts/reverse-search", obj)).data; - } - - private async apiGet(url: string, additionalHeaders: any = {}): Promise { - const fullUrl = this.apiUrl + url; - const config: AxiosRequestConfig = { - method: "GET", - url: fullUrl - }; - - config.headers = { ...this.baseHeaders, ...additionalHeaders }; - return this.execute(config); - } - - private async apiPost(url: string, data: any, additionalHeaders: any = {}): Promise { - const fullUrl = this.apiUrl + url; - const config: AxiosRequestConfig = { - method: "POST", - url: fullUrl, - data: data - }; - - config.headers = { ...this.baseHeaders, ...additionalHeaders }; - return this.execute(config); - } - - private async apiPut(url: string, data: any, additionalHeaders: any = {}): Promise { - const fullUrl = this.apiUrl + url; - const config: AxiosRequestConfig = { - method: "PUT", - url: fullUrl, - data: data - }; - - config.headers = { ...this.baseHeaders, ...additionalHeaders }; - return this.execute(config); - } - - private async execute(config: AxiosRequestConfig): Promise { - if (this.username && this.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 { - return new SzuruWrapper(siteConfig.domain, siteConfig.username, siteConfig.authToken); - } -} +import axios, { AxiosRequestConfig, CancelToken } from "axios"; +import { ScrapedPost } from "neo-scraper"; +import { SzuruSiteConfig } from "./Config"; +import { TagsResult, TagCategoriesResult, Post, Tag, SzuruError, ImageSearchResult, TagFields } from "./SzuruTypes"; + +/** + * A 1:1 wrapper around the szurubooru API. + * + * @class SzuruWrapper + */ +export default class SzuruWrapper { + baseUrl: string; + apiUrl: string; + username: string; + authToken: string; + + private readonly baseHeaders = { + Accept: "application/json", + "Content-Type": "application/json" + }; + + /** + * Creates an instance of SzuruWrapper. + * @param {string} baseUrl + * @memberof SzuruWrapper + */ + constructor(baseUrl: string, username: string, authToken: string) { + const x = baseUrl.replace(/\/+$/, ""); // Trim trailing slashes, to make sure we only have one + this.baseUrl = x + "/"; + this.apiUrl = x + "/api/"; + this.username = username; + this.authToken = authToken; + } + + async getInfo(): Promise { + return (await this.apiGet("info")).data; + } + + async updateTag(tag: Tag): Promise { + return (await this.apiPut("tag/" + tag.names[0], tag)).data; + } + + async getTags( + query: string, + offset: number = 0, + limit: number = 100, + fields: TagFields[] | undefined = undefined, + cancelToken: CancelToken | undefined = undefined + ): Promise { + const params = new URLSearchParams(); + params.append("offset", offset.toString()); + params.append("limit", limit.toString()); + + if (fields && fields.length > 0) params.append("fields", fields.join()); + if (query) params.append("query", query); + + return (await this.apiGet("tags?" + params.toString(), {}, cancelToken)).data; + } + + async getTagCategories(): Promise { + return (await this.apiGet("tag-categories")).data; + } + + async createPost(post: ScrapedPost): Promise { + var obj = { + tags: post.tags.map(x => x.name), + safety: post.rating, + source: post.source, + contentUrl: post.contentUrl + }; + + console.log("Create new post object"); + console.dir(obj); + + return (await this.apiPost("posts", obj)).data; + } + + async reverseSearch(contentUrl: string): Promise { + var obj = { + contentUrl: contentUrl + }; + + return (await this.apiPost("posts/reverse-search", obj)).data; + } + + private async apiGet( + url: string, + additionalHeaders: any = {}, + cancelToken: CancelToken | undefined = undefined + ): Promise { + const fullUrl = this.apiUrl + url; + const config: AxiosRequestConfig = { + method: "GET", + url: fullUrl, + cancelToken + }; + + config.headers = { ...this.baseHeaders, ...additionalHeaders }; + return this.execute(config); + } + + private async apiPost(url: string, data: any, additionalHeaders: any = {}): Promise { + const fullUrl = this.apiUrl + url; + const config: AxiosRequestConfig = { + method: "POST", + url: fullUrl, + data: data + }; + + config.headers = { ...this.baseHeaders, ...additionalHeaders }; + return this.execute(config); + } + + private async apiPut(url: string, data: any, additionalHeaders: any = {}): Promise { + const fullUrl = this.apiUrl + url; + const config: AxiosRequestConfig = { + method: "PUT", + url: fullUrl, + data: data + }; + + config.headers = { ...this.baseHeaders, ...additionalHeaders }; + return this.execute(config); + } + + private async execute(config: AxiosRequestConfig): Promise { + if (this.username && this.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 { + return new SzuruWrapper(siteConfig.domain, siteConfig.username, siteConfig.authToken); + } +} diff --git a/src/css/style.css b/src/css/style.css index 88b1ecb..6b1b5e9 100644 --- a/src/css/style.css +++ b/src/css/style.css @@ -1,127 +1,132 @@ -body { - margin: 0px; - font-family: "Open Sans", sans-serif; - display: block; - font-size: 100%; -} - -button { - background-color: #a2a2a2; - border: none; - color: white; - font-family: "Open Sans", sans-serif; - height: 30px; - cursor: pointer; - padding: 0 10px; - white-space: nowrap; -} - -button.primary { - background-color: #24aadd; -} - -input[type="text"] { - background-color: #fafafa; - border: 1px solid #eee; - height: 26px; - width: 100%; -} - -img { - max-width: 100%; - max-height: 100%; -} - -.full { - width: 100%; -} - -select { - background-color: #fafafa; - border: 1px solid #eee; - height: 30px; - width: 100%; -} - -.compact-tags { - list-style-type: none; - padding: 0; - display: inline; -} - -.tag-general { - color: #24aadd; -} - -.tag-copyright { - color: rgb(170, 0, 170); -} - -.tag-character { - color: rgb(0, 170, 0); -} - -.tag-artist { - color: rgb(170, 0, 0); -} - -.tag-meta { - color: rgb(255, 128, 0); -} - -.tag-species { - color: rgb(17, 14, 247); -} - -.tag-lore { - color: rgb(48, 48, 48); -} - -.remove-tag, -.tag-usages { - color: #888; -} - -a.remove-tag { - text-decoration: none; - cursor: pointer; -} - -.tag-usages { - font-size: 90%; - margin-left: 0.7em; -} - -.messages { - list-style-type: none; - padding: 0; - display: inline; -} - -.messages > li { - padding: 5px; - border-radius: 2px; - background: #24aadd; - color: white; -} - -.messages > li > a { - color: white; - text-decoration: none; -} - -.messages > li:first-child { - margin-top: 10px; -} - -.messages > li:not(:last-child) { - margin-bottom: 5px; -} - -.message-error { - background: #f44336 !important; -} - -.message-success { - background: #4caf50 !important; -} +body { + margin: 0px; + font-family: "Open Sans", sans-serif; + display: block; + font-size: 100%; +} + +button { + background-color: #a2a2a2; + border: none; + color: white; + font-family: "Open Sans", sans-serif; + height: 30px; + cursor: pointer; + padding: 0 10px; + white-space: nowrap; +} + +button.primary { + background-color: #24aadd; +} + +input[type="text"] { + background-color: #fafafa; + border: 1px solid #eee; + height: 26px; + width: 100%; +} + +input[type="text"]:focus { + outline: #24aadd solid 2px; + box-sizing: content-box; +} + +img { + max-width: 100%; + max-height: 100%; +} + +.full { + width: 100%; +} + +select { + background-color: #fafafa; + border: 1px solid #eee; + height: 30px; + width: 100%; +} + +.compact-tags { + list-style-type: none; + padding: 0; + display: inline; +} + +.tag-general { + color: #24aadd; +} + +.tag-copyright { + color: rgb(170, 0, 170); +} + +.tag-character { + color: rgb(0, 170, 0); +} + +.tag-artist { + color: rgb(170, 0, 0); +} + +.tag-meta { + color: rgb(255, 128, 0); +} + +.tag-species { + color: rgb(17, 14, 247); +} + +.tag-lore { + color: rgb(48, 48, 48); +} + +.remove-tag, +.tag-usages { + color: #888; +} + +a.remove-tag { + text-decoration: none; + cursor: pointer; +} + +.tag-usages { + font-size: 90%; + margin-left: 0.7em; +} + +.messages { + list-style-type: none; + padding: 0; + display: inline; +} + +.messages > li { + padding: 5px; + border-radius: 2px; + background: #24aadd; + color: white; +} + +.messages > li > a { + color: white; + text-decoration: none; +} + +.messages > li:first-child { + margin-top: 10px; +} + +.messages > li:not(:last-child) { + margin-bottom: 5px; +} + +.message-error { + background: #f44336 !important; +} + +.message-success { + background: #4caf50 !important; +} diff --git a/src/popup/App.vue b/src/popup/App.vue index 1439990..e145e55 100644 --- a/src/popup/App.vue +++ b/src/popup/App.vue @@ -1,464 +1,596 @@ - - - - - + + + + +