Cleanup + some setup required for the "change tag category" feature

feature-show_image_resolution
neobooru 2019-08-08 23:20:44 +02:00
parent 09814c0e29
commit e5467c5487
13 changed files with 134 additions and 88 deletions

View File

@ -56,7 +56,6 @@ Firefox: `about:debugging` -> "Load Temporary Add-on..." -> Select any file in t
- Auto-set tag category when creating a new tag (imported tag)
- Reverse image search to check whether (similar) file already exist before uploading it
- Right click any image on a page to import it
- Support more sites
- Configurable list of tags to always ignore (e.g. tagme, highres)
- [low priority] Import artist commentary
- [low priority] Import notes

31
src/LocalTypes.ts Normal file
View File

@ -0,0 +1,31 @@
/**
* These types are NOT compatible with szurubooru, they are only used in the WebExtension.
*/
import { Safety } from "./SzuruTypes";
export type LocalCategory = "copyright" | "character" | "artist" | "meta";
export class LocalTag {
name: string = "";
category?: string;
constructor(name: string, category?: string) {
this.name = name.replace(/\s/g, "_"); // Replace all spaces with underscores
this.category = category;
}
}
// FIXME: LocalPost -> valid szurubooru type
export class LocalPost {
safety: Safety = "safe";
source: string = "";
imageUrl: string = "";
tags: LocalTag[] = [];
}
export interface LocalError {
name: string;
title: string;
description: string;
}

View File

@ -1,39 +1,46 @@
export type SzuruSafety = "safe" | "sketchy" | "unsafe";
/**
* 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 class SzuruCategory {
export type Safety = "safe" | "sketchy" | "unsafe";
export interface PagedSearchResult<T> {
query: string;
offset: number;
limit: number;
total: number;
results: T[];
}
export interface UnpagedSearchResult<T> {
results: T[];
}
export interface MicroTag {
names: string[];
category: string;
usages: number;
}
export interface Tag {
names: string[];
category: string;
version: number;
description?: any;
creationTime: Date;
lastEditTime?: Date;
usages: number;
suggestions: MicroTag[];
implications: MicroTag[];
}
export interface TagCategory {
name: string;
version: number;
color: string;
private constructor(name: string, color: string = "") {
this.name = name;
this.color = color;
usages: number;
default: boolean;
}
static readonly Copyright = new SzuruCategory("copyright", "#a0a");
static readonly Character = new SzuruCategory("character", "#0a0");
static readonly Artist = new SzuruCategory("artist", "#a00");
static readonly Meta = new SzuruCategory("meta", "#f80");
}
export class SzuruTag {
name: string = "";
category?: SzuruCategory;
constructor(name: string, category?: SzuruCategory) {
this.name = name.replace(/\s/g, "_"); // Replace all spaces with underscores
this.category = category;
}
}
export class SzuruPost {
safety: SzuruSafety = "safe";
source: string = "";
imageUrl: string = "";
tags: SzuruTag[] = [];
}
export interface SzuruError {
name: string;
title: string;
description: string;
}
export type TagsResult = PagedSearchResult<Tag>
export type TagCategoriesResult = UnpagedSearchResult<TagCategory>

View File

@ -1,6 +1,7 @@
import axios, { AxiosRequestConfig, AxiosPromise, Method } from 'axios';
import { SzuruPost, SzuruError } from './SzuruTypes';
import axios, { AxiosRequestConfig } from 'axios';
import { LocalPost, LocalError } from './LocalTypes';
import { Config } from './Config';
import { TagsResult, TagCategoriesResult } from './SzuruTypes';
/**
* A 1:1 wrapper around the szurubooru API.
@ -32,10 +33,18 @@ export default class SzuruWrapper {
}
async getInfo(): Promise<any> {
return await this.apiGet("info");
return (await this.apiGet("info")).data;
}
async createPost(post: SzuruPost): Promise<void> {
async getTags(): Promise<TagsResult> {
return (await this.apiGet("tags")).data;
}
async getTagCategories(): Promise<TagCategoriesResult> {
return (await this.apiGet("tag-categories")).data;
}
async createPost(post: LocalPost): Promise<void> {
var obj = {
tags: post.tags.map(x => x.name),
safety: post.safety,
@ -82,7 +91,7 @@ export default class SzuruWrapper {
try {
return await axios(config);
} catch (ex) {
const error = ex.response.data as SzuruError;
const error = ex.response.data as LocalError;
throw error ? error : ex;
}
}

View File

@ -1,5 +1,5 @@
import ILoader from "./ILoader"
import { SzuruPost, SzuruTag, SzuruCategory } from "../SzuruTypes";
import { LocalPost, LocalTag, LocalCategory } from "../LocalTypes";
export default class Danbooru implements ILoader {
canImport(location: Location): boolean {
@ -9,8 +9,8 @@ export default class Danbooru implements ILoader {
);
}
async grabPost(dom: Document): Promise<SzuruPost | null> {
let post = new SzuruPost();
async grabPost(dom: Document): Promise<LocalPost | null> {
let post = new LocalPost();
post.source = dom.location.href;
// Set image url
@ -54,24 +54,24 @@ export default class Danbooru implements ILoader {
tagName = tagNameElement.innerText;
}
let category: SzuruCategory | undefined;
let category: LocalCategory | undefined;
switch (tagElement.className) {
case "category-3":
category = SzuruCategory.Copyright;
category = "copyright";
break;
case "category-4":
category = SzuruCategory.Character;
category = "character";
break;
case "category-1":
category = SzuruCategory.Artist;
category = "artist";
break;
case "category-5":
category = SzuruCategory.Meta;
category = "meta";
break;
}
if (tagName) {
let tag = new SzuruTag(tagName, category);
let tag = new LocalTag(tagName, category);
post.tags.push(tag);
}
}

View File

@ -1,5 +1,5 @@
import ILoader from "./ILoader"
import { SzuruPost, SzuruTag, SzuruCategory } from "../SzuruTypes";
import { LocalPost, LocalTag, LocalCategory } from "../LocalTypes";
// Small hack to avoid screwing with semver
enum Version {
@ -16,7 +16,7 @@ export default class Gelbooru implements ILoader {
);
}
async grabPost(dom: Document): Promise<SzuruPost | null> {
async grabPost(dom: Document): Promise<LocalPost | null> {
/**
* Make sure to use the `dom` variable and NOT `document`!
*/
@ -37,7 +37,7 @@ export default class Gelbooru implements ILoader {
console.log("Gelbooru guessed version: " + version);
let post = new SzuruPost();
let post = new LocalPost();
post.source = dom.location.href;
// Set image url (direct url to image)
@ -115,24 +115,24 @@ export default class Gelbooru implements ILoader {
tagName = el.getElementsByTagName("a")[1].innerText;
}
let category: SzuruCategory | undefined;
let category: LocalCategory | undefined;
switch (el.className) {
case "tag-type-copyright":
category = SzuruCategory.Copyright;
category = "copyright";
break;
case "tag-type-character":
category = SzuruCategory.Character;
category = "character";
break;
case "tag-type-artist":
category = SzuruCategory.Artist;
category = "artist";
break;
case "tag-type-metadata":
category = SzuruCategory.Meta;
category = "meta";
break;
}
let tag = new SzuruTag(tagName, category);
let tag = new LocalTag(tagName, category);
post.tags.push(tag);
}
}

View File

@ -1,6 +1,6 @@
import { SzuruPost } from "../SzuruTypes";
import { LocalPost } from "../LocalTypes";
export default interface ILoader {
canImport(location: Location): boolean;
grabPost(dom: Document): Promise<SzuruPost | null>;
grabPost(dom: Document): Promise<LocalPost | null>;
}

View File

@ -1,5 +1,5 @@
import ILoader from "./ILoader"
import { SzuruPost, SzuruTag, SzuruCategory } from "../SzuruTypes";
import { LocalPost, LocalTag, LocalCategory } from "../LocalTypes";
export default class Moebooru implements ILoader {
canImport(location: Location): boolean {
@ -9,8 +9,8 @@ export default class Moebooru implements ILoader {
);
}
async grabPost(dom: Document): Promise<SzuruPost | null> {
let post = new SzuruPost();
async grabPost(dom: Document): Promise<LocalPost | null> {
let post = new LocalPost();
post.source = dom.location.href;
// Set image url
@ -60,28 +60,28 @@ export default class Moebooru implements ILoader {
continue;
}
let category: SzuruCategory | undefined;
let category: LocalCategory | undefined;
// Loop over all classes because some sites (konachan)
// have mutliple classes, e.g. "tag-link tag-type-copyright"
for (const className of Array.from(el.classList)) {
switch (className) {
case "tag-type-copyright":
category = SzuruCategory.Copyright;
category = "copyright";
break;
case "tag-type-character":
category = SzuruCategory.Character;
category = "character";
break;
case "tag-type-artist":
category = SzuruCategory.Artist;
category = "artist";
break;
case "tag-type-metadata":
category = SzuruCategory.Meta;
category = "meta";
break;
}
}
let tag = new SzuruTag(tagName, category);
let tag = new LocalTag(tagName, category);
post.tags.push(tag);
}

View File

@ -1,5 +1,5 @@
import ILoader from "./ILoader"
import { SzuruPost, SzuruTag } from "../SzuruTypes";
import { LocalPost, LocalTag } from "../LocalTypes";
export default class Reddit implements ILoader {
canImport(location: Location): boolean {
@ -10,8 +10,8 @@ export default class Reddit implements ILoader {
);
}
async grabPost(dom: Document): Promise<SzuruPost | null> {
let post = new SzuruPost();
async grabPost(dom: Document): Promise<LocalPost | null> {
let post = new LocalPost();
post.source = dom.location.href;
// Set image url

View File

@ -1,13 +1,13 @@
import ILoader from "./ILoader"
import { SzuruPost, SzuruTag } from "../SzuruTypes";
import { LocalPost, LocalTag } from "../LocalTypes";
export default class Shimmie2 implements ILoader {
canImport(location: Location): boolean {
return location.host == "rule34.paheal.net";
}
async grabPost(dom: Document): Promise<SzuruPost | null> {
let post = new SzuruPost();
async grabPost(dom: Document): Promise<LocalPost | null> {
let post = new LocalPost();
post.source = dom.location.href;
// Set image url
@ -29,7 +29,7 @@ export default class Shimmie2 implements ILoader {
// For example by deleting 'wrong' tags or setting up some aliases in szurubooru.
post.tags = Array.from(dom.querySelectorAll("a.tag_name"))
.map(x => (x as HTMLAnchorElement).innerText.toLowerCase())
.map(x => new SzuruTag(x));
.map(x => new LocalTag(x));
// console.dir(post);

View File

@ -1,5 +1,5 @@
import ILoader from "./ILoader"
import { SzuruPost, SzuruTag } from "../SzuruTypes";
import { LocalPost, LocalTag } from "../LocalTypes";
function getOriginalImage(imageUrl: string) {
const x = imageUrl.replace(/:large|:med|:small|:orig$/, ""); // Remove `:large`, `:small` etc from url
@ -11,8 +11,8 @@ export default class Twitter implements ILoader {
return location.host == "twitter.com";
}
async grabPost(dom: Document): Promise<SzuruPost | null> {
let post = new SzuruPost();
async grabPost(dom: Document): Promise<LocalPost | null> {
let post = new LocalPost();
post.source = dom.location.href;
// Set image url

View File

@ -88,7 +88,7 @@
<script lang="ts">
import Vue from "vue";
import { browser } from "webextension-polyfill-ts";
import { SzuruPost, SzuruCategory, SzuruTag, SzuruError } from "../SzuruTypes";
import { LocalPost, LocalTag, LocalError } from "../LocalTypes";
import SzuruWrapper from "../SzuruWrapper";
type MessageType = "error" | "info";
@ -107,7 +107,7 @@ export default Vue.extend({
data() {
return {
szuru: null as SzuruWrapper | null,
post: new SzuruPost(),
post: new LocalPost(),
messages: [] as Message[]
};
},
@ -120,7 +120,7 @@ export default Vue.extend({
}))[0];
// Send 'grab_post' to the content script on the active tab
const post = (await browser.tabs.sendMessage(activeTab.id!, "grab_post")) as SzuruPost;
const post = (await browser.tabs.sendMessage(activeTab.id!, "grab_post")) as LocalPost;
if (post) {
this.post = post;
@ -148,7 +148,7 @@ export default Vue.extend({
// TODO: Add url to post page on szurubooru
this.pushInfo("Uploaded <a href='#'>post</a>");
} catch (ex) {
const error = ex as SzuruError;
const error = ex as LocalError;
if (error) {
this.pushError(error.description);
@ -166,18 +166,18 @@ export default Vue.extend({
const url = browser.extension.getURL("options/options.html");
window.open(url);
},
getTagClasses(tag: SzuruTag): string[] {
getTagClasses(tag: LocalTag): string[] {
let classes: string[] = [];
if (tag.category) {
classes.push("tag-" + tag.category.name);
classes.push("tag-" + tag.category);
} else {
classes.push("tag-general");
}
return classes;
},
removeTag(tag: SzuruTag) {
removeTag(tag: LocalTag) {
if (this.post) {
const idx = this.post.tags.indexOf(tag);
if (idx != -1) {

View File

@ -1,9 +1,9 @@
import { browser, Runtime } from "webextension-polyfill-ts";
import * as loaders from "../loaders";
import { SzuruPost } from "../SzuruTypes";
import { LocalPost } from "../LocalTypes";
import ILoader from "../loaders/ILoader";
async function grabPost(): Promise<SzuruPost | null> {
async function grabPost(): Promise<LocalPost | null> {
for (const loaderName in loaders) {
// HACK:
const loader = new (loaders as any)[loaderName]() as ILoader;