Compare commits

..

41 Commits

Author SHA1 Message Date
Nes 633bf05dad Add updates.json 2026-10-30 15:05:16 -07:00
Nes370 37b359597c Update icon-128.png
Replaced default icon with Nol <3
2026-10-12 20:32:09 -07:00
neobooru c17bd87089 Update neo-scraper to 0.11.1 2026-08-19 00:41:28 +02:00
neobooru 59145cebdc Lock primevue to version 3.45.0
Needed because we override some of their styles. These overrides slightly changed in newer versions.
2026-08-19 00:13:40 +02:00
neobooru 4895e126fb Update dependencies + small fix 2026-08-19 00:07:46 +02:00
neobooru a0a0a591bc Run prettier 2026-08-18 23:25:03 +02:00
neobooru 381dac72d4 Update 'Tags to ignore' interface 2026-08-18 23:24:43 +02:00
G1org1owo 646241b45f PopupMain.vue: filter tags for each post based on tag ignores 2026-08-18 23:22:06 +02:00
G1org1owo 45f20ebfc8 Add config interface for tag ignores 2026-08-18 23:22:06 +02:00
G1org1owo 3d243252cd Add ability to select multiple tabs 2026-08-18 22:33:57 +02:00
neobooru 0f83292e86 Disable upload notifications by default 2026-08-18 22:23:49 +02:00
Keo Cheung be2afe55ff Add option to send browser notifications 2026-08-18 22:22:02 +02:00
Keo Cheung 5101c79c59 Send notifications after uploading 2026-08-18 22:22:02 +02:00
neobooru e2bf28a29b Update neo-scraper to 0.11.0 2026-07-31 02:18:44 +02:00
G1org1owo 1a6fe13512 Fix undefined tag usages access in PopupMain.vue 2025-08-27 22:15:36 +02:00
neobooru ce6a6c6521 Update neo-scraper to 0.10.1 2025-04-12 01:05:04 +02:00
neobooru 3aa31955ce Use ubuntu 24.04 for builds and fix artifact naming issue 2025-02-19 00:11:51 +01:00
neobooru 24cec0bd81 Update web-ext to v8 2025-02-19 00:06:51 +01:00
neobooru f4a1b2a1ea Update to neo-scraper v0.10.0
And try to rename build artifacts
2025-02-19 00:01:16 +01:00
neobooru e9d37caf7c Update actions/upload-artifact version 2025-02-17 00:30:49 +01:00
neobooru afe4ff535b Fix issue where merging new image content for existing posts did not work when uploadMode is set to "content" 2025-02-17 00:24:09 +01:00
shibao 9e07ee1594 add alwaysUploadAsContent option 2025-01-21 23:43:28 +01:00
neobooru 7c5c4d3ccd Fix pnpm-lock.yaml 2024-12-09 00:30:54 +01:00
neobooru 4bea802819 Update neo-scraper to 0.9.3 2024-12-08 18:58:45 +01:00
neobooru c08e6473e9 Update neo-scraper to fix yande.re issues 2024-07-11 23:10:09 +02:00
neobooru 76a4c01577 Fix linter warning 2024-07-10 20:34:00 +02:00
G1org1owo cae923b68a Refactor nesting in loadTagCounts() 2024-07-09 01:31:35 +02:00
G1org1owo 5ff664e725 Check for aliases when loading tag counts 2024-07-09 01:21:12 +02:00
neobooru eae1b3949f Update neo-scraper to 0.9.0 + post.uploadMode changes 2024-06-25 23:46:05 +02:00
neobooru 9843f0b641 Improved workaround for sites with hotlink protection or which require authentication 2024-06-14 16:59:57 +02:00
neobooru 3d9586147b Update danbooru tag category scraper 2024-01-09 23:28:12 +01:00
neobooru 4f16088a3b Fix typo 2024-01-04 20:36:11 +01:00
neobooru 390d865e5a Update lockfile 2024-01-01 23:41:58 +01:00
neobooru 1fa42c94a6 Workaround for new danbooru bot protection 2024-01-01 20:24:01 +01:00
neobooru b4e63c8c5e Update dependencies to latest major + css fix 2023-12-29 19:13:18 +01:00
neobooru 828e8c5a16 Allow hiding of select UI elements 2023-12-29 16:33:06 +01:00
neobooru dfd9c87858 Add strict_min_version for gecko and gecko_android 2023-12-29 15:40:35 +01:00
neobooru ee003912d5 Update dependencies and run prettier 2023-12-29 15:17:42 +01:00
neobooru c74926b8c1 Add pool feature in post upload view 2023-12-05 21:25:11 +01:00
neobooru b7f1ce3a21 Also run build action when package.json or lockfile changes 2023-12-01 23:24:35 +01:00
neobooru e745252038 Use neo-scraper 0.7.3 2023-12-01 23:22:07 +01:00
36 changed files with 6719 additions and 5619 deletions

View File

@ -23,5 +23,7 @@ module.exports = {
},
],
"@typescript-eslint/no-explicit-any": "off",
"eol-last": ["error", "always"],
"comma-dangle": ["error", "always-multiline"],
},
};

View File

@ -27,12 +27,12 @@ runs:
name: Install pnpm
id: pnpm-install
with:
version: 7
version: 9
run_install: false
- uses: actions/setup-node@v3
with:
node-version: "16"
node-version: "20"
cache: "pnpm"
- name: Install dependencies

View File

@ -4,6 +4,8 @@ on:
paths:
- src/**
- extension/**
- package.json
- pnpm-lock.yaml
workflow_dispatch:
inputs:
publish_amo_listed:
@ -13,7 +15,7 @@ on:
jobs:
build_chrome:
name: "Build Chrome Extension"
runs-on: ubuntu-20.04
runs-on: ubuntu-24.04
steps:
- name: Checkout
uses: actions/checkout@v2
@ -28,14 +30,14 @@ jobs:
- name: Pack ZIP
run: pnpm pack:zip
- uses: actions/upload-artifact@v3
- uses: actions/upload-artifact@v4
with:
name: Chrome Extension (Unsigned)
path: extension.zip
build_unlisted_firefox:
name: "Build Unlisted Firefox Extension"
runs-on: ubuntu-20.04
runs-on: ubuntu-24.04
# Doesn't really need the Chrome build, but we can reuse its cache.
needs: build_chrome
if: github.ref == 'refs/heads/master' || startsWith(github.ref, 'refs/tags/v')
@ -58,17 +60,20 @@ jobs:
WEB_EXT_API_KEY: ${{ secrets.WEB_EXT_API_KEY }}
WEB_EXT_API_SECRET: ${{ secrets.WEB_EXT_API_SECRET }}
- uses: actions/upload-artifact@v3
- name: Rename XPI
run: rename.ul szuruchrome_gh- SzuruChrome-v web-ext-artifacts/*.xpi
- uses: actions/upload-artifact@v4
with:
name: Firefox Extension (Signed)
path: web-ext-artifacts/*.xpi
build_amo_firefox:
name: "Build Firefox Extension for AMO"
runs-on: ubuntu-20.04
runs-on: ubuntu-24.04
# Doesn't really need the Chrome build, but we can reuse its cache.
needs: build_chrome
# Only run on master branch AND if tagging or manually dispatching.
# Only run (on master branch && if tagged) or (manually dispatching).
if: startsWith(github.ref, 'refs/tags/v') || github.event.inputs.publish_amo_listed == true
steps:
- name: Checkout

View File

@ -1,4 +1,4 @@
{
"printWidth": 120,
"tabWidth": 2
}
}

View File

@ -1,6 +1,3 @@
{
"recommendations": [
"vue.volar",
"dbaeumer.vscode-eslint",
]
"recommendations": ["vue.volar", "dbaeumer.vscode-eslint", "esbenp.prettier-vscode"]
}

View File

@ -1,6 +1,6 @@
{
"typescript.tsdk": "node_modules/typescript/lib",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": true
},
"source.fixAll.eslint": "explicit"
}
}

Binary file not shown.

Before

Width:  |  Height:  |  Size: 33 KiB

After

Width:  |  Height:  |  Size: 36 KiB

View File

@ -4,7 +4,6 @@
"description": "Import media from various sites into szurubooru.",
"version": "0.0.0",
"private": true,
"packageManager": "pnpm@7.8.0",
"scripts": {
"dev": "npm run clear && cross-env NODE_ENV=development run-p dev:*",
"dev:prepare": "esno scripts/prepare.ts",
@ -20,60 +19,63 @@
"pack:xpi": "cross-env WEB_EXT_ARTIFACTS_DIR=./ web-ext build --source-dir ./extension --filename extension.xpi --overwrite-dest",
"start:chromium": "web-ext run --source-dir ./extension --target=chromium",
"start:firefox": "web-ext run --source-dir ./extension --target=firefox-desktop",
"sign:firefox": "web-ext sign --use-submission-api --source-dir ./extension",
"sign:firefox": "web-ext sign --source-dir ./extension",
"clear": "rimraf extension/dist extension/manifest.json extension.*",
"lint": "eslint .",
"test": "vitest test"
"extlint": "web-ext lint -s extension",
"test": "vitest test",
"pretty": "prettier -w ."
},
"dependencies": {
"axios": "^1.2.2",
"byte-size": "^8.1.1",
"axios": "^1.19.0",
"byte-size": "^8.2.1",
"deepmerge": "^4.3.1",
"lodash": "^4.17.21",
"markdown-it": "^13.0.1",
"neo-scraper": "^0.7.1",
"lodash": "^4.18.1",
"markdown-it": "^14.3.0",
"neo-scraper": "^0.11.1",
"normalize.css": "^8.0.1",
"pinia": "^2.0.36",
"pinia": "^2.3.1",
"primeflex": "^3.3.1",
"primevue": "^3.33.0",
"vue-router": "4"
"primevue": "3.45.0",
"vue-router": "^4.6.4"
},
"devDependencies": {
"@ffflorian/jszip-cli": "^3.1.6",
"@fortawesome/fontawesome-svg-core": "^6.2.1",
"@fortawesome/free-solid-svg-icons": "^6.2.1",
"@fortawesome/vue-fontawesome": "^3.0.3",
"@types/byte-size": "^8.1.0",
"@types/fs-extra": "^9.0.13",
"@types/lodash": "^4.14.194",
"@types/markdown-it": "^12.2.3",
"@types/node": "^18.11.9",
"@types/webextension-polyfill": "^0.9.1",
"@typescript-eslint/eslint-plugin": "^5.44.0",
"@vitejs/plugin-vue": "^3.2.0",
"@vue/compiler-sfc": "^3.2.45",
"@vue/test-utils": "^2.2.4",
"@vueuse/core": "^9.6.0",
"chokidar": "^3.5.3",
"@ffflorian/jszip-cli": "^3.16.5",
"@fortawesome/fontawesome-svg-core": "^6.7.2",
"@fortawesome/free-solid-svg-icons": "^6.7.2",
"@fortawesome/vue-fontawesome": "^3.3.3",
"@types/byte-size": "^8.1.2",
"@types/fs-extra": "^11.0.4",
"@types/lodash": "^4.17.25",
"@types/markdown-it": "^13.0.9",
"@types/node": "^20.19.43",
"@types/webextension-polyfill": "^0.10.7",
"@typescript-eslint/eslint-plugin": "^6.21.0",
"@vitejs/plugin-vue": "^5.2.4",
"@vue/compiler-sfc": "^3.5.41",
"@vue/test-utils": "^2.4.11",
"@vueuse/core": "^10.11.1",
"chokidar": "^3.6.0",
"cross-env": "^7.0.3",
"crx": "^5.0.1",
"eslint": "^8.32.0",
"eslint-plugin-vue": "^9.9.0",
"esno": "^0.16.3",
"fs-extra": "^10.1.0",
"jsdom": "^20.0.3",
"kolorist": "^1.6.0",
"eslint": "^8.57.1",
"eslint-plugin-vue": "^9.33.0",
"esno": "^4.8.0",
"fs-extra": "^11.4.0",
"jsdom": "^23.2.0",
"kolorist": "^1.8.0",
"npm-run-all": "^4.1.5",
"prettier": "^3.9.6",
"rimraf": "^3.0.2",
"sass": "^1.57.1",
"typescript": "^4.9.3",
"unplugin-auto-import": "^0.12.2",
"unplugin-vue-components": "^0.23.0",
"vite": "^3.2.4",
"vitest": "^0.25.3",
"vue": "^3.2.45",
"vue-eslint-parser": "^9.1.0",
"web-ext": "^7.5.0",
"sass": "^1.102.0",
"typescript": "^5.9.3",
"unplugin-auto-import": "^0.17.8",
"unplugin-vue-components": "^0.26.0",
"vite": "^5.4.21",
"vitest": "^1.6.1",
"vue": "^3.5.41",
"vue-eslint-parser": "^9.4.3",
"web-ext": "^8.10.0",
"webextension-polyfill": "^0.10.0"
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,9 +1,9 @@
import { resolve } from 'path';
import { bgCyan, black } from 'kolorist';
import { resolve } from "path";
import { bgCyan, black } from "kolorist";
export const port = parseInt(process.env.PORT || '') || 3303;
export const r = (...args: string[]) => resolve(__dirname, '..', ...args);
export const isDev = process.env.NODE_ENV !== 'production';
export const port = parseInt(process.env.PORT || "") || 3303;
export const r = (...args: string[]) => resolve(__dirname, "..", ...args);
export const isDev = process.env.NODE_ENV !== "production";
export function log(name: string, message: string) {
console.log(black(bgCyan(` ${name} `)), message);

View File

@ -10,8 +10,13 @@ import {
TagFields,
TemporaryFileUploadResult,
UpdatePostRequest,
PoolsResult,
PoolFields,
Pool,
UpdatePoolRequest,
} from "./models";
import { ScrapedPostDetails, SzuruSiteConfig } from "~/models";
import { UploadMode } from "neo-scraper";
/**
* A 1:1 wrapper around the szurubooru API.
@ -63,7 +68,7 @@ export default class SzurubooruApi {
offset = 0,
limit = 100,
fields?: TagFields[],
cancelToken?: CancelToken
cancelToken?: CancelToken,
): Promise<TagsResult> {
const params = new URLSearchParams();
params.append("offset", offset.toString());
@ -99,6 +104,40 @@ export default class SzurubooruApi {
return (await this.apiPost("posts", obj)).data;
}
async createPool(name: string, category: string, posts?: number[]): Promise<Pool> {
const obj = <any>{
names: [name],
category,
};
if (posts) {
obj.posts = posts;
}
return (await this.apiPost("pool", obj)).data;
}
async getPools(
query?: string,
offset = 0,
limit = 100,
fields?: PoolFields[],
cancelToken?: CancelToken,
): Promise<PoolsResult> {
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("pools?" + params.toString(), {}, cancelToken)).data;
}
async updatePool(id: number, updateRequest: UpdatePoolRequest): Promise<Pool> {
return (await this.apiPut("pool/" + id, updateRequest)).data;
}
async reverseSearch(contentUrl: string): Promise<ImageSearchResult> {
const obj = { contentUrl };
return (await this.apiPost("posts/reverse-search", obj)).data;
@ -109,11 +148,42 @@ export default class SzurubooruApi {
return (await this.apiPost("posts/reverse-search", obj)).data;
}
async uploadTempFile(contentUrl: string): Promise<TemporaryFileUploadResult> {
async uploadTempFile(contentUrl: string, uploadMode: UploadMode): Promise<TemporaryFileUploadResult> {
// For some sources we need to download the image to the client and then upload it to szurubooru.
// We can't just pass the contentUrl because that would trigger the bot/hotlink protection.
if (uploadMode == "content") {
console.log("Upload from content");
return this.uploadTempFileFromContent(contentUrl);
} else {
console.log("Upload from URL");
return this.uploadTempFileFromUrl(contentUrl);
}
}
async uploadTempFileFromUrl(contentUrl: string): Promise<TemporaryFileUploadResult> {
const obj = { contentUrl };
return (await this.apiPost("uploads", obj)).data;
}
async uploadTempFileFromContent(contentUrl: string): Promise<TemporaryFileUploadResult> {
const content = await (await fetch(contentUrl)).blob();
const fullUrl = this.apiUrl + "uploads";
const formData = new FormData();
formData.append("content", content);
const config: AxiosRequestConfig = {
method: "POST",
url: fullUrl,
data: formData,
};
config.headers = { ...this.baseHeaders, "Content-Type": "multipart/form-data" };
return (await this.execute(config)).data;
}
static createFromConfig(siteConfig: SzuruSiteConfig): SzurubooruApi {
return new SzurubooruApi(siteConfig.domain, siteConfig.username, siteConfig.authToken);
}

View File

@ -3,9 +3,11 @@
*/
export type Safety = "safe" | "sketchy" | "unsafe";
export type TagFields = "names" | "category" | "usages" | "implications";
export type TagFields = "version" | "names" | "category" | "usages" | "implications";
export type PoolFields = "version" | "id" | "names" | "category" | "description" | "postCount" | "posts";
export type TagsResult = PagedSearchResult<Tag>;
export type PoolsResult = PagedSearchResult<Pool>;
export type TagCategoriesResult = UnpagedSearchResult<TagCategory>;
export interface SzuruError {
@ -37,18 +39,28 @@ export interface MicroTag {
usages: number;
}
export interface Tag {
names: string[];
category: string;
export interface Tag extends MicroTag {
version: number;
description?: string; // Markdown
creationTime: Date;
lastEditTime?: Date;
usages: number;
suggestions: MicroTag[];
implications: MicroTag[];
}
/**
* All fields can be optional.
*/
export interface Pool {
id: number;
names: string[];
category: string;
version: number;
description: null;
postCount: number;
posts: MicroPost[];
}
export interface TagCategory {
name: string;
version: number;
@ -62,8 +74,12 @@ export interface MicroUser {
avatarUrl: string;
}
export interface Post {
export interface MicroPost {
id: number;
thumbnailUrl: string;
}
export interface Post extends MicroPost {
version: number;
creationTime: Date;
lastEditTime?: Date;
@ -76,7 +92,6 @@ export interface Post {
canvasWidth: number;
canvasHeight: number;
contentUrl: string;
thumbnailUrl: string;
flags: string[];
tags: MicroTag[];
relations: any[]; // MicroPost resource
@ -127,3 +142,9 @@ export interface UpdatePostRequest {
contentUrl: string | undefined;
contentToken: string | undefined;
}
export interface UpdatePoolRequest {
version: number;
// names?: string[];
posts: number[];
}

View File

@ -1,4 +1,4 @@
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />

View File

@ -1,4 +1,4 @@
import { encodeTagName, getErrorMessage } from "~/utils";
import { encodeTagName, getErrorMessage, getUrl } from "~/utils";
import {
BrowserCommand,
PostUploadCommandData,
@ -8,7 +8,7 @@ import {
PostUpdateCommandData,
FetchCommandData,
} from "~/models";
import { PostAlreadyUploadedError } from "~/api/models";
import { PostAlreadyUploadedError, UpdatePoolRequest } from "~/api/models";
import SzurubooruApi from "~/api";
// Only on dev mode
@ -19,6 +19,13 @@ if (import.meta.hot) {
import("./contentScriptHMR");
}
browser.notifications.onClicked.addListener((url) => {
if (url) {
browser.tabs.create({ url });
browser.notifications.clear(url);
}
});
async function uploadPost(data: PostUploadCommandData) {
const info: PostUploadInfo = {
state: "uploading",
@ -26,7 +33,7 @@ async function uploadPost(data: PostUploadCommandData) {
const pushInfo = () =>
browser.runtime.sendMessage(
new BrowserCommand("set_post_upload_info", new SetPostUploadInfoData(data.selectedSite.id, data.post.id, info))
new BrowserCommand("set_post_upload_info", new SetPostUploadInfoData(data.selectedSite.id, data.post.id, info)),
);
try {
@ -36,6 +43,12 @@ async function uploadPost(data: PostUploadCommandData) {
pushInfo();
const contentToken = data.post.instanceSpecificData[data.selectedSite.id]?.contentToken;
if (!contentToken) {
// TODO: If donmai URL is the source then we need to do the uploadTempFileFromContent here!
console.log("contentToken is undefined. This could be a problem for some sources!");
}
const createdPost = await szuru.createPost(data.post, contentToken);
info.state = "uploaded";
@ -73,7 +86,7 @@ async function uploadPost(data: PostUploadCommandData) {
categoriesChangedCount++;
} else {
console.log(
`Not adding the '${wantedCategory}' category to the tag '${tags[i].names[0]}' because the szurubooru instance does not have this category.`
`Not adding the '${wantedCategory}' category to the tag '${tags[i].names[0]}' because the szurubooru instance does not have this category.`,
);
}
}
@ -84,11 +97,49 @@ async function uploadPost(data: PostUploadCommandData) {
pushInfo();
}
}
// TODO: This code shouldn't all be in the same try catch.
// Add post to pools
for (const scrapedPool of data.post.pools) {
// Attention! Don't use the .name getter as it does not exist. Just use names[0].
const existingPools = await szuru.getPools(encodeTagName(scrapedPool.names[0]), 0, 1, ["id", "posts", "version"]);
if (existingPools.results.length == 0) {
// Pool does not exist. Create a new pool and add the post to it in one API call.
console.log(`Creating new pool ${scrapedPool.names[0]} and adding post ${createdPost.id}.`);
await szuru.createPool(scrapedPool.names[0], "default", [createdPost.id]);
} else {
// Pool exists, so add it to the existing pool.
const existingPool = existingPools.results[0];
const posts = existingPool.posts.map((x) => x.id);
posts.push(createdPost.id);
console.log(`Adding post ${createdPost.id} to existing pool ${existingPool.id}`);
const updateRequest = <UpdatePoolRequest>{
version: existingPool.version,
posts,
};
await szuru.updatePool(existingPool.id, updateRequest);
}
}
if (data.sendNotifications && browser.notifications) {
const createdPostUrl = getUrl(data.selectedSite.domain, "post", createdPost.id.toString());
browser.notifications.create(createdPostUrl, {
type: "basic",
iconUrl: browser.runtime.getURL("assets/icon-128.png"),
title: data.selectedSite.domain,
message: `Uploaded ${data.post.contentUrl}`,
});
}
} catch (ex: any) {
console.error(ex);
if (ex.name && ex.name == "PostAlreadyUploadedError") {
const otherPostId = (ex as PostAlreadyUploadedError).otherPostId;
browser.runtime.sendMessage(
new BrowserCommand("set_exact_post_id", new SetExactPostId(data.selectedSite.id, data.post.id, otherPostId))
new BrowserCommand("set_exact_post_id", new SetExactPostId(data.selectedSite.id, data.post.id, otherPostId)),
);
// We don't set an error message, because we have a different message for posts that are already uploaded.
} else {
@ -110,8 +161,8 @@ async function updatePost(data: PostUpdateCommandData) {
browser.runtime.sendMessage(
new BrowserCommand(
"set_post_update_info",
new SetPostUploadInfoData(data.selectedSite.id, `merge-${data.postId}`, info)
)
new SetPostUploadInfoData(data.selectedSite.id, `merge-${data.postId}`, info),
),
);
try {
@ -124,6 +175,7 @@ async function updatePost(data: PostUpdateCommandData) {
info.state = "uploaded";
pushInfo();
} catch (ex: any) {
console.error(ex);
info.state = "error";
info.error = getErrorMessage(ex);
pushInfo();

View File

@ -0,0 +1,154 @@
<script setup lang="ts">
import axios, { CancelTokenSource } from "axios";
const inputText = ref("");
const autocompleteShown = ref(false);
const cancelSource = ref<CancelTokenSource | undefined>(undefined);
const autocompleteIndex = ref(-1);
const props = defineProps({
autocompleteItems: {
type: Array<any>,
required: true,
},
});
const emit = defineEmits(["addItem", "addFromCurrentInput", "autocompletePopulator"]);
function addItem(item: any) {
emit("addItem", item);
}
async function onAddItemKeyUp(e: KeyboardEvent) {
await autocompletePopulator((<HTMLInputElement>e.target).value);
}
function addItemFromCurrentInput() {
emit("addFromCurrentInput", inputText.value);
inputText.value = ""; // Reset input
// Only needed when the button is clicked
// When this is triggered by the enter key the `onAddItemKeyUp` will internally also hide the autocomplete.
// Though hiding it twice doesn't hurt so we don't care.
hideAutocomplete();
}
function onAddItemKeyDown(e: KeyboardEvent) {
if (e.code == "ArrowDown") {
e.preventDefault();
if (autocompleteIndex.value < props.autocompleteItems.length - 1) {
autocompleteIndex.value++;
}
} else if (e.code == "ArrowUp") {
e.preventDefault();
if (autocompleteIndex.value >= 0) {
autocompleteIndex.value--;
}
} else if (e.code == "Enter") {
if (autocompleteIndex.value == -1) {
addItemFromCurrentInput();
} else {
// Add auto completed item
const itemToAdd = props.autocompleteItems[autocompleteIndex.value];
addItem(itemToAdd);
inputText.value = ""; // Reset input
}
}
}
function onClickAutocompleteItem(item: any) {
addItem(item);
inputText.value = ""; // Reset input
autocompleteShown.value = false; // Hide autocomplete list
}
function hideAutocomplete() {
autocompleteIndex.value = -1;
autocompleteShown.value = false;
}
async function autocompletePopulator(input: string) {
// Based on https://www.w3schools.com/howto/howto_js_autocomplete.asp
// Hide autocomplete when the input is empty, and don't do anything else.
if (input.length == 0) {
hideAutocomplete();
return;
}
// Cancel previous request, not sure if this still works after the refactor.
if (cancelSource.value) {
cancelSource.value.cancel();
}
cancelSource.value = axios.CancelToken.source();
emit("autocompletePopulator", inputText.value, cancelSource.value.token);
}
watch(props, (newValue) => {
if (newValue.autocompleteItems.length > 0) {
autocompleteShown.value = true;
}
});
</script>
<template>
<div style="display: flex; flex-direction: column">
<div style="display: flex">
<input type="text" v-model="inputText" @keyup="onAddItemKeyUp" @keydown="onAddItemKeyDown" autocomplete="off" />
<button class="primary" style="margin-left: 5px" @click="addItemFromCurrentInput">Add</button>
</div>
<div class="autocomplete-items" v-bind:class="{ show: autocompleteShown }">
<div
v-for="(item, idx) in autocompleteItems"
@click="onClickAutocompleteItem(item)"
:key="item.name"
:class="{
active: idx == autocompleteIndex,
}"
>
<slot :item="item"></slot>
</div>
</div>
</div>
</template>
<style lang="scss">
.autocomplete-items {
position: absolute;
z-index: 10;
background-color: var(--bg-main-color);
border: 2px solid var(--primary-color);
margin-top: 34px;
display: none;
&.show {
display: block;
}
> div {
cursor: pointer;
padding: 2px 4px;
display: flex;
align-items: center;
gap: 0.5em;
&:hover {
background: var(--primary-color);
> span {
color: var(--text-color);
}
}
}
> div.active {
background: var(--primary-color);
> span {
color: var(--text-color);
}
}
}
</style>

View File

@ -0,0 +1,33 @@
<script setup lang="ts">
import type { PropType } from "vue";
import type { PoolDetails } from "~/models";
import { breakTagName } from "~/utils";
defineProps({
pools: Array as PropType<PoolDetails[]>,
showPostCount: Boolean,
showRemovePool: Boolean,
});
defineEmits(["removePool"]);
</script>
<template>
<ul class="compact-pools">
<li v-for="pool in pools" :key="pool.name">
<a v-if="showRemovePool" class="remove-tag" @click="$emit('removePool', pool)">x</a>
<span class="tag-general" v-html="breakTagName(pool.name)"></span>
<span v-if="showPostCount" class="tag-usages tag-usages-reserve-space">{{
pool.postCount ? pool.postCount : ""
}}</span>
</li>
</ul>
</template>
<style scoped lang="scss">
/* Hacky way to stop the layout from resizing when we lazy load the usage count. */
.tag-usages-reserve-space {
min-width: 5ch;
/* 5 character width */
display: inline-block;
}
</style>

View File

@ -24,7 +24,8 @@ defineEmits(["removeTag"]);
<style scoped lang="scss">
/* Hacky way to stop the layout from resizing when we lazy load the usage count. */
.tag-usages-reserve-space {
min-width: 5ch; /* 5 character width */
min-width: 5ch;
/* 5 character width */
display: inline-block;
}
</style>

View File

@ -0,0 +1,47 @@
<script setup lang="ts">
import { PropType } from "vue";
import { PoolDetails } from "~/models";
import { encodeTagName, getTagClasses } from "~/utils";
import SzurubooruApi from "~/api";
import { CancelToken } from "axios";
const autocompleteItems = ref<PoolDetails[]>([]);
const props = defineProps({
szuru: Object as PropType<SzurubooruApi>,
});
const emit = defineEmits(["addPool"]);
function addPool(tag: PoolDetails) {
emit("addPool", tag);
}
function addPoolFromCurrentInput(input: string) {
addPool(new PoolDetails([input]));
}
async function autocompletePopulator(input: string, ct: CancelToken) {
const query = decodeURIComponent(`*${encodeTagName(input)}*`);
const res = await props.szuru?.getPools(query, 0, 100, ["id", "names", "category", "description", "postCount"], ct);
if (res) {
// TODO: Maybe search on hamming distance or something?
autocompleteItems.value = res.results.map((x) => PoolDetails.fromPool(x));
} else {
autocompleteItems.value = [];
}
}
</script>
<template>
<AutocompleteInput
:autocomplete-items="autocompleteItems"
@add-item="addPool"
@add-from-current-input="addPoolFromCurrentInput"
@autocomplete-populator="autocompletePopulator"
v-slot="slotProps"
>
<span :class="getTagClasses(slotProps.item)">{{ slotProps.item.name }}</span>
<span class="tag-usages">{{ slotProps.item.usages ? slotProps.item.usages : "" }}</span>
</AutocompleteInput>
</template>

View File

@ -2,7 +2,10 @@
import { ScrapedNote } from "neo-scraper";
class Point {
constructor(public x: any, public y: any) {}
constructor(
public x: any,
public y: any,
) {}
}
// HACK: Copied from old client

View File

@ -1,15 +1,11 @@
<script setup lang="ts">
import axios, { CancelTokenSource } from "axios";
import { PropType } from "vue";
import SzurubooruApi from "~/api";
import { TagDetails } from "~/models";
import { encodeTagName, getTagClasses } from "~/utils";
import SzurubooruApi from "~/api";
import { CancelToken } from "axios";
const addTagInput = ref("");
const autocompleteShown = ref(false);
const autocompleteTags = ref<TagDetails[]>([]);
const cancelSource = ref<CancelTokenSource | undefined>(undefined);
const autocompleteIndex = ref(-1);
const autocompleteItems = ref<TagDetails[]>([]);
const props = defineProps({
szuru: Object as PropType<SzurubooruApi>,
@ -20,148 +16,33 @@ function addTag(tag: TagDetails) {
emit("addTag", tag);
}
async function onAddTagKeyUp(e: KeyboardEvent) {
await autocompletePopulator((<HTMLInputElement>e.target).value);
function addTagFromCurrentInput(input: string) {
addTag(new TagDetails([input]));
}
function addTagFromCurrentInput() {
addTag(new TagDetails([addTagInput.value]));
addTagInput.value = ""; // Reset input
// Only needed when the button is clicked
// When this is triggered by the enter key the `onAddTagKeyUp` will internally also hide the autocomplete.
// Though hiding it twice doesn't hurt so we don't care.
hideAutocomplete();
// TODO: Do we also want to add the implications?
// Answer: Probably not, because if the user wanted to have implications they should've clicked/selected the autocomplete entry.
}
function onAddTagKeyDown(e: KeyboardEvent) {
if (e.code == "ArrowDown") {
e.preventDefault();
if (autocompleteIndex.value < autocompleteTags.value.length - 1) {
autocompleteIndex.value++;
}
} else if (e.code == "ArrowUp") {
e.preventDefault();
if (autocompleteIndex.value >= 0) {
autocompleteIndex.value--;
}
} else if (e.code == "Enter") {
if (autocompleteIndex.value == -1) {
addTagFromCurrentInput();
} else {
// Add auto completed tag
const tagToAdd = autocompleteTags.value[autocompleteIndex.value];
addTag(tagToAdd);
addTagInput.value = ""; // Reset input
}
}
}
function onClickAutocompleteTagItem(tag: TagDetails) {
addTag(tag);
addTagInput.value = ""; // Reset input
autocompleteShown.value = false; // Hide autocomplete list
}
function hideAutocomplete() {
autocompleteIndex.value = -1;
autocompleteShown.value = false;
}
async function autocompletePopulator(input: string) {
// Based on https://www.w3schools.com/howto/howto_js_autocomplete.asp
// Hide autocomplete when the input is empty, and don't do anything else.
if (input.length == 0) {
hideAutocomplete();
return;
}
// Cancel previous request
if (cancelSource.value) {
cancelSource.value.cancel();
}
cancelSource.value = axios.CancelToken.source();
async function autocompletePopulator(input: string, ct: CancelToken) {
const query = decodeURIComponent(`*${encodeTagName(input)}* sort:usages`);
const res = await props.szuru?.getTags(
query,
0,
100,
["names", "category", "usages", "implications"],
cancelSource.value.token
);
const res = await props.szuru?.getTags(query, 0, 100, ["names", "category", "usages", "implications"], ct);
if (res) {
// TODO: Maybe search on hamming distance or something?
autocompleteTags.value = res.results.map((x) => TagDetails.fromTag(x));
if (autocompleteTags.value.length > 0) autocompleteShown.value = true;
autocompleteItems.value = res.results.map((x) => TagDetails.fromTag(x));
console.dir(autocompleteItems.value);
} else {
autocompleteTags.value = [];
autocompleteItems.value = [];
}
}
</script>
<template>
<div style="display: flex; flex-direction: column">
<div style="display: flex">
<input type="text" v-model="addTagInput" @keyup="onAddTagKeyUp" @keydown="onAddTagKeyDown" autocomplete="off" />
<button class="primary" style="margin-left: 5px" @click="addTagFromCurrentInput">Add</button>
</div>
<div class="autocomplete-items" v-bind:class="{ show: autocompleteShown }">
<div
v-for="(tag, idx) in autocompleteTags"
@click="onClickAutocompleteTagItem(tag)"
:key="tag.name"
:class="{
active: idx == autocompleteIndex,
}"
>
<span :class="getTagClasses(tag)">{{ tag.name }}</span>
<span class="tag-usages">{{ tag.usages ? tag.usages : "" }}</span>
</div>
</div>
</div>
<AutocompleteInput
:autocomplete-items="autocompleteItems"
@add-item="addTag"
@add-from-current-input="addTagFromCurrentInput"
@autocomplete-populator="autocompletePopulator"
v-slot="slotProps"
>
<span :class="getTagClasses(slotProps.item)">{{ slotProps.item.name }}</span>
<span class="tag-usages">{{ slotProps.item.usages ? slotProps.item.usages : "" }}</span>
</AutocompleteInput>
</template>
<style scoped lang="scss">
.autocomplete-items {
position: absolute;
z-index: 10;
background-color: var(--bg-main-color);
border: 2px solid var(--primary-color);
margin-top: 34px;
display: none;
&.show {
display: block;
}
> div {
cursor: pointer;
padding: 2px 4px;
display: flex;
align-items: center;
gap: 0.5em;
&:hover {
background: var(--primary-color);
> span {
color: var(--text-color);
}
}
}
> div.active {
background: var(--primary-color);
> span {
color: var(--text-color);
}
}
}
</style>

View File

@ -20,5 +20,5 @@ const storageLocal: StorageLikeAsync = {
export const useStorageLocal = <T>(
key: string,
initialValue: MaybeRef<T>,
options?: UseStorageAsyncOptions<T>
options?: UseStorageAsyncOptions<T>,
): RemovableRef<T> => useStorageAsync(key, initialValue, storageLocal, options);

View File

@ -31,7 +31,7 @@ export async function getManifest() {
page: "./dist/background/index.html",
// persistent: false,
},
permissions: ["storage", "activeTab", "webRequest", "webRequestBlocking", "<all_urls>"],
permissions: ["storage", "activeTab", "webRequest", "webRequestBlocking", "notifications", "<all_urls>"],
content_scripts: [
{
matches: ["http://*/*", "https://*/*"],
@ -44,6 +44,10 @@ export async function getManifest() {
manifest.browser_specific_settings = {
gecko: {
id: `{${process.env.SZ_GECKO_ID}}`,
strict_min_version: "113.0",
},
gecko_android: {
strict_min_version: "113.0",
},
};
}

View File

@ -1,10 +1,14 @@
import { BooruTypes, ContentType, ScrapedNote, ScrapedPost, ScrapedTag } from "neo-scraper";
import { MicroTag, Tag, UpdatePostRequest } from "~/api/models";
import { BooruTypes, ContentType, ScrapedNote, ScrapedPost, ScrapedTag, UploadMode } from "neo-scraper";
import { MicroTag, Pool, Tag, UpdatePostRequest } from "~/api/models";
export class TagDetails {
public implications: TagDetails[] = [];
constructor(public names: string[], public category?: string, public usages?: number) {}
constructor(
public names: string[],
public category?: string,
public usages?: number,
) {}
get name() {
return this.names[0];
@ -29,6 +33,22 @@ export class TagDetails {
}
}
export class PoolDetails {
constructor(
public names: string[],
public category?: string,
public postCount?: number,
) {}
get name() {
return this.names[0];
}
static fromPool(pool: Pool) {
return new PoolDetails(pool.names, pool.category, pool.postCount);
}
}
export class InstanceSpecificData {
contentToken?: string;
genericError?: string;
@ -44,6 +64,7 @@ export class ScrapedPostDetails {
id = window.crypto.randomUUID();
name = "";
tags: TagDetails[] = [];
pools: PoolDetails[] = [];
notes: ScrapedNote[];
contentUrl: string;
extraContentUrl: string | undefined;
@ -52,7 +73,8 @@ export class ScrapedPostDetails {
contentType: ContentType;
contentSubType: string | undefined;
rating: BooruTypes.SafetyRating;
source = "";
source;
uploadMode: UploadMode;
referrer?: string;
resolution?: [number, number];
instanceSpecificData: MappedInstanceSpecificData = {};
@ -69,6 +91,7 @@ export class ScrapedPostDetails {
this.tags = post.tags.map((x) => TagDetails.fromScapedTag(x));
this.notes = post.notes;
this.resolution = post.resolution;
this.uploadMode = post.uploadMode;
}
}
@ -83,7 +106,10 @@ export interface SimpleSimilarPost {
}
export class SimilarPostInfo {
constructor(public readonly id: number, public readonly percentage: number) {}
constructor(
public readonly id: number,
public readonly percentage: number,
) {}
}
export type PostUploadState = "uploading" | "uploaded" | "error";
@ -119,18 +145,26 @@ export class BrowserCommand<T = any> {
}
export class PostUploadCommandData {
constructor(public readonly post: ScrapedPostDetails, public readonly selectedSite: SzuruSiteConfig) {}
constructor(
public readonly post: ScrapedPostDetails,
public readonly selectedSite: SzuruSiteConfig,
public readonly sendNotifications: boolean,
) {}
}
export class SetPostUploadInfoData {
constructor(public instanceId: string, public postId: string, public info: PostUploadInfo) {}
constructor(
public instanceId: string,
public postId: string,
public info: PostUploadInfo,
) {}
}
export class SetExactPostId {
constructor(
public readonly instanceId: string,
public readonly postId: string,
public readonly exactPostId: number
public readonly exactPostId: number,
) {}
}
@ -138,12 +172,15 @@ export class PostUpdateCommandData {
constructor(
public readonly postId: number,
public readonly updateRequest: UpdatePostRequest,
public readonly selectedSite: SzuruSiteConfig
public readonly selectedSite: SzuruSiteConfig,
) {}
}
export class FetchCommandData {
constructor(public readonly url: string, public readonly options: RequestInit | undefined = undefined) {}
constructor(
public readonly url: string,
public readonly options: RequestInit | undefined = undefined,
) {}
}
export class SzuruSiteConfig {
@ -154,7 +191,10 @@ export class SzuruSiteConfig {
}
export class TagCategoryColor {
constructor(public name: string, public color: string) {}
constructor(
public name: string,
public color: string,
) {}
}
export const getDefaultTagCategories = () => [

View File

@ -8,9 +8,11 @@ import TabView from "primevue/tabview";
import TabPanel from "primevue/tabpanel";
import DataTable from "primevue/datatable";
import Column from "primevue/column";
import { SzuruSiteConfig, TagCategoryColor, getDefaultTagCategories } from "~/models";
import { TagDetails, SzuruSiteConfig, TagCategoryColor, getDefaultTagCategories } from "~/models";
import SzurubooruApi from "~/api";
type StatusType = "success" | "error" | "quiet";
const statusText = ref("");
const statusType = ref("status-quiet");
const versionInfo = "Version: " + (import.meta.env.VITE_SZ_VERSION ?? browser.runtime.getManifest().version);
@ -26,9 +28,7 @@ const selectedSite = computed(() => {
}
});
type StatusType = "success" | "error" | "quiet";
let mode = useColorMode({ emitAuto: true });
const mode = useColorMode({ emitAuto: true });
async function testConnection() {
if (
@ -102,6 +102,14 @@ async function importTagCategoriesFromInstance() {
}
}
function addTagIgnore() {
cfg.value.tagIgnores.push(new TagDetails([""]));
}
function resetTagIgnores() {
cfg.value.tagIgnores.splice(0);
}
// For debugging
const wnd = window as any;
wnd.szc_get_config = () => JSON.parse(JSON.stringify(cfg.value));
@ -110,33 +118,12 @@ wnd.szc_set_config_version = (v = 0) => (cfg.value.version = v);
<template>
<div class="content-holder">
<div class="content-wrapper flex flex-column gap-3">
<div class="content-wrapper flex flex-auto flex-column gap-3">
<div><strong>SzuruChrome Settings</strong></div>
<TabView>
<TabPanel header="General">
<div class="grid">
<div class="col-12 md:col-6">
<label>Theme</label>
<select v-model="mode">
<option value="auto">System</option>
<option value="light">Light</option>
<option value="dark">Dark</option>
</select>
<p class="hint">This setting auto-saves.</p>
</div>
<div class="col-12 md:col-6">
<label>
<input type="checkbox" v-model="cfg.autoSearchSimilar" />
Automatically search for similar posts
</label>
<p class="hint">
Automatically start searching for similar posts when opening the popup. Enabling this option will hide
the "Find Similar" button.
</p>
</div>
<div class="col-12 md:col-6">
<label>
<input type="checkbox" v-model="cfg.addPageUrlToSource" />
@ -160,6 +147,53 @@ wnd.szc_set_config_version = (v = 0) => (cfg.value.version = v);
</p>
</div>
<div class="col-12 md:col-6">
<label>
<input type="checkbox" v-model="cfg.alwaysUploadAsContent" />
Always upload as content
</label>
<p class="hint">This will always upload the image content directly, instead of as an URL</p>
</div>
<div class="col-12 md:col-6">
<label>
<input type="checkbox" v-model="cfg.sendUploadNotifications" />
Send browser notifications after uploading
</label>
<p class="hint">
This will send a browser notification after a successful upload. Clicking the notification will open the
uploaded post in a new tab.
</p>
</div>
<span class="col-12 status-quiet">{{ versionInfo }}</span>
</div>
</TabPanel>
<TabPanel header="Interface">
<div class="grid">
<div class="col-12 md:col-6 mb-2">
<label>Theme</label>
<select v-model="mode">
<option value="auto">System</option>
<option value="light">Light</option>
<option value="dark">Dark</option>
</select>
</div>
</div>
<div class="grid">
<div class="col-12 md:col-6">
<label>
<input type="checkbox" v-model="cfg.autoSearchSimilar" />
Automatically search for similar posts
</label>
<p class="hint">
Automatically start searching for similar posts when opening the popup. Enabling this option will hide
the "Find Similar" button.
</p>
</div>
<div class="col-12 md:col-6">
<label>
<input type="checkbox" v-model="cfg.loadTagCounts" />
@ -171,14 +205,33 @@ wnd.szc_set_config_version = (v = 0) => (cfg.value.version = v);
</p>
</div>
<span class="col-12 status-quiet">{{ versionInfo }}</span>
<div class="col-12 md:col-6">
<label>
<input type="checkbox" v-model="cfg.popup.showSource" />
Show source textbox in popup
</label>
</div>
<div class="col-12 md:col-6">
<label>
<input type="checkbox" v-model="cfg.popup.showPools" />
Show pools in popup
</label>
</div>
<div class="col-12 md:col-6">
<label>
<input type="checkbox" v-model="cfg.popup.showInstancePicker" />
Show instance picker in popup
</label>
</div>
</div>
</TabPanel>
<TabPanel header="Instances">
<div class="formgrid grid">
<div class="field col-12">
<label>Szurubooru Instances</label>
<label>Selected Szurubooru Instance</label>
<div class="flex gap-1">
<select v-model="cfg.selectedSiteId">
@ -223,52 +276,95 @@ wnd.szc_set_config_version = (v = 0) => (cfg.value.version = v);
</TabPanel>
<TabPanel header="Tags">
<div class="grid">
<DataTable :value="cfg.tagCategories" table-class="col-12" style="width: 100%">
<Column v-for="col of columns" :key="col.field" :field="col.field" :header="col.header">
<template #body="{ data, field }">
<template v-if="field == 'color'">
<div class="color-preview">
<input type="text" class="color" v-model="data[field]" />
<div
class="preview background-preview"
:style="{ 'border-color': data[field], 'background-color': data[field] }"
></div>
<div
class="preview text-preview"
:style="{ 'border-color': data[field], color: data[field] }"
></div>
</div>
<div class="separated-content">
<div class="grid">
<DataTable :value="cfg.tagCategories" table-class="col-12" style="width: 100%">
<Column v-for="col of columns" :key="col.field" :field="col.field" :header="col.header">
<template #body="{ data, field }">
<template v-if="field == 'color'">
<div class="color-preview">
<input type="text" class="color" v-model="data[field]" />
<div
class="preview background-preview"
:style="{ 'border-color': data[field], 'background-color': data[field] }"
></div>
<div
class="preview text-preview"
:style="{ 'border-color': data[field], color: data[field] }"
></div>
</div>
</template>
<template v-else>
<input type="text" :name="field" v-model="data[field]" />
</template>
</template>
</Column>
<template v-else>
<input type="text" :name="field" v-model="data[field]" />
<Column field="name">
<template #body="{ index }">
<a class="color-primary cursor-pointer" @click="() => cfg.tagCategories.splice(index, 1)">Remove</a>
</template>
</template>
</Column>
</Column>
</DataTable>
<Column field="name">
<template #body="{ index }">
<a class="color-primary cursor-pointer" @click="() => cfg.tagCategories.splice(index, 1)">Remove</a>
</template>
</Column>
</DataTable>
<div class="col-12 flex flex-wrap grid grid-nogutter gap-1">
<button class="primary" @click="addTagCategory">Add new category</button>
<button @click="importTagCategoriesFromInstance">Import from active instance</button>
<button class="bg-danger sm:ml-auto" @click="resetTagCategories">Reset to default</button>
<div class="col-12 flex flex-wrap grid grid-nogutter gap-1">
<button class="primary" @click="addTagCategory">Add new category</button>
<button @click="importTagCategoriesFromInstance">Import from active instance</button>
<button class="bg-danger sm:ml-auto" @click="resetTagCategories">Reset to default</button>
</div>
</div>
<!-- TODO: Tag category colors -->
<div class="grid">
<DataTable :value="cfg.tagIgnores" table-class="col-12" style="width: 100%">
<Column field="names" header="Tags to ignore">
<template #body="{ data, field }">
<input type="text" :name="field" v-model="data[field][0]" />
</template>
</Column>
<Column field="remove">
<template #body="{ index }">
<a class="color-primary cursor-pointer" @click="() => cfg.tagIgnores.splice(index, 1)">Remove</a>
</template>
</Column>
</DataTable>
<div class="col-12 flex flex-wrap grid grid-nogutter gap-1">
<button class="primary" @click="addTagIgnore">Add tag</button>
<button class="bg-danger sm:ml-auto" @click="resetTagIgnores">Clear</button>
</div>
</div>
<!-- TODO: Category ignore list -->
</div>
<!-- TODO: Tag category colors -->
<!-- TODO: Tag ignore list -->
<!-- TODO: Category ignore list -->
</TabPanel>
<!-- TODO: Settings import/export? -->
<!-- <TabPanel header="About">
<div class="grid">
<div v-for="engine in scraper.engines" :key="engine.name" class="col-12 md:col-4 flex flex-column">
<span class="font-bold">{{ engine.name }}</span>
<span>Features: {{ engine.features.join(", ") }}</span>
<span>Hosts: {{ engine.supportedHosts.join(", ") }}</span>
</div>
</div>
</TabPanel> -->
<!--
<TabPanel header="Developer">
<div class="flex flex-column gap-2">
<b>Config</b>
<Textarea v-model="configText" autoResize rows="16" />
<div class="flex flex-wrap gap-1">
<button class="primary" @click="showConfig">Show config</button>
<button class="bg-danger" @click="importConfig">Import config</button>
</div>
</div>
</TabPanel>
-->
</TabView>
</div>
</div>

View File

@ -1,4 +1,4 @@
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />

View File

@ -15,7 +15,7 @@ watch(
(value) => {
el = refreshTagCategoryColorMap(value.tagCategories, el);
},
{ immediate: true }
{ immediate: true },
);
onMounted(() => {
@ -93,7 +93,7 @@ onMounted(() => {
};
},
{ urls: ["<all_urls>"] },
extraInfoSpec
extraInfoSpec,
);
});
</script>

View File

@ -1,4 +1,4 @@
<!DOCTYPE html>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />

View File

@ -5,7 +5,7 @@ import { Post, UpdatePostRequest } from "~/api/models";
import { isMobile } from "~/env";
import { BrowserCommand, PostUpdateCommandData, TagDetails } from "~/models";
import { cfg, useMergeStore, usePopupStore } from "~/stores";
import { emptyPost, getUrl } from "~/utils";
import { emptyPost, ensurePostHasContentToken, getUrl } from "~/utils";
const props = defineProps(["siteId", "postId"]);
const merge = useMergeStore();
@ -129,7 +129,7 @@ async function mergeChanges() {
if (
!isEqual(
newTags,
existingPostRo.tags.map((x) => x.names[0])
existingPostRo.tags.map((x) => x.names[0]),
)
) {
req.tags = newTags;
@ -140,7 +140,10 @@ async function mergeChanges() {
}
if (imageToKeep.value == "new") {
req.contentUrl = scrapedPost?.contentUrl;
// In practice this should not be needed, because finding similar posts requires the use of content tokens.
// So if a similar post is found, it means that a contentToken has already been set.
await ensurePostHasContentToken(szuru, scrapedPost, cfg);
req.contentToken = scrapedPost.instanceSpecificData[cfg.value.selectedSiteId!].contentToken!;
}
if (!req.safety && !req.source && !(req.contentUrl || req.contentToken) && req.tags === undefined) {
@ -165,7 +168,7 @@ watch(
// TODO: Implement this.
sourcesAdded.value = 0;
sourcesRemoved.value = 0;
}
},
);
watch(
@ -176,7 +179,7 @@ watch(
} else if (existingPostRo) {
tagsToAdd.splice(0);
}
}
},
);
watch(
@ -187,7 +190,7 @@ watch(
} else if (existingPostRo) {
post.value.source = existingPostRo.source;
}
}
},
);
watch(
@ -198,7 +201,7 @@ watch(
} else if (existingPostRo) {
post.value.safety = existingPostRo.safety;
}
}
},
);
onMounted(async () => {

View File

@ -2,7 +2,7 @@
import { useDark } from "@vueuse/core";
import { cloneDeep } from "lodash";
import { ScrapeResults } from "neo-scraper";
import { getUrl, encodeTagName, getErrorMessage, getPostInfoSummary } from "~/utils";
import { getUrl, encodeTagName, getErrorMessage, getPostInfoSummary, ensurePostHasContentToken } from "~/utils";
import {
BrowserCommand,
ScrapedPostDetails,
@ -12,8 +12,8 @@ import {
SimpleImageSearchResult,
PostUploadCommandData,
SzuruSiteConfig,
PoolDetails,
} from "~/models";
import { ImageSearchResult } from "~/api/models";
import { isMobile } from "~/env";
import { DeepReadonly } from "vue";
import { cfg, usePopupStore } from "~/stores";
@ -40,7 +40,7 @@ const instanceSpecificData = readonly(
if (pop.selectedPost && cfg.value.selectedSiteId) {
return pop.selectedPost.instanceSpecificData[cfg.value.selectedSiteId];
}
})
}),
);
watch(
@ -51,7 +51,7 @@ watch(
let selectedPost = pop.posts.find((x) => x.id == value);
if (selectedPost) findSimilar(selectedPost);
}
}
},
);
watch(
@ -66,47 +66,63 @@ watch(
findSimilar(pop.selectedPost);
}
}
}
},
);
function openOptionsPage() {
browser.runtime.openOptionsPage();
}
async function getActiveTabId(): Promise<number> {
const activeTabs = await browser.tabs.query({
active: true,
async function getSelectedTabIds(): Promise<number[]> {
const selectedTabs = await browser.tabs.query({
highlighted: true,
currentWindow: true,
});
if (activeTabs.length > 0 && activeTabs[0].id) return activeTabs[0].id;
if (selectedTabs.length > 0 && selectedTabs[0].id) {
return selectedTabs.filter((tab) => tab.id).map((tab) => tab.id as number);
}
throw new Error("No active tab.");
}
async function grabPost() {
// Get active tab
const activeTabId = await getActiveTabId();
const selectedTabIds = await getSelectedTabIds();
// Send 'grab_post' to the content script on the active tab
const x = await browser.tabs.sendMessage(activeTabId, new BrowserCommand("grab_post"));
// We need to create a new ScrapeResults object and fill it with our data, because the get_posts()
// method gets 'lost' when sent from the contentscript to the popup (it gets JSON.stringified and any prototype defines are lost there)
const res = Object.assign(new ScrapeResults(), x);
let scrapeResults: ScrapeResults = new ScrapeResults();
for (const selectedTabId of selectedTabIds) {
// Send 'grab_post' to the content script on the active tabs
const x = await browser.tabs.sendMessage(selectedTabId, new BrowserCommand("grab_post"));
// We need to create a new ScrapeResults object and fill it with our data, because the get_posts()
// method gets 'lost' when sent from the contentscript to the popup (it gets JSON.stringified and any prototype defines are lost there)
const results: ScrapeResults = Object.assign(new ScrapeResults(), x);
console.dir(res);
for (const result of results.results) {
const res = scrapeResults.results.find((r) => r.engine === result.engine);
if (res) res.posts.push(...result.posts);
else scrapeResults.results.push(result);
}
}
console.dir(scrapeResults);
// Clear current posts array
pop.posts.splice(0);
for (const result of res.results) {
for (const result of scrapeResults.results) {
for (const i in result.posts) {
const vm = new ScrapedPostDetails(result.posts[i]);
vm.name = `[${result.engine}] Post ${parseInt(i) + 1}`; // parseInt() is required!
const name = result.posts[i].name ?? `Post ${parseInt(i) + 1}`; // parseInt() is required!
vm.name = `[${result.engine}] ${name}`;
if (!cfg.value.addAllParsedTags) {
vm.tags.splice(0);
}
if (cfg.value.alwaysUploadAsContent && vm.name !== "[fallback] Upload as URL") {
vm.uploadMode = "content";
}
// Add current pageUrl to the source if either
// a. user has addPageUrlToSource set to true
// b. source is empty
@ -120,6 +136,14 @@ async function grabPost() {
vm.instanceSpecificData[site.id] = {};
}
// Remove ignored tags
// The complexity for this is quite bad but all the other ways I tried were even worse
vm.tags = vm.tags.filter((tag) =>
cfg.value.tagIgnores.every(
(ignoredTag) => tag.name != ignoredTag.names[0], // no getters after deserializing
),
);
pop.posts.push(vm);
}
}
@ -136,7 +160,7 @@ async function grabPost() {
}
if (cfg.value.fetchPostInfo) {
await fetchPostInfo();
await fetchPostsInfo();
}
// Delayed call to findSimilar, as fetchPostInfo might change the post's contentUrl.
@ -159,8 +183,18 @@ async function upload() {
}
try {
const post: any = cloneDeep(pop.selectedPost);
const cmdData = new PostUploadCommandData(post, <SzuruSiteConfig>cloneDeep(selectedSite.value));
const post: ScrapedPostDetails = cloneDeep(pop.selectedPost)!;
// uploadMode "content" requires a content token to work. So ensure it is set.
if (post.uploadMode === "content") {
await ensurePostHasContentToken(szuru.value!, post, cfg);
}
const cmdData = new PostUploadCommandData(
post,
<SzuruSiteConfig>cloneDeep(selectedSite.value),
cfg.value.sendUploadNotifications,
);
const cmd = new BrowserCommand("upload_post", cmdData);
await browser.runtime.sendMessage(cmd);
} catch (ex: any) {
@ -181,6 +215,15 @@ function removeTag(tag: TagDetails) {
}
}
function removePool(pool: PoolDetails) {
if (pop.selectedPost) {
const idx = pop.selectedPost.pools.indexOf(pool);
if (idx != -1) {
pop.selectedPost.pools.splice(idx, 1);
}
}
}
function getActiveSitePostUrl(postId: number): string {
if (!selectedSite.value) return "";
return getUrl(selectedSite.value.domain, "post", postId.toString());
@ -216,6 +259,15 @@ function addTag(tag: TagDetails) {
}
}
function addPool(pool: PoolDetails) {
if (pop.selectedPost) {
// Only add pool if it doesn't already exist
if (pool.name.length > 0 && pop.selectedPost.pools.find((x) => x.name == pool.name) == undefined) {
pop.selectedPost.pools.push(pool);
}
}
}
async function clickFindSimilar() {
if (pop.selectedPost) return await findSimilar(pop.selectedPost);
}
@ -224,6 +276,10 @@ async function findSimilar(post: ScrapedPostDetails | undefined) {
if (!post || !szuru.value || !cfg.value.selectedSiteId) return;
// TODO: This code might not work.
// Why is toRaw used?
// I guess it could happen that the selectedInstance changes halfway during this function?
// And in that case toRaw would ensure we have the original selected instance (as it was at the start of this function).
// In theory this sounds correct, but idk if it is needed in practice.
const selectedInstance = toRaw(szuru.value); // Get current object, and not reactive.
let instanceSpecificData = post.instanceSpecificData[cfg.value.selectedSiteId];
@ -233,23 +289,14 @@ async function findSimilar(post: ScrapedPostDetails | undefined) {
}
// Don't load this again if it is already loaded.
if (instanceSpecificData?.reverseSearchResult) return;
if (instanceSpecificData.reverseSearchResult) return;
isSearchingForSimilarPosts.value++;
try {
let res: ImageSearchResult | undefined;
await ensurePostHasContentToken(selectedInstance, post, cfg);
if (cfg.value.useContentTokens) {
let tmpRes = await selectedInstance.uploadTempFile(post.contentUrl);
// TODO: Error handling?
// Save contentToken in PostViewModel so that we can reuse it when creating/uploading the post.
instanceSpecificData.contentToken = tmpRes.token;
res = await selectedInstance.reverseSearchToken(tmpRes.token);
} else {
res = await selectedInstance.reverseSearch(post.contentUrl);
}
const res = await selectedInstance.reverseSearchToken(instanceSpecificData.contentToken!);
instanceSpecificData.reverseSearchResult = {
exactPostId: res.exactPost?.id,
@ -276,43 +323,66 @@ async function loadTagCounts() {
// Not the best code, but it works I guess?
if (resp) {
for (let post of pop.posts)
for (let tag of resp.results)
for (let postTag of post.tags)
if (postTag.name == tag.names[0]) {
postTag.usages = tag.usages;
break;
}
for (let tag of resp.results) {
const postTag = post.tags.find((postTag) => tag.names.includes(postTag.name));
if (postTag) postTag.usages = tag.usages;
}
}
}
}
async function fetchPostInfo() {
async function updatePostWithRemoteInfo(post: ScrapedPostDetails, contentUrl: string) {
// We are missing cookies/etc which means that the request might fail.
// If you want access to the cookies/session/etc then you need to execute this code inside the content script.
try {
// This request follows redirects.
const res = await fetch(contentUrl, { method: "HEAD" });
const size = res.headers.get("Content-Length");
const type = res.headers.get("Content-Type");
if (type) {
if (type.indexOf("text/html") != -1) {
// If we get a HTML page back it usually means that the request failed.
// Not all sites (e.g. e-hentai) reply with a 403.
throw new Error(
"Received a text/html content type. This probably means that we don't have permission to access the resource.",
);
}
const [_main, sub] = type.split("/");
if (sub) post.contentSubType = sub.toUpperCase();
}
// This should be after the type check, because the type check also checks whether the response we get is valid/unsable.
if (size) post.contentSize = parseInt(size);
// Update url if it changes. The url can change when:
// a. the extraContentUrl was successfully loaded
// b. because of redirects
if (res.url != post.contentUrl) {
// The developer needs to make sure that the remote server can load this URL if `post.uploadMode == 'url'`!
console.log(`Updating post.contentUrl to '${res.url}'`);
post.contentUrl = res.url;
}
return true;
} catch (ex) {
console.error(ex);
return false;
}
}
async function fetchPostsInfo() {
for (const post of pop.posts) {
if (!post.contentSize || post.extraContentUrl) {
// We are missing cookies/etc which means that the request might fail.
// If you want access to the cookies/session/etc then you need to execute this code inside the content script.
try {
const contentUrl = post.extraContentUrl ?? post.contentUrl;
// TODO: Check whether we need to call this in the background page, or if it is fine to call it from the popup.
const res = await fetch(contentUrl, { method: "HEAD" });
const size = res.headers.get("Content-Length");
const type = res.headers.get("Content-Type");
if (size) post.contentSize = parseInt(size);
if (type) {
const [_main, sub] = type.split("/");
if (sub) post.contentSubType = sub.toUpperCase();
}
// Resolve extraContentUrl redirects.
if (post.extraContentUrl && res.url != post.contentUrl) {
post.contentUrl = res.url;
}
} catch (ex) {
// TODO: Maybe display an error in the UI.
console.error(ex);
let ok = false;
if (post.extraContentUrl) {
// Prioritize info from extraContentUrl.
ok = await updatePostWithRemoteInfo(post, post.extraContentUrl);
}
if (!ok) {
// Fall back to normal contentUrl if we can't get the data from the extraContentUrl.
await updatePostWithRemoteInfo(post, post.contentUrl);
}
}
}
@ -324,7 +394,8 @@ function getUpdatedTagsText(count: number) {
}
function onResolutionLoaded(res: any) {
if (pop.selectedPost && !pop.selectedPost.resolution) {
// This no longer checks for `!pop.selectedPost.resolution` because the resolution can change if the contentUrl is updated.
if (pop.selectedPost) {
pop.selectedPost.resolution = res;
}
}
@ -341,7 +412,7 @@ useDark();
<template>
<div class="popup-messages">
<div class="messages">
<!--
<!--
Error messages
-->
@ -375,7 +446,10 @@ useDark();
</div>
</div>
<!-- Only show error when it's not undefined and not empty. -->
<!--
Only show error when it's not undefined and not empty.
TODO: Maybe remove this in favor of genericError?
-->
<div v-if="instanceSpecificData?.uploadState?.error?.length" class="bg-danger">
<span>Couldn't upload post. {{ instanceSpecificData.uploadState.error }}</span>
</div>
@ -506,13 +580,13 @@ useDark();
</div>
</div>
<div class="section-row">
<div v-if="cfg.popup.showSource" class="section-row">
<span class="section-label">Source</span>
<textarea v-model="pop.selectedPost.source"></textarea>
</div>
</PopupSection>
<PopupSection header="Tags">
<PopupSection header="Tags" toggleable v-model="cfg.popup.expandTags">
<div class="section-row">
<TagInput :szuru="szuru" @add-tag="addTag" />
</div>
@ -527,7 +601,22 @@ useDark();
</div>
</PopupSection>
<div class="popup-section">
<PopupSection v-if="cfg.popup.showPools" header="Pools" toggleable v-model="cfg.popup.expandPools">
<div class="section-row">
<PoolInput :szuru="szuru" @add-pool="addPool" />
</div>
<div class="section-row">
<CompactPools
:pools="pop.selectedPost.pools"
:show-remove-pool="true"
show-post-count
@remove-pool="removePool"
/>
</div>
</PopupSection>
<div v-if="cfg.popup.showInstancePicker" class="popup-section">
<div class="section-row">
<select v-model="cfg.selectedSiteId">
<option v-for="site in cfg.sites" :key="site.id" :value="site.id">

View File

@ -1,6 +1,7 @@
import { defineStore } from "pinia";
import deepMerge from "deepmerge";
import {
type TagDetails,
getDefaultTagCategories,
type ScrapedPostDetails,
type SetPostUploadInfoData,
@ -14,6 +15,8 @@ export const cfg = useStorageLocal(
{
version: 0,
addPageUrlToSource: true,
alwaysUploadAsContent: false,
sendUploadNotifications: false,
autoSearchSimilar: false,
loadTagCounts: true,
fetchPostInfo: true,
@ -30,7 +33,15 @@ export const cfg = useStorageLocal(
appendSource: true,
mergeSafety: true,
},
popup: {
expandTags: true,
expandPools: false,
showSource: true,
showPools: true,
showInstancePicker: true,
},
tagCategories: [] as Array<TagCategoryColor>,
tagIgnores: [] as Array<TagDetails>,
},
{
mergeDefaults(storageValue, defaults) {
@ -56,7 +67,7 @@ export const cfg = useStorageLocal(
return cfg;
},
}
},
);
export const usePopupStore = defineStore("popup", {

View File

@ -7,7 +7,15 @@
font-family: "Open Sans";
font-style: normal;
font-weight: 400;
src: local(""), url("/assets/open-sans-v34-latin-regular.woff2") format("woff2");
src:
local(""),
url("/assets/open-sans-v34-latin-regular.woff2") format("woff2");
}
*,
*:before,
*:after {
box-sizing: border-box;
}
body {
@ -91,7 +99,7 @@ select {
border: 2px solid var(--border-color);
padding: 0.2em 0.3em;
box-sizing: border-box;
// box-sizing: border-box;
width: 100%;
@ -117,7 +125,7 @@ textarea {
&:focus {
outline: var(--primary-color) solid 2px;
box-sizing: content-box;
// box-sizing: content-box;
}
}
@ -141,7 +149,8 @@ ul.compact {
display: inline;
}
.compact-tags {
.compact-tags,
.compact-pools {
list-style-type: none;
padding: 0;
display: inline;
@ -259,3 +268,9 @@ a.remove-tag {
.color-primary {
color: var(--primary-color);
}
.separated-content > * + * {
padding-top: 1rem;
margin-top: 1.5rem;
border-top: 3px solid #333;
}

View File

@ -17,7 +17,7 @@
.p-datatable {
margin: -0.4em;
.p-datatable-table {
th,
td {

View File

@ -1,7 +1,7 @@
import { describe, expect, it } from 'vitest';
import { describe, expect, it } from "vitest";
describe('Demo', () => {
it('should work', () => {
describe("Demo", () => {
it("should work", () => {
expect(1 + 1).toBe(2);
});
});

View File

@ -1,6 +1,7 @@
import byteSize from "byte-size";
import { MicroUser, Post } from "./api/models";
import { ScrapedPostDetails, TagDetails } from "./models";
import SzurubooruApi from "./api";
export function getUrl(root: string, ...parts: string[]): string {
let url = root.replace(/\/+$/, "");
@ -102,3 +103,28 @@ export function getPostInfoSummary(post: ScrapedPostDetails) {
}
return parts.join(" / ");
}
export async function ensurePostHasContentToken(selectedInstance: SzurubooruApi, post: ScrapedPostDetails, cfg: any) {
if (!selectedInstance || !cfg.value.selectedSiteId) return;
const instanceSpecificData = post.instanceSpecificData[cfg.value.selectedSiteId];
if (!instanceSpecificData) {
console.error("instanceSpecificData is undefined. This should never happen!");
return;
}
if (instanceSpecificData.contentToken) {
console.log("[ensurePostHasContentToken] contentToken is already set.");
return;
}
try {
const tmpRes = await selectedInstance.uploadTempFile(post.contentUrl, post.uploadMode);
// Save contentToken in PostViewModel so that we can reuse it when creating/uploading the post.
instanceSpecificData.contentToken = tmpRes.token;
} catch (ex) {
instanceSpecificData.genericError = "Couldn't upload content. " + getErrorMessage(ex);
throw ex;
}
}

View File

@ -3,10 +3,7 @@
"baseUrl": ".",
"module": "ESNext",
"target": "es2016",
"lib": [
"DOM",
"ESNext"
],
"lib": ["DOM", "ESNext"],
"strict": true,
"esModuleInterop": true,
"incremental": false,
@ -16,17 +13,10 @@
"resolveJsonModule": true,
"noUnusedLocals": true,
"forceConsistentCasingInFileNames": true,
"types": [
"vite/client"
],
"types": ["vite/client"],
"paths": {
"~/*": [
"src/*"
]
"~/*": ["src/*"]
}
},
"exclude": [
"dist",
"node_modules"
]
"exclude": ["dist", "node_modules"]
}

12
updates.json Normal file
View File

@ -0,0 +1,12 @@
{
"addons": {
"suzuruchrome@goblincave": {
"updates": [
{
"version": "0.1.4",
"update_link": "https://gitea.goblincave.synology.me/Nes/SzuruChrome/releases/download/mod-v0.1.X/SzuruChrome_v0.1.4.xpi"
}
]
}
}
}