Compare commits
1 Commits
master
...
feature-sh
| Author | SHA1 | Date |
|---|---|---|
|
|
fe1c7a1bf5 |
|
|
@ -1,5 +0,0 @@
|
||||||
dist
|
|
||||||
node_modules
|
|
||||||
public
|
|
||||||
auto-imports.d.ts
|
|
||||||
components.d.ts
|
|
||||||
|
|
@ -1,29 +0,0 @@
|
||||||
module.exports = {
|
|
||||||
env: {
|
|
||||||
browser: true,
|
|
||||||
es2021: true,
|
|
||||||
},
|
|
||||||
extends: ["eslint:recommended", "plugin:vue/vue3-essential", "plugin:@typescript-eslint/recommended"],
|
|
||||||
parser: "vue-eslint-parser",
|
|
||||||
parserOptions: {
|
|
||||||
ecmaVersion: "latest",
|
|
||||||
parser: "@typescript-eslint/parser",
|
|
||||||
sourceType: "module",
|
|
||||||
},
|
|
||||||
plugins: ["vue", "@typescript-eslint"],
|
|
||||||
rules: {
|
|
||||||
semi: ["error", "always"],
|
|
||||||
"no-undef": "off",
|
|
||||||
"@typescript-eslint/no-unused-vars": [
|
|
||||||
"warn",
|
|
||||||
{
|
|
||||||
argsIgnorePattern: "^_",
|
|
||||||
varsIgnorePattern: "^_",
|
|
||||||
caughtErrorsIgnorePattern: "^_",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
"@typescript-eslint/no-explicit-any": "off",
|
|
||||||
"eol-last": ["error", "always"],
|
|
||||||
"comma-dangle": ["error", "always-multiline"],
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
@ -1,44 +0,0 @@
|
||||||
name: "Setup project"
|
|
||||||
runs:
|
|
||||||
using: "composite"
|
|
||||||
steps:
|
|
||||||
- uses: actions/setup-dotnet@v3
|
|
||||||
with:
|
|
||||||
dotnet-version: "7.0.x"
|
|
||||||
|
|
||||||
- name: Install GitVersion
|
|
||||||
uses: gittools/actions/gitversion/setup@v0
|
|
||||||
with:
|
|
||||||
versionSpec: "5.x"
|
|
||||||
|
|
||||||
- name: Determine Version
|
|
||||||
uses: gittools/actions/gitversion/execute@v0
|
|
||||||
id: version
|
|
||||||
with:
|
|
||||||
useConfigFile: true
|
|
||||||
|
|
||||||
- name: Use the version
|
|
||||||
shell: bash
|
|
||||||
run: |
|
|
||||||
echo VITE_SZ_VERSION="${{ steps.version.outputs.AssemblySemFileVer }}" >> $GITHUB_ENV
|
|
||||||
echo VITE_SZ_VERSION_INFO="${{ steps.version.outputs.InformationalVersion }}" >> $GITHUB_ENV
|
|
||||||
|
|
||||||
- uses: pnpm/action-setup@v2
|
|
||||||
name: Install pnpm
|
|
||||||
id: pnpm-install
|
|
||||||
with:
|
|
||||||
version: 9
|
|
||||||
run_install: false
|
|
||||||
|
|
||||||
- uses: actions/setup-node@v3
|
|
||||||
with:
|
|
||||||
node-version: "20"
|
|
||||||
cache: "pnpm"
|
|
||||||
|
|
||||||
- name: Install dependencies
|
|
||||||
shell: bash
|
|
||||||
run: pnpm install
|
|
||||||
|
|
||||||
- name: Linter
|
|
||||||
shell: bash
|
|
||||||
run: pnpm lint
|
|
||||||
|
|
@ -1,95 +0,0 @@
|
||||||
name: Build release
|
|
||||||
on:
|
|
||||||
push:
|
|
||||||
paths:
|
|
||||||
- src/**
|
|
||||||
- extension/**
|
|
||||||
- package.json
|
|
||||||
- pnpm-lock.yaml
|
|
||||||
workflow_dispatch:
|
|
||||||
inputs:
|
|
||||||
publish_amo_listed:
|
|
||||||
description: "Publish to AMO"
|
|
||||||
required: false
|
|
||||||
type: boolean
|
|
||||||
jobs:
|
|
||||||
build_chrome:
|
|
||||||
name: "Build Chrome Extension"
|
|
||||||
runs-on: ubuntu-24.04
|
|
||||||
steps:
|
|
||||||
- name: Checkout
|
|
||||||
uses: actions/checkout@v2
|
|
||||||
with:
|
|
||||||
fetch-depth: 0
|
|
||||||
|
|
||||||
- uses: ./.github/actions/setup-for-build
|
|
||||||
|
|
||||||
- name: Build
|
|
||||||
run: pnpm build
|
|
||||||
|
|
||||||
- name: Pack ZIP
|
|
||||||
run: pnpm pack:zip
|
|
||||||
|
|
||||||
- uses: actions/upload-artifact@v4
|
|
||||||
with:
|
|
||||||
name: Chrome Extension (Unsigned)
|
|
||||||
path: extension.zip
|
|
||||||
|
|
||||||
build_unlisted_firefox:
|
|
||||||
name: "Build Unlisted Firefox Extension"
|
|
||||||
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')
|
|
||||||
steps:
|
|
||||||
- name: Checkout
|
|
||||||
uses: actions/checkout@v2
|
|
||||||
with:
|
|
||||||
fetch-depth: 0
|
|
||||||
|
|
||||||
- uses: ./.github/actions/setup-for-build
|
|
||||||
|
|
||||||
- name: Build
|
|
||||||
run: pnpm build
|
|
||||||
env:
|
|
||||||
SZ_GECKO_ID: 13372607-2257-4360-8f51-5ce66fa73350
|
|
||||||
|
|
||||||
- name: Sign XPI
|
|
||||||
run: pnpm sign:firefox --channel unlisted
|
|
||||||
env:
|
|
||||||
WEB_EXT_API_KEY: ${{ secrets.WEB_EXT_API_KEY }}
|
|
||||||
WEB_EXT_API_SECRET: ${{ secrets.WEB_EXT_API_SECRET }}
|
|
||||||
|
|
||||||
- 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-24.04
|
|
||||||
# Doesn't really need the Chrome build, but we can reuse its cache.
|
|
||||||
needs: build_chrome
|
|
||||||
# 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
|
|
||||||
uses: actions/checkout@v2
|
|
||||||
with:
|
|
||||||
fetch-depth: 0
|
|
||||||
|
|
||||||
- uses: ./.github/actions/setup-for-build
|
|
||||||
|
|
||||||
- name: Build
|
|
||||||
run: pnpm build
|
|
||||||
env:
|
|
||||||
SZ_GECKO_ID: 18365332-8485-4c04-9498-2843a41ab620
|
|
||||||
|
|
||||||
- name: Sign XPI and upload to AMO
|
|
||||||
run: pnpm sign:firefox --channel listed
|
|
||||||
env:
|
|
||||||
WEB_EXT_API_KEY: ${{ secrets.WEB_EXT_API_KEY }}
|
|
||||||
WEB_EXT_API_SECRET: ${{ secrets.WEB_EXT_API_SECRET }}
|
|
||||||
|
|
@ -1,17 +1,5 @@
|
||||||
.DS_Store
|
.vscode/
|
||||||
.idea/
|
/node_modules
|
||||||
.vite-ssg-dist
|
/*.log
|
||||||
.vite-ssg-temp
|
/dist
|
||||||
*.crx
|
/dist-zip
|
||||||
*.local
|
|
||||||
*.log
|
|
||||||
*.pem
|
|
||||||
*.xpi
|
|
||||||
*.zip
|
|
||||||
dist
|
|
||||||
dist-ssr
|
|
||||||
extension/manifest.json
|
|
||||||
node_modules
|
|
||||||
src/auto-imports.d.ts
|
|
||||||
src/components.d.ts
|
|
||||||
*artifacts/
|
|
||||||
|
|
|
||||||
3
.npmrc
|
|
@ -1,3 +0,0 @@
|
||||||
shamefully-hoist=true
|
|
||||||
auto-install-peers=true
|
|
||||||
strict-peer-dependencies=false
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
{
|
{
|
||||||
"printWidth": 120,
|
"printWidth": 120,
|
||||||
"tabWidth": 2
|
"tabWidth": 4
|
||||||
}
|
}
|
||||||
|
|
@ -1,3 +0,0 @@
|
||||||
{
|
|
||||||
"recommendations": ["vue.volar", "dbaeumer.vscode-eslint", "esbenp.prettier-vscode"]
|
|
||||||
}
|
|
||||||
|
|
@ -1,6 +0,0 @@
|
||||||
{
|
|
||||||
"typescript.tsdk": "node_modules/typescript/lib",
|
|
||||||
"editor.codeActionsOnSave": {
|
|
||||||
"source.fixAll.eslint": "explicit"
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,7 +0,0 @@
|
||||||
mode: ContinuousDelivery
|
|
||||||
increment: None
|
|
||||||
assembly-file-versioning-format: "{Major}.{Minor}.{Patch}.{CommitsSinceVersionSource}"
|
|
||||||
branches: {}
|
|
||||||
ignore:
|
|
||||||
sha: []
|
|
||||||
merge-message-formats: {}
|
|
||||||
109
README.md
|
|
@ -2,38 +2,115 @@
|
||||||
|
|
||||||
Chrome/Firefox extension which can import media from various sites into a [szurubooru](https://github.com/rr-/szurubooru) instance.
|
Chrome/Firefox extension which can import media from various sites into a [szurubooru](https://github.com/rr-/szurubooru) instance.
|
||||||
|
|
||||||

|
## Supported sites
|
||||||
|
|
||||||
|
- Gelbooru/Safebooru/Rule34.xxx
|
||||||
|
- Danbooru (and Safebooru variant)
|
||||||
|
- Rule34 paheal (Shimmie2)
|
||||||
|
- yande.re/konachan (Moebooru)
|
||||||
|
- Reddit
|
||||||
|
- Twitter
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
### Firefox
|
_Work in progress_
|
||||||
|
|
||||||
I am experimenting with publishing the extension to Firefox Add-ons. You can find it [here](https://addons.mozilla.org/en-US/firefox/addon/szuruchrome/). This version will automatically update.
|
For development see [build/debug](#Builddebug).
|
||||||
|
There also is a .xpi for Firefox on the [release tab](https://github.com/neobooru/SzuruChrome/releases).
|
||||||
|
There _isn't_ a .crx for Chrome because you can't install it anyway, so you'll have to use the unpacked extension.
|
||||||
|
|
||||||
You can also download the .xpi from the [release tab](https://github.com/neobooru/SzuruChrome/releases) and manually install it. This version will not automatically update.
|
This extension should also work on Firefox Android (I can't test this because Firefox Android doesn't support client certificates).
|
||||||
|
Same goes for Kiwi Browser and pretty much all other Chromium based browsers with extension support.
|
||||||
|
The Yandex browser does support client certificates and extensions (SzuruChrome works fine), but I'm not sure if I'd trust them with my data.
|
||||||
|
The layout is obviously not designed for mobile, so at times it looks less than ideal.
|
||||||
|
|
||||||
Both versions can be installed side by side, as they do not affect each other. This also means that you need to configure both of them independently.
|
## How does it work?
|
||||||
|
|
||||||
### Chrome
|
This add-on has two parts. The first part is the popup, which is what pops up when you click on the logo. The second part is a content script which is injected on _every_ website ([why?](#Why-is-the-content-script-injected-on-every-page)).
|
||||||
|
|
||||||
I can't create a valid .crx file, which means that you'll have to use [Chrome's "Load unpacked extension"](https://developer.chrome.com/docs/extensions/mv3/getstarted/#unpacked) feature.
|
### The popup
|
||||||
You can download a .zip containing the needed files from the [release tab](https://github.com/neobooru/SzuruChrome/releases). Check [this Stack Overflow answer](https://stackoverflow.com/a/38011386) if you want to get rid of the developer extension popup.
|
|
||||||
|
When opened (when you click on the add-on logo in your browser) it sends a message to the content script injected on the current page (active tab). This message asks the content script to scrape the DOM and return a SzuruPost element (with a source, imageUrl, safety, and a list of tags). Once it receives this element it renders it in the popup where you can make changes before importing it.
|
||||||
|
|
||||||
|
### The content script
|
||||||
|
|
||||||
|
This script is injected on every page ([why?](#Why-is-the-content-script-injected-on-every-page)) and does absolutely nothing until it receives the 'grab_post' message from the popup. Once it does received this message it checks if there are any loaders available to scrape this page. If a matching loader is found it'll pass the DOM of this page to the loader. The loader then uses the DOM to fill (as much as possible) a SzuruPost object. The SzuruPost is then passed back to the popup where you can change the post before importing it.
|
||||||
|
|
||||||
## Build/debug
|
## Build/debug
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
# Install node modules
|
# Install node modules
|
||||||
pnpm install
|
npm install
|
||||||
|
|
||||||
# Live dev build (watch)
|
# Use any of the following commands to build once
|
||||||
pnpm dev
|
npm run build
|
||||||
|
npm run build:dev
|
||||||
|
|
||||||
# Production build
|
# Or use one of these to build + rebuild on changes
|
||||||
pnpm build
|
npm run watch
|
||||||
|
npm run watch:dev
|
||||||
```
|
```
|
||||||
|
|
||||||
After this you can load them in your browser.
|
After this you can load them in your browser.
|
||||||
Chrome: `chrome://extensions/` → "Load unpacked" → Select `./dist` folder
|
Chrome: `chrome://extensions/` -> "Load unpacked" -> Select `./dist` folder
|
||||||
Firefox: `about:debugging` → "Load Temporary Add-on..." → Select any file in the `./dist` folder (it will load all files)
|
Firefox: `about:debugging` -> "Load Temporary Add-on..." -> Select any file in the `./dist` folder (it will load all files)
|
||||||
|
|
||||||
See [the developer notes](https://github.com/neobooru/SzuruChrome/wiki/Developer-notes) for more information.
|
## Planned features
|
||||||
|
|
||||||
|
- Finishing the initial version of this add-on (see visual design doc for all features)
|
||||||
|
- 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
|
||||||
|
- Configurable list of tags to always ignore (e.g. tagme, highres)
|
||||||
|
- [low priority] Import artist commentary
|
||||||
|
- [low priority] Import notes
|
||||||
|
- [lowest priority] Move from 'options_page' to 'options_ui'
|
||||||
|
- [lowest priority] Multiple szurubooru instances (where you can choose to which one you want to upload the post)
|
||||||
|
- [lowest priority] Application size could be reduced, but I am not too worried about that because it's all local and zipped it is just 1.2MB
|
||||||
|
|
||||||
|
## FAQ
|
||||||
|
|
||||||
|
### I can't connect to a szurubooru instance because it complains about CORS
|
||||||
|
|
||||||
|
See workaround below.
|
||||||
|
|
||||||
|
```apache
|
||||||
|
# CORS hack
|
||||||
|
Header always set Access-Control-Allow-Origin "*"
|
||||||
|
Header always set Access-Control-Allow-Methods "GET, POST, PUT, DELETE, OPTIONS"
|
||||||
|
Header always set Access-Control-Allow-Headers "Authorization, Content-Type"
|
||||||
|
|
||||||
|
# CORS custom response for OPTIONS request (because szurubooru doesn't handle this, yet)
|
||||||
|
RewriteEngine on
|
||||||
|
RewriteCond %{REQUEST_METHOD} OPTIONS
|
||||||
|
RewriteRule ^(.*)$ $1 [R=204,L]
|
||||||
|
```
|
||||||
|
|
||||||
|
### I can't connect to a szurubooru instance which requires a client certificate (Firefox only)
|
||||||
|
|
||||||
|
Because the CORS spec is a bitch the preflight request is sent without the client certificate. A workaround would be to add the following to your apache2 config. Chrome has a 'bug' where it does send the client certificate in the preflight request (the sane thing to do if you ask me).
|
||||||
|
|
||||||
|
```apache
|
||||||
|
<Location "/api">
|
||||||
|
# Can also be set to 'none'
|
||||||
|
SSLVerifyClient optional
|
||||||
|
</Location>
|
||||||
|
```
|
||||||
|
|
||||||
|
### Why is the content script injected on every page?
|
||||||
|
|
||||||
|
Because I didn't know that you can inject a script from the popup (after opening it, so only when needed). I might change this later.
|
||||||
|
|
||||||
|
### Why send the DOM to the ILoader instead of just an url?
|
||||||
|
|
||||||
|
If we send an URL to the ILoader it has to download the page again itself, which can go wrong in a dozen ways:
|
||||||
|
|
||||||
|
1. The website might be offline now
|
||||||
|
2. If the website is a SPA it might not see the same version/content as you did
|
||||||
|
3. If you made changes to the DOM but didn't submit them to the server (e.g. edit post page) the ILoader would see a different page (kinda the same as point 2)
|
||||||
|
4. If any form of authentication (username+password/client certificate/etc) is involved it gets tricky
|
||||||
|
5. We already have the DOM, why download it again?
|
||||||
|
|
||||||
|
### Why is the CSS so bad?
|
||||||
|
|
||||||
|
Because CSS scares me.
|
||||||
|
|
|
||||||
|
Before Width: | Height: | Size: 350 KiB |
|
Before Width: | Height: | Size: 306 KiB |
|
Before Width: | Height: | Size: 293 KiB |
|
Before Width: | Height: | Size: 36 KiB |
|
Before Width: | Height: | Size: 36 KiB |
107
package.json
|
|
@ -1,81 +1,42 @@
|
||||||
{
|
{
|
||||||
"name": "szuruchrome",
|
"name": "szuru-chrome",
|
||||||
"displayName": "SzuruChrome",
|
"version": "0.2.2",
|
||||||
"description": "Import media from various sites into szurubooru.",
|
"description": "Import media from various sites into szurubooru",
|
||||||
"version": "0.0.0",
|
"author": "neobooru",
|
||||||
"private": true,
|
"license": "MIT",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
"dev": "npm run clear && cross-env NODE_ENV=development run-p dev:*",
|
"build": "cross-env NODE_ENV=production webpack --hide-modules",
|
||||||
"dev:prepare": "esno scripts/prepare.ts",
|
"build:dev": "cross-env NODE_ENV=development webpack --hide-modules",
|
||||||
"dev:web": "vite",
|
"build-zip": "node scripts/build-zip.js",
|
||||||
"dev:js": "npm run build:js -- --mode development",
|
"watch": "npm run build -- --watch",
|
||||||
"build": "cross-env NODE_ENV=production run-s clear build:web build:prepare build:js",
|
"watch:dev": "cross-env HMR=true npm run build:dev -- --watch"
|
||||||
"build:prepare": "esno scripts/prepare.ts",
|
|
||||||
"build:web": "vite build",
|
|
||||||
"build:js": "vite build --config vite.config.content.ts",
|
|
||||||
"pack": "cross-env NODE_ENV=production run-p pack:*",
|
|
||||||
"pack:zip": "rimraf extension.zip && jszip-cli add extension/* -o ./extension.zip",
|
|
||||||
"pack:crx": "crx pack extension -o ./extension.crx",
|
|
||||||
"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 --source-dir ./extension",
|
|
||||||
"clear": "rimraf extension/dist extension/manifest.json extension.*",
|
|
||||||
"lint": "eslint .",
|
|
||||||
"extlint": "web-ext lint -s extension",
|
|
||||||
"test": "vitest test",
|
|
||||||
"pretty": "prettier -w ."
|
|
||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"axios": "^1.19.0",
|
"axios": "^0.19.0",
|
||||||
"byte-size": "^8.2.1",
|
"neo-scraper": "^0.2.1",
|
||||||
"deepmerge": "^4.3.1",
|
"ts-optchain": "^0.1.8",
|
||||||
"lodash": "^4.18.1",
|
"vue": "^2.6.10",
|
||||||
"markdown-it": "^14.3.0",
|
"webextension-polyfill": "^0.3.1"
|
||||||
"neo-scraper": "^0.11.1",
|
|
||||||
"normalize.css": "^8.0.1",
|
|
||||||
"pinia": "^2.3.1",
|
|
||||||
"primeflex": "^3.3.1",
|
|
||||||
"primevue": "3.45.0",
|
|
||||||
"vue-router": "^4.6.4"
|
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"@ffflorian/jszip-cli": "^3.16.5",
|
"archiver": "^3.1.1",
|
||||||
"@fortawesome/fontawesome-svg-core": "^6.7.2",
|
"copy-webpack-plugin": "^4.5.3",
|
||||||
"@fortawesome/free-solid-svg-icons": "^6.7.2",
|
"core-js": "^3.0.1",
|
||||||
"@fortawesome/vue-fontawesome": "^3.3.3",
|
"cross-env": "^5.2.0",
|
||||||
"@types/byte-size": "^8.1.2",
|
"css-loader": "^2.1.1",
|
||||||
"@types/fs-extra": "^11.0.4",
|
"ejs": "^2.6.1",
|
||||||
"@types/lodash": "^4.17.25",
|
"file-loader": "^1.1.11",
|
||||||
"@types/markdown-it": "^13.0.9",
|
"mini-css-extract-plugin": "^0.4.4",
|
||||||
"@types/node": "^20.19.43",
|
"node-sass": "^4.9.3",
|
||||||
"@types/webextension-polyfill": "^0.10.7",
|
"sass-loader": "^7.1.0",
|
||||||
"@typescript-eslint/eslint-plugin": "^6.21.0",
|
"ts-loader": "^6.0.4",
|
||||||
"@vitejs/plugin-vue": "^5.2.4",
|
"typescript": "^3.5.3",
|
||||||
"@vue/compiler-sfc": "^3.5.41",
|
"vue-loader": "^15.4.2",
|
||||||
"@vue/test-utils": "^2.4.11",
|
"vue-template-compiler": "^2.6.10",
|
||||||
"@vueuse/core": "^10.11.1",
|
"web-ext-types": "^2.1.0",
|
||||||
"chokidar": "^3.6.0",
|
"webextension-polyfill-ts": "^0.9.1",
|
||||||
"cross-env": "^7.0.3",
|
"webpack": "^4.39.1",
|
||||||
"crx": "^5.0.1",
|
"webpack-cli": "^3.1.2",
|
||||||
"eslint": "^8.57.1",
|
"webpack-extension-reloader": "^1.1.0"
|
||||||
"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.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"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
7271
pnpm-lock.yaml
|
|
@ -0,0 +1,53 @@
|
||||||
|
#!/usr/bin/env node
|
||||||
|
|
||||||
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
|
const archiver = require('archiver');
|
||||||
|
|
||||||
|
const DEST_DIR = path.join(__dirname, '../dist');
|
||||||
|
const DEST_ZIP_DIR = path.join(__dirname, '../dist-zip');
|
||||||
|
|
||||||
|
const extractExtensionData = () => {
|
||||||
|
const extPackageJson = require('../package.json');
|
||||||
|
|
||||||
|
return {
|
||||||
|
name: extPackageJson.name,
|
||||||
|
version: extPackageJson.version
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
const makeDestZipDirIfNotExists = () => {
|
||||||
|
if(!fs.existsSync(DEST_ZIP_DIR)) {
|
||||||
|
fs.mkdirSync(DEST_ZIP_DIR);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const buildZip = (src, dist, zipFilename) => {
|
||||||
|
console.info(`Building ${zipFilename}...`);
|
||||||
|
|
||||||
|
const archive = archiver('zip', { zlib: { level: 9 }});
|
||||||
|
const stream = fs.createWriteStream(path.join(dist, zipFilename));
|
||||||
|
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
archive
|
||||||
|
.directory(src, false)
|
||||||
|
.on('error', err => reject(err))
|
||||||
|
.pipe(stream);
|
||||||
|
|
||||||
|
stream.on('close', () => resolve());
|
||||||
|
archive.finalize();
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
const main = () => {
|
||||||
|
const {name, version} = extractExtensionData();
|
||||||
|
const zipFilename = `${name}-v${version}.zip`;
|
||||||
|
|
||||||
|
makeDestZipDirIfNotExists();
|
||||||
|
|
||||||
|
buildZip(DEST_DIR, DEST_ZIP_DIR, zipFilename)
|
||||||
|
.then(() => console.info('OK'))
|
||||||
|
.catch(console.err);
|
||||||
|
};
|
||||||
|
|
||||||
|
main();
|
||||||
|
|
@ -1,10 +0,0 @@
|
||||||
import fs from "fs-extra";
|
|
||||||
import { getManifest } from "../src/manifest";
|
|
||||||
import { log, r } from "./utils";
|
|
||||||
|
|
||||||
export async function writeManifest() {
|
|
||||||
await fs.writeJSON(r("extension/manifest.json"), await getManifest(), { spaces: 2 });
|
|
||||||
log("PRE", "write manifest.json");
|
|
||||||
}
|
|
||||||
|
|
||||||
writeManifest();
|
|
||||||
|
|
@ -1,38 +0,0 @@
|
||||||
// generate stub index.html files for dev entry
|
|
||||||
import { execSync } from "child_process";
|
|
||||||
import fs from "fs-extra";
|
|
||||||
import chokidar from "chokidar";
|
|
||||||
import { isDev, log, port, r } from "./utils";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Stub index.html to use Vite in development
|
|
||||||
*/
|
|
||||||
async function stubIndexHtml() {
|
|
||||||
const views = ["options", "popup", "background"];
|
|
||||||
|
|
||||||
for (const view of views) {
|
|
||||||
await fs.ensureDir(r(`extension/dist/${view}`));
|
|
||||||
let data = await fs.readFile(r(`src/${view}/index.html`), "utf-8");
|
|
||||||
data = data
|
|
||||||
.replace('"./main.ts"', `"http://localhost:${port}/${view}/main.ts"`)
|
|
||||||
.replace('<div id="app"></div>', '<div id="app">Vite server did not start</div>');
|
|
||||||
await fs.writeFile(r(`extension/dist/${view}/index.html`), data, "utf-8");
|
|
||||||
log("PRE", `stub ${view}`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function writeManifest() {
|
|
||||||
execSync("npx esno ./scripts/manifest.ts", { stdio: "inherit" });
|
|
||||||
}
|
|
||||||
|
|
||||||
writeManifest();
|
|
||||||
|
|
||||||
if (isDev) {
|
|
||||||
stubIndexHtml();
|
|
||||||
chokidar.watch(r("src/**/*.html")).on("change", () => {
|
|
||||||
stubIndexHtml();
|
|
||||||
});
|
|
||||||
chokidar.watch([r("src/manifest.ts"), r("package.json")]).on("change", () => {
|
|
||||||
writeManifest();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
@ -1,10 +0,0 @@
|
||||||
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 function log(name: string, message: string) {
|
|
||||||
console.log(black(bgCyan(` ${name} `)), message);
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,37 @@
|
||||||
|
import { browser } from "webextension-polyfill-ts";
|
||||||
|
|
||||||
|
export class SzuruSiteConfig {
|
||||||
|
domain: string = "";
|
||||||
|
username: string = "";
|
||||||
|
authToken: string = "";
|
||||||
|
}
|
||||||
|
|
||||||
|
export class Config {
|
||||||
|
importExistingTags: boolean = true;
|
||||||
|
importAllTags: boolean = false;
|
||||||
|
importCommentary: boolean = false;
|
||||||
|
autoSearchSimilar: boolean = false;
|
||||||
|
sites: Array<SzuruSiteConfig> = [];
|
||||||
|
|
||||||
|
public async save() {
|
||||||
|
await browser.storage.local.set({
|
||||||
|
"config": JSON.stringify(this)
|
||||||
|
});
|
||||||
|
|
||||||
|
console.dir("Save " + JSON.stringify(this));
|
||||||
|
console.log("Saved config");
|
||||||
|
}
|
||||||
|
|
||||||
|
static async load(): Promise<Config> {
|
||||||
|
let storage = await browser.storage.local.get("config");
|
||||||
|
let config: Config = new Config();
|
||||||
|
|
||||||
|
if ("config" in storage) {
|
||||||
|
config = Object.assign(config, JSON.parse(storage["config"]));
|
||||||
|
}
|
||||||
|
|
||||||
|
console.dir("Load " + JSON.stringify(config));
|
||||||
|
return config;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
@ -0,0 +1,102 @@
|
||||||
|
/**
|
||||||
|
* 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<Tag>
|
||||||
|
export type TagCategoriesResult = UnpagedSearchResult<TagCategory>
|
||||||
|
|
||||||
|
export interface SzuruError {
|
||||||
|
name: string;
|
||||||
|
title: string;
|
||||||
|
description: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
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?: 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[];
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,130 @@
|
||||||
|
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<any> {
|
||||||
|
return (await this.apiGet("info")).data;
|
||||||
|
}
|
||||||
|
|
||||||
|
async updateTag(tag: Tag): Promise<any> {
|
||||||
|
return (await this.apiPut("tag/" + tag.names[0], tag)).data;
|
||||||
|
}
|
||||||
|
|
||||||
|
async getTags(query: string): Promise<TagsResult> {
|
||||||
|
let url = "tags";
|
||||||
|
if (query) {
|
||||||
|
url += query;
|
||||||
|
}
|
||||||
|
return (await this.apiGet(url)).data;
|
||||||
|
}
|
||||||
|
|
||||||
|
async getTagCategories(): Promise<TagCategoriesResult> {
|
||||||
|
return (await this.apiGet("tag-categories")).data;
|
||||||
|
}
|
||||||
|
|
||||||
|
async createPost(post: ScrapedPost): Promise<Post> {
|
||||||
|
var obj = {
|
||||||
|
tags: post.tags.map(x => x.name),
|
||||||
|
safety: post.rating,
|
||||||
|
source: post.source,
|
||||||
|
contentUrl: post.imageUrl
|
||||||
|
};
|
||||||
|
|
||||||
|
console.log("Create new post object");
|
||||||
|
console.dir(obj);
|
||||||
|
|
||||||
|
return (await this.apiPost("posts", obj)).data;
|
||||||
|
}
|
||||||
|
|
||||||
|
async reverseSearch(imageUrl: string): Promise<ImageSearchResult> {
|
||||||
|
var obj = {
|
||||||
|
contentUrl: imageUrl
|
||||||
|
};
|
||||||
|
|
||||||
|
return (await this.apiPost("posts/reverse-search", obj)).data;
|
||||||
|
}
|
||||||
|
|
||||||
|
private async apiGet(url: string, additionalHeaders: any = {}): Promise<any> {
|
||||||
|
const fullUrl = this.apiUrl + url;
|
||||||
|
const config: AxiosRequestConfig = {
|
||||||
|
method: "GET",
|
||||||
|
url: fullUrl
|
||||||
|
}
|
||||||
|
|
||||||
|
config.headers = { ...this.baseHeaders, ...additionalHeaders };
|
||||||
|
return await this.execute(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async apiPost(url: string, data: any, additionalHeaders: any = {}): Promise<any> {
|
||||||
|
const fullUrl = this.apiUrl + url;
|
||||||
|
const config: AxiosRequestConfig = {
|
||||||
|
method: "POST",
|
||||||
|
url: fullUrl,
|
||||||
|
data: data
|
||||||
|
}
|
||||||
|
|
||||||
|
config.headers = { ...this.baseHeaders, ...additionalHeaders };
|
||||||
|
return await this.execute(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async apiPut(url: string, data: any, additionalHeaders: any = {}): Promise<any> {
|
||||||
|
const fullUrl = this.apiUrl + url;
|
||||||
|
const config: AxiosRequestConfig = {
|
||||||
|
method: "PUT",
|
||||||
|
url: fullUrl,
|
||||||
|
data: data
|
||||||
|
}
|
||||||
|
|
||||||
|
config.headers = { ...this.baseHeaders, ...additionalHeaders };
|
||||||
|
return await this.execute(config);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async execute(config: AxiosRequestConfig): Promise<any> {
|
||||||
|
if (this.username && this.authToken) {
|
||||||
|
const token = "Token " + btoa(`${this.username}:${this.authToken}`);
|
||||||
|
config.headers["Authorization"] = token;
|
||||||
|
// console.log(token);
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
return await axios(config);
|
||||||
|
} catch (ex) {
|
||||||
|
const error = ex.response.data as SzuruError;
|
||||||
|
throw error ? error : ex;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static async createFromConfig(siteConfig: SzuruSiteConfig): Promise<SzuruWrapper | null> {
|
||||||
|
return new SzuruWrapper(siteConfig.domain, siteConfig.username, siteConfig.authToken);
|
||||||
|
}
|
||||||
|
}
|
||||||
266
src/api/index.ts
|
|
@ -1,266 +0,0 @@
|
||||||
import axios, { AxiosRequestConfig, CancelToken } from "axios";
|
|
||||||
import { isEqual } from "lodash";
|
|
||||||
import {
|
|
||||||
TagsResult,
|
|
||||||
TagCategoriesResult,
|
|
||||||
Post,
|
|
||||||
Tag,
|
|
||||||
SzuruError,
|
|
||||||
ImageSearchResult,
|
|
||||||
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.
|
|
||||||
*
|
|
||||||
* @class SzuruWrapper
|
|
||||||
*/
|
|
||||||
export default class SzurubooruApi {
|
|
||||||
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<any> {
|
|
||||||
return (await this.apiGet("info")).data;
|
|
||||||
}
|
|
||||||
|
|
||||||
async getPost(id: number): Promise<Post> {
|
|
||||||
return (await this.apiGet("post/" + id)).data;
|
|
||||||
}
|
|
||||||
|
|
||||||
async updateTag(tag: Tag): Promise<any> {
|
|
||||||
return (await this.apiPut("tag/" + encodeURIComponent(tag.names[0]), tag)).data;
|
|
||||||
}
|
|
||||||
|
|
||||||
async updatePost(id: number, updateRequest: UpdatePostRequest): Promise<Post> {
|
|
||||||
return (await this.apiPut("post/" + id, updateRequest)).data;
|
|
||||||
}
|
|
||||||
|
|
||||||
async getTags(
|
|
||||||
query: string,
|
|
||||||
offset = 0,
|
|
||||||
limit = 100,
|
|
||||||
fields?: TagFields[],
|
|
||||||
cancelToken?: CancelToken,
|
|
||||||
): Promise<TagsResult> {
|
|
||||||
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<TagCategoriesResult> {
|
|
||||||
return (await this.apiGet("tag-categories")).data;
|
|
||||||
}
|
|
||||||
|
|
||||||
async createPost(post: ScrapedPostDetails, contentToken?: string): Promise<Post> {
|
|
||||||
const obj = <any>{
|
|
||||||
tags: post.tags.map((x) => x.names[0]),
|
|
||||||
safety: post.rating,
|
|
||||||
source: post.source,
|
|
||||||
notes: post.notes,
|
|
||||||
};
|
|
||||||
|
|
||||||
if (contentToken) {
|
|
||||||
obj.contentToken = contentToken;
|
|
||||||
} else {
|
|
||||||
obj.contentUrl = post.contentUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
console.log("Create new post object");
|
|
||||||
console.dir(obj);
|
|
||||||
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
async reverseSearchToken(contentToken: string): Promise<ImageSearchResult> {
|
|
||||||
const obj = { contentToken };
|
|
||||||
return (await this.apiPost("posts/reverse-search", obj)).data;
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
static createUpdatePostRequest(orig: Post, newPost: Post /*, anonymous = false*/) {
|
|
||||||
const detail = <UpdatePostRequest>{ version: newPost.version };
|
|
||||||
|
|
||||||
// Send only changed fields to avoid user privilege violation
|
|
||||||
// if (anonymous === true) {
|
|
||||||
// detail.anonymous = true;
|
|
||||||
// }
|
|
||||||
|
|
||||||
if (orig.safety != newPost.safety) {
|
|
||||||
detail.safety = newPost.safety;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (orig.source != newPost.source) {
|
|
||||||
detail.source = newPost.source;
|
|
||||||
}
|
|
||||||
|
|
||||||
const oldTags = orig.tags.map((x) => x.names[0]);
|
|
||||||
const newTags = newPost.tags.map((x) => x.names[0]);
|
|
||||||
if (isEqual(oldTags, newTags) == false) {
|
|
||||||
detail.tags = newTags;
|
|
||||||
}
|
|
||||||
|
|
||||||
return detail;
|
|
||||||
}
|
|
||||||
|
|
||||||
private async apiGet(url: string, additionalHeaders: any = {}, cancelToken?: CancelToken): Promise<any> {
|
|
||||||
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<any> {
|
|
||||||
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<any> {
|
|
||||||
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<any> {
|
|
||||||
if (this.username && this.authToken) {
|
|
||||||
const token = "Token " + btoa(`${this.username}:${this.authToken}`);
|
|
||||||
if (!config.headers) config.headers = {};
|
|
||||||
config.headers["Authorization"] = token;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
return await axios(config);
|
|
||||||
} catch (ex: any) {
|
|
||||||
const error = ex.response?.data as SzuruError | undefined;
|
|
||||||
throw error?.name ? error : ex;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,150 +0,0 @@
|
||||||
/**
|
|
||||||
* 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 = "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 {
|
|
||||||
name: string;
|
|
||||||
title: string;
|
|
||||||
description: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface PostAlreadyUploadedError extends SzuruError {
|
|
||||||
otherPostId: number;
|
|
||||||
otherPostUrl: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
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 extends MicroTag {
|
|
||||||
version: number;
|
|
||||||
description?: string; // Markdown
|
|
||||||
creationTime: Date;
|
|
||||||
lastEditTime?: Date;
|
|
||||||
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;
|
|
||||||
color: string;
|
|
||||||
usages: number;
|
|
||||||
default: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface MicroUser {
|
|
||||||
name: string;
|
|
||||||
avatarUrl: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface MicroPost {
|
|
||||||
id: number;
|
|
||||||
thumbnailUrl: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface Post extends MicroPost {
|
|
||||||
version: number;
|
|
||||||
creationTime: Date;
|
|
||||||
lastEditTime?: Date;
|
|
||||||
safety: Safety;
|
|
||||||
source: string | null;
|
|
||||||
type: string;
|
|
||||||
mimeType: string;
|
|
||||||
checksum: string;
|
|
||||||
fileSize: number;
|
|
||||||
canvasWidth: number;
|
|
||||||
canvasHeight: number;
|
|
||||||
contentUrl: 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 SzuruNote {
|
|
||||||
polygon: number[][];
|
|
||||||
text: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface SimilarPost {
|
|
||||||
distance: number;
|
|
||||||
post: Post;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ImageSearchResult {
|
|
||||||
exactPost: Post | undefined;
|
|
||||||
similarPosts: SimilarPost[];
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface TemporaryFileUploadResult {
|
|
||||||
token: string;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface UpdatePostRequest {
|
|
||||||
version: number;
|
|
||||||
tags: string[] | undefined;
|
|
||||||
safety: Safety | undefined;
|
|
||||||
source: string | undefined | null;
|
|
||||||
// relations: string[];
|
|
||||||
// notes: string[],
|
|
||||||
// flags: string[];
|
|
||||||
contentUrl: string | undefined;
|
|
||||||
contentToken: string | undefined;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface UpdatePoolRequest {
|
|
||||||
version: number;
|
|
||||||
// names?: string[];
|
|
||||||
posts: number[];
|
|
||||||
}
|
|
||||||
|
|
@ -1,18 +0,0 @@
|
||||||
import { isFirefox, isForbiddenUrl } from "~/env";
|
|
||||||
|
|
||||||
// Firefox fetch files from cache instead of reloading changes from disk,
|
|
||||||
// hmr will not work as Chromium based browser
|
|
||||||
browser.webNavigation.onCommitted.addListener(({ tabId, frameId, url }) => {
|
|
||||||
// Filter out non main window events.
|
|
||||||
if (frameId !== 0) return;
|
|
||||||
|
|
||||||
if (isForbiddenUrl(url)) return;
|
|
||||||
|
|
||||||
// inject the latest scripts
|
|
||||||
browser.tabs
|
|
||||||
.executeScript(tabId, {
|
|
||||||
file: `${isFirefox ? "" : "."}/dist/contentScripts/index.global.js`,
|
|
||||||
runAt: "document_end",
|
|
||||||
})
|
|
||||||
.catch((error) => console.error(error));
|
|
||||||
});
|
|
||||||
|
|
@ -1,10 +0,0 @@
|
||||||
<!doctype html>
|
|
||||||
<html lang="en">
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8" />
|
|
||||||
<title>Background</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<script type="module" src="./main.ts"></script>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
|
|
@ -1,208 +0,0 @@
|
||||||
import { encodeTagName, getErrorMessage, getUrl } from "~/utils";
|
|
||||||
import {
|
|
||||||
BrowserCommand,
|
|
||||||
PostUploadCommandData,
|
|
||||||
PostUploadInfo,
|
|
||||||
SetPostUploadInfoData,
|
|
||||||
SetExactPostId,
|
|
||||||
PostUpdateCommandData,
|
|
||||||
FetchCommandData,
|
|
||||||
} from "~/models";
|
|
||||||
import { PostAlreadyUploadedError, UpdatePoolRequest } from "~/api/models";
|
|
||||||
import SzurubooruApi from "~/api";
|
|
||||||
|
|
||||||
// Only on dev mode
|
|
||||||
if (import.meta.hot) {
|
|
||||||
// @ts-expect-error for background HMR
|
|
||||||
import("/@vite/client");
|
|
||||||
// load latest content script
|
|
||||||
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",
|
|
||||||
};
|
|
||||||
|
|
||||||
const pushInfo = () =>
|
|
||||||
browser.runtime.sendMessage(
|
|
||||||
new BrowserCommand("set_post_upload_info", new SetPostUploadInfoData(data.selectedSite.id, data.post.id, info)),
|
|
||||||
);
|
|
||||||
|
|
||||||
try {
|
|
||||||
const szuru = SzurubooruApi.createFromConfig(data.selectedSite);
|
|
||||||
|
|
||||||
// Create and upload post
|
|
||||||
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";
|
|
||||||
info.instancePostId = createdPost.id;
|
|
||||||
pushInfo();
|
|
||||||
|
|
||||||
// Find tags with "default" category and update it
|
|
||||||
// TODO: Make all these categories configurable
|
|
||||||
const tagsWithCategory = data.post.tags.filter((x) => x.category);
|
|
||||||
const unsetCategoryTags = createdPost.tags
|
|
||||||
.filter((x) => x.category == "default")
|
|
||||||
.filter((x) => tagsWithCategory.some((y) => x.names.includes(y.names[0])));
|
|
||||||
|
|
||||||
if (unsetCategoryTags.length != 0) {
|
|
||||||
info.updateTagsState = {
|
|
||||||
total: unsetCategoryTags.length,
|
|
||||||
};
|
|
||||||
pushInfo();
|
|
||||||
|
|
||||||
// unsetCategoryTags is of type MicroTag[] and we need a Tag resource to update it, so let's get those
|
|
||||||
const query = unsetCategoryTags.map((x) => encodeTagName(x.names[0])).join();
|
|
||||||
const tags = (await szuru.getTags(query)).results;
|
|
||||||
const existingCategories = (await szuru.getTagCategories()).results;
|
|
||||||
let categoriesChangedCount = 0;
|
|
||||||
|
|
||||||
for (const i in tags) {
|
|
||||||
info.updateTagsState.current = parseInt(i);
|
|
||||||
pushInfo();
|
|
||||||
|
|
||||||
const wantedCategory = tagsWithCategory.find((x) => tags[i].names.includes(x.names[0]))?.category;
|
|
||||||
if (wantedCategory) {
|
|
||||||
if (existingCategories.some((x) => x.name == wantedCategory)) {
|
|
||||||
tags[i].category = wantedCategory;
|
|
||||||
await szuru.updateTag(tags[i]);
|
|
||||||
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.`,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (categoriesChangedCount > 0) {
|
|
||||||
info.updateTagsState.totalChanged = categoriesChangedCount;
|
|
||||||
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)),
|
|
||||||
);
|
|
||||||
// We don't set an error message, because we have a different message for posts that are already uploaded.
|
|
||||||
} else {
|
|
||||||
// Set generic error message.
|
|
||||||
info.error = getErrorMessage(ex);
|
|
||||||
}
|
|
||||||
info.state = "error";
|
|
||||||
pushInfo();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function updatePost(data: PostUpdateCommandData) {
|
|
||||||
const info: PostUploadInfo = {
|
|
||||||
state: "uploading",
|
|
||||||
instancePostId: data.postId,
|
|
||||||
};
|
|
||||||
|
|
||||||
const pushInfo = () =>
|
|
||||||
browser.runtime.sendMessage(
|
|
||||||
new BrowserCommand(
|
|
||||||
"set_post_update_info",
|
|
||||||
new SetPostUploadInfoData(data.selectedSite.id, `merge-${data.postId}`, info),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
|
|
||||||
try {
|
|
||||||
const szuru = SzurubooruApi.createFromConfig(data.selectedSite);
|
|
||||||
|
|
||||||
pushInfo();
|
|
||||||
|
|
||||||
await szuru.updatePost(data.postId, data.updateRequest);
|
|
||||||
|
|
||||||
info.state = "uploaded";
|
|
||||||
pushInfo();
|
|
||||||
} catch (ex: any) {
|
|
||||||
console.error(ex);
|
|
||||||
info.state = "error";
|
|
||||||
info.error = getErrorMessage(ex);
|
|
||||||
pushInfo();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Executes fetch in the background page. This allows us to do "forbidden" stuff, like ignoring CORS headers.
|
|
||||||
* @param data
|
|
||||||
* @returns
|
|
||||||
*/
|
|
||||||
async function executeFetch(data: FetchCommandData) {
|
|
||||||
return await fetch(data.url, data.options);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function messageHandler(cmd: BrowserCommand): Promise<any> {
|
|
||||||
console.log("Background received message:");
|
|
||||||
console.dir(cmd);
|
|
||||||
|
|
||||||
switch (cmd.name) {
|
|
||||||
case "upload_post":
|
|
||||||
return uploadPost(cmd.data);
|
|
||||||
case "update_post":
|
|
||||||
return updatePost(cmd.data);
|
|
||||||
case "fetch":
|
|
||||||
return executeFetch(cmd.data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
browser.runtime.onMessage.addListener(messageHandler);
|
|
||||||
|
|
@ -1,154 +0,0 @@
|
||||||
<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>
|
|
||||||
|
|
@ -1,33 +0,0 @@
|
||||||
<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>
|
|
||||||
|
|
@ -1,31 +0,0 @@
|
||||||
<script setup lang="ts">
|
|
||||||
import type { PropType } from "vue";
|
|
||||||
import type { TagDetails } from "~/models";
|
|
||||||
import { getTagClasses, breakTagName } from "~/utils";
|
|
||||||
|
|
||||||
defineProps({
|
|
||||||
tags: Array as PropType<TagDetails[]>,
|
|
||||||
showUsages: Boolean,
|
|
||||||
showRemoveTag: Boolean,
|
|
||||||
});
|
|
||||||
defineEmits(["removeTag"]);
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<template>
|
|
||||||
<ul class="compact-tags">
|
|
||||||
<li v-for="tag in tags" :key="tag.name">
|
|
||||||
<a v-if="showRemoveTag" class="remove-tag" @click="$emit('removeTag', tag)">x</a>
|
|
||||||
<span :class="getTagClasses(tag)" v-html="breakTagName(tag.name)"></span>
|
|
||||||
<span v-if="showUsages" class="tag-usages tag-usages-reserve-space">{{ tag.usages ? tag.usages : "" }}</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>
|
|
||||||
|
|
@ -1,22 +0,0 @@
|
||||||
<!-- Based on https://github.com/cloudacy/vue-markdown-render/blob/master/src/VueMarkdown.ts -->
|
|
||||||
|
|
||||||
<script setup lang="ts">
|
|
||||||
import MarkdownIt from "markdown-it";
|
|
||||||
|
|
||||||
const props = defineProps({
|
|
||||||
source: {
|
|
||||||
type: String,
|
|
||||||
required: true,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
const md = new MarkdownIt({
|
|
||||||
breaks: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
let content = computed(() => md?.render(props.source));
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<template>
|
|
||||||
<div v-html="content"></div>
|
|
||||||
</template>
|
|
||||||
|
|
@ -1,47 +0,0 @@
|
||||||
<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>
|
|
||||||
|
|
@ -1,39 +0,0 @@
|
||||||
<script setup lang="ts">
|
|
||||||
const props = defineProps({
|
|
||||||
modelValue: {
|
|
||||||
type: Boolean,
|
|
||||||
default: true,
|
|
||||||
},
|
|
||||||
toggleable: {
|
|
||||||
type: Boolean,
|
|
||||||
},
|
|
||||||
header: {
|
|
||||||
type: String,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
defineEmits(["update:modelValue"]);
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<template>
|
|
||||||
<div class="popup-section">
|
|
||||||
<div
|
|
||||||
class="section-header"
|
|
||||||
:class="{
|
|
||||||
toggleable: props.toggleable,
|
|
||||||
}"
|
|
||||||
@click="$emit('update:modelValue', !props.modelValue)"
|
|
||||||
>
|
|
||||||
<slot name="header">
|
|
||||||
<span>{{ props.header }}</span>
|
|
||||||
<template v-if="props.toggleable">
|
|
||||||
<font-awesome-icon v-show="props.modelValue" icon="fa-solid fa-chevron-up" class="cursor-pointer" />
|
|
||||||
<font-awesome-icon v-show="!props.modelValue" icon="fa-solid fa-chevron-down" class="cursor-pointer" />
|
|
||||||
</template>
|
|
||||||
</slot>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<slot v-if="props.modelValue"></slot>
|
|
||||||
</div>
|
|
||||||
</template>
|
|
||||||
|
|
||||||
<style lang="scss" scoped></style>
|
|
||||||
|
|
@ -1,51 +0,0 @@
|
||||||
<script setup lang="ts">
|
|
||||||
defineProps({
|
|
||||||
contentUrl: String,
|
|
||||||
notes: {
|
|
||||||
type: Array,
|
|
||||||
default: () => [],
|
|
||||||
},
|
|
||||||
contentType: String,
|
|
||||||
});
|
|
||||||
|
|
||||||
const emit = defineEmits(["onResolutionLoaded"]);
|
|
||||||
|
|
||||||
const imgEl = ref<HTMLImageElement | undefined>(undefined);
|
|
||||||
|
|
||||||
// Get image width and height from the <img> element.
|
|
||||||
// This works because we load the full-resolution image in the <img> element.
|
|
||||||
// This code might stop showing correct resolutions if/when we implement a progressive image loading.
|
|
||||||
function onloadImage() {
|
|
||||||
// I guess post/selectedPost could have a race condition here?
|
|
||||||
// Seems very unlikely though, as the load event gets cancelled as soon as we change the :src attribute.
|
|
||||||
// A race condition might even be impossible then.
|
|
||||||
if (imgEl.value) {
|
|
||||||
emit("onResolutionLoaded", [imgEl.value.naturalWidth, imgEl.value.naturalHeight]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<template>
|
|
||||||
<div class="post-container">
|
|
||||||
<img v-if="contentType == 'image'" ref="imgEl" :src="contentUrl" @load="onloadImage" />
|
|
||||||
<video v-if="contentType == 'video'" controls>
|
|
||||||
<source :src="contentUrl" />
|
|
||||||
</video>
|
|
||||||
<div class="post-overlay">
|
|
||||||
<PostNotes :notes="notes" />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</template>
|
|
||||||
|
|
||||||
<style scoped lang="scss">
|
|
||||||
.post-overlay {
|
|
||||||
position: absolute;
|
|
||||||
width: 100%;
|
|
||||||
height: 100%;
|
|
||||||
}
|
|
||||||
|
|
||||||
.post-container {
|
|
||||||
position: relative;
|
|
||||||
display: flex;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
|
|
@ -1,29 +0,0 @@
|
||||||
<script setup lang="ts">
|
|
||||||
import byteSize from "byte-size";
|
|
||||||
import { PropType } from "vue";
|
|
||||||
import { ScrapedPostDetails } from "~/models";
|
|
||||||
import { resolutionToString } from "~/utils";
|
|
||||||
|
|
||||||
defineProps({
|
|
||||||
modelValue: {
|
|
||||||
required: true,
|
|
||||||
type: Object as PropType<ScrapedPostDetails>,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<template>
|
|
||||||
<div class="basic-info-inline">
|
|
||||||
<span v-if="modelValue.contentSize">{{ byteSize(modelValue.contentSize) }}</span>
|
|
||||||
<span v-if="modelValue.contentSubType">{{ modelValue.contentSubType }}</span>
|
|
||||||
<span v-if="modelValue.resolution">({{ resolutionToString(modelValue.resolution) }})</span>
|
|
||||||
</div>
|
|
||||||
</template>
|
|
||||||
|
|
||||||
<style scoped lang="scss">
|
|
||||||
.basic-info-inline {
|
|
||||||
display: flex;
|
|
||||||
gap: 0 1ch;
|
|
||||||
flex-wrap: wrap;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
|
|
@ -1,131 +0,0 @@
|
||||||
<script setup lang="ts">
|
|
||||||
import { ScrapedNote } from "neo-scraper";
|
|
||||||
|
|
||||||
class Point {
|
|
||||||
constructor(
|
|
||||||
public x: any,
|
|
||||||
public y: any,
|
|
||||||
) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
// HACK: Copied from old client
|
|
||||||
function* range(start = 0, end: any = null, step = 1) {
|
|
||||||
if (end === null) {
|
|
||||||
end = start;
|
|
||||||
start = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
for (let i = start; i < end; i += step) {
|
|
||||||
yield i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// HACK: Copied from old client
|
|
||||||
function _getNoteCentroid(note: any) {
|
|
||||||
const vertexCount = note.polygon.length;
|
|
||||||
const centroid = new Point(0, 0);
|
|
||||||
let signedArea = 0.0;
|
|
||||||
for (let i of range(vertexCount)) {
|
|
||||||
const x0 = note.polygon.at(i)[0];
|
|
||||||
const y0 = note.polygon.at(i)[1];
|
|
||||||
const x1 = note.polygon.at((i + 1) % vertexCount)[0];
|
|
||||||
const y1 = note.polygon.at((i + 1) % vertexCount)[1];
|
|
||||||
const a = x0 * y1 - x1 * y0;
|
|
||||||
signedArea += a;
|
|
||||||
centroid.x += (x0 + x1) * a;
|
|
||||||
centroid.y += (y0 + y1) * a;
|
|
||||||
}
|
|
||||||
signedArea *= 0.5;
|
|
||||||
centroid.x /= 6 * signedArea;
|
|
||||||
centroid.y /= 6 * signedArea;
|
|
||||||
return centroid;
|
|
||||||
}
|
|
||||||
|
|
||||||
defineProps(["notes"]);
|
|
||||||
|
|
||||||
const mouseOverNote = ref(false);
|
|
||||||
const mouseOverNoteText = ref(false);
|
|
||||||
const hoverNote = ref<ScrapedNote | undefined>(undefined);
|
|
||||||
|
|
||||||
const showHoverNote = computed(() => (!hoverNote.value ? false : mouseOverNote.value || mouseOverNoteText.value));
|
|
||||||
const hoverNoteStyle = computed(() => {
|
|
||||||
if (!hoverNote.value) return "";
|
|
||||||
const { x, y } = _getNoteCentroid(hoverNote.value);
|
|
||||||
const top = y * 100;
|
|
||||||
const left = x * 100;
|
|
||||||
return `display: block; top: ${top}%; left: ${left}%`;
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<template>
|
|
||||||
<div>
|
|
||||||
<div
|
|
||||||
v-if="hoverNote && showHoverNote"
|
|
||||||
class="note-text"
|
|
||||||
:style="hoverNoteStyle"
|
|
||||||
@mouseenter="mouseOverNoteText = true"
|
|
||||||
@mouseleave="mouseOverNoteText = false"
|
|
||||||
>
|
|
||||||
<div class="wrapper">
|
|
||||||
<Markdown :source="hoverNote.text" />
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<svg class="notes-overlay" preserveAspectRatio="none" viewBox="0 0 1 1">
|
|
||||||
<g v-for="note in notes">
|
|
||||||
<polygon
|
|
||||||
vector-effect="non-scaling-stroke"
|
|
||||||
stroke-alignment="inside"
|
|
||||||
:points="note.polygon.join()"
|
|
||||||
@mouseenter="
|
|
||||||
hoverNote = note;
|
|
||||||
mouseOverNote = true;
|
|
||||||
"
|
|
||||||
@mouseleave="mouseOverNote = false"
|
|
||||||
/>
|
|
||||||
</g>
|
|
||||||
</svg>
|
|
||||||
</div>
|
|
||||||
</template>
|
|
||||||
|
|
||||||
<style lang="scss" scoped>
|
|
||||||
.notes-overlay {
|
|
||||||
width: 100%;
|
|
||||||
height: 100%;
|
|
||||||
position: absolute;
|
|
||||||
}
|
|
||||||
|
|
||||||
.notes-overlay g {
|
|
||||||
stroke-width: 1px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.notes-overlay g polygon {
|
|
||||||
fill: rgba(255, 255, 205, 0.3);
|
|
||||||
stroke: rgba(0, 0, 0, 0.2);
|
|
||||||
}
|
|
||||||
|
|
||||||
.note-text {
|
|
||||||
position: absolute;
|
|
||||||
max-width: 22.5em;
|
|
||||||
display: none;
|
|
||||||
text-align: left;
|
|
||||||
z-index: 20;
|
|
||||||
transform: translate(-50%, -50%);
|
|
||||||
|
|
||||||
> .wrapper {
|
|
||||||
background: lemonchiffon;
|
|
||||||
padding: 0.3em 0.6em;
|
|
||||||
border: 1px solid black;
|
|
||||||
color: black;
|
|
||||||
box-sizing: border-box;
|
|
||||||
}
|
|
||||||
|
|
||||||
&:deep(p:last-of-type) {
|
|
||||||
margin-bottom: 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
&:deep(p:first-of-type) {
|
|
||||||
margin-top: 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
|
|
@ -1,48 +0,0 @@
|
||||||
<script setup lang="ts">
|
|
||||||
import { PropType } from "vue";
|
|
||||||
import { TagDetails } from "~/models";
|
|
||||||
import { encodeTagName, getTagClasses } from "~/utils";
|
|
||||||
import SzurubooruApi from "~/api";
|
|
||||||
import { CancelToken } from "axios";
|
|
||||||
|
|
||||||
const autocompleteItems = ref<TagDetails[]>([]);
|
|
||||||
|
|
||||||
const props = defineProps({
|
|
||||||
szuru: Object as PropType<SzurubooruApi>,
|
|
||||||
});
|
|
||||||
const emit = defineEmits(["addTag"]);
|
|
||||||
|
|
||||||
function addTag(tag: TagDetails) {
|
|
||||||
emit("addTag", tag);
|
|
||||||
}
|
|
||||||
|
|
||||||
function addTagFromCurrentInput(input: string) {
|
|
||||||
addTag(new TagDetails([input]));
|
|
||||||
}
|
|
||||||
|
|
||||||
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"], ct);
|
|
||||||
|
|
||||||
if (res) {
|
|
||||||
// TODO: Maybe search on hamming distance or something?
|
|
||||||
autocompleteItems.value = res.results.map((x) => TagDetails.fromTag(x));
|
|
||||||
console.dir(autocompleteItems.value);
|
|
||||||
} else {
|
|
||||||
autocompleteItems.value = [];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<template>
|
|
||||||
<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>
|
|
||||||
|
|
@ -1,11 +0,0 @@
|
||||||
import { describe, expect, it } from "vitest";
|
|
||||||
import { mount } from "@vue/test-utils";
|
|
||||||
import Logo from "../Logo.vue";
|
|
||||||
|
|
||||||
describe("Logo Component", () => {
|
|
||||||
it("should render", () => {
|
|
||||||
const wrapper = mount(Logo);
|
|
||||||
|
|
||||||
expect(wrapper.html()).toBeTruthy();
|
|
||||||
});
|
|
||||||
});
|
|
||||||
|
|
@ -1,24 +0,0 @@
|
||||||
import { storage } from "webextension-polyfill";
|
|
||||||
import type { MaybeRef, RemovableRef, StorageLikeAsync, UseStorageAsyncOptions } from "@vueuse/core";
|
|
||||||
import { useStorageAsync } from "@vueuse/core";
|
|
||||||
|
|
||||||
const storageLocal: StorageLikeAsync = {
|
|
||||||
removeItem(key: string) {
|
|
||||||
return storage.local.remove(key);
|
|
||||||
},
|
|
||||||
|
|
||||||
setItem(key: string, value: string) {
|
|
||||||
return storage.local.set({ [key]: value });
|
|
||||||
},
|
|
||||||
|
|
||||||
async getItem(key: string) {
|
|
||||||
const item = (await storage.local.get(key))[key];
|
|
||||||
return item;
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
export const useStorageLocal = <T>(
|
|
||||||
key: string,
|
|
||||||
initialValue: MaybeRef<T>,
|
|
||||||
options?: UseStorageAsyncOptions<T>,
|
|
||||||
): RemovableRef<T> => useStorageAsync(key, initialValue, storageLocal, options);
|
|
||||||
|
|
@ -1,19 +0,0 @@
|
||||||
import { NeoScraper, ScrapeResults } from "neo-scraper";
|
|
||||||
import { BrowserCommand } from "~/models";
|
|
||||||
|
|
||||||
// Firefox `browser.tabs.executeScript()` requires scripts return a primitive value
|
|
||||||
(() => {
|
|
||||||
function grabPost(): ScrapeResults {
|
|
||||||
const scraper = new NeoScraper();
|
|
||||||
return scraper.scrapeDocument(document, true);
|
|
||||||
}
|
|
||||||
|
|
||||||
async function messageHandler(cmd: BrowserCommand): Promise<any> {
|
|
||||||
switch (cmd.name) {
|
|
||||||
case "grab_post":
|
|
||||||
return grabPost();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
browser.runtime.onMessage.addListener(messageHandler);
|
|
||||||
})();
|
|
||||||
|
|
@ -0,0 +1,16 @@
|
||||||
|
/*!
|
||||||
|
* Font Awesome Free 5.10.1 by @fontawesome - https://fontawesome.com
|
||||||
|
* License - https://fontawesome.com/license/free (Icons: CC BY 4.0, Fonts: SIL OFL 1.1, Code: MIT License)
|
||||||
|
*/
|
||||||
|
@font-face {
|
||||||
|
font-family: 'Font Awesome 5 Free';
|
||||||
|
font-style: normal;
|
||||||
|
font-weight: 900;
|
||||||
|
font-display: auto;
|
||||||
|
src: url("../fonts/fa-solid-900.eot");
|
||||||
|
src: url("../fonts/fa-solid-900.eot?#iefix") format("embedded-opentype"), url("../fonts/fa-solid-900.woff2") format("woff2"), url("../fonts/fa-solid-900.woff") format("woff"), url("../fonts/fa-solid-900.ttf") format("truetype"); }
|
||||||
|
|
||||||
|
.fa,
|
||||||
|
.fas {
|
||||||
|
font-family: 'Font Awesome 5 Free';
|
||||||
|
font-weight: 900; }
|
||||||
|
|
@ -0,0 +1,120 @@
|
||||||
|
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: 30px;
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
img {
|
||||||
|
max-width: 100%;
|
||||||
|
max-height: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
.full {
|
||||||
|
width: 100%;
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
background-color: #fafafa;
|
||||||
|
border: 1px solid #eee;
|
||||||
|
height: 30px;
|
||||||
|
width: 100%;
|
||||||
|
font-family: "Open Sans", sans-serif;
|
||||||
|
}
|
||||||
|
|
||||||
|
.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);
|
||||||
|
}
|
||||||
|
|
||||||
|
.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;
|
||||||
|
}
|
||||||
|
|
@ -1,23 +0,0 @@
|
||||||
import { TagCategoryColor } from "./models";
|
|
||||||
|
|
||||||
export function makeCssName(prefix: string, name: string) {
|
|
||||||
return prefix + "-" + name.replace(/[^a-z0-9]/g, "_");
|
|
||||||
}
|
|
||||||
|
|
||||||
export function refreshTagCategoryColorMap(tagCategories: TagCategoryColor[], styleElement?: HTMLStyleElement) {
|
|
||||||
if (styleElement) {
|
|
||||||
document.head.removeChild(styleElement);
|
|
||||||
}
|
|
||||||
styleElement = document.createElement("style");
|
|
||||||
document.head.appendChild(styleElement);
|
|
||||||
for (const category of tagCategories) {
|
|
||||||
const ruleName = makeCssName("tag", category.name);
|
|
||||||
|
|
||||||
if (styleElement.sheet) {
|
|
||||||
styleElement.sheet.insertRule(`.${ruleName} { color: ${category.color} }`, styleElement.sheet.cssRules.length);
|
|
||||||
} else {
|
|
||||||
console.error("styleElement.sheet is undefined. This should never happen!");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return styleElement;
|
|
||||||
}
|
|
||||||
18
src/env.ts
|
|
@ -1,18 +0,0 @@
|
||||||
const forbiddenProtocols = [
|
|
||||||
"chrome-extension://",
|
|
||||||
"chrome-search://",
|
|
||||||
"chrome://",
|
|
||||||
"devtools://",
|
|
||||||
"edge://",
|
|
||||||
"https://chrome.google.com/webstore",
|
|
||||||
];
|
|
||||||
|
|
||||||
export function isForbiddenUrl(url: string): boolean {
|
|
||||||
return forbiddenProtocols.some((protocol) => url.startsWith(protocol));
|
|
||||||
}
|
|
||||||
|
|
||||||
export const isFirefox = navigator.userAgent.includes("Firefox");
|
|
||||||
export const isChrome = navigator.userAgent.match(/Chrome|Chromium/i);
|
|
||||||
|
|
||||||
// Based on https://stackoverflow.com/a/25394023
|
|
||||||
export const isMobile = navigator.userAgent.match(/Android/i);
|
|
||||||
|
|
@ -1,6 +0,0 @@
|
||||||
declare const __DEV__: boolean;
|
|
||||||
|
|
||||||
declare module "*.vue" {
|
|
||||||
const component: any;
|
|
||||||
export default component;
|
|
||||||
}
|
|
||||||
|
After Width: | Height: | Size: 33 KiB |
|
After Width: | Height: | Size: 8.6 KiB |
|
|
@ -1,20 +0,0 @@
|
||||||
import type { App } from "vue";
|
|
||||||
import { library } from "@fortawesome/fontawesome-svg-core";
|
|
||||||
import { FontAwesomeIcon } from "@fortawesome/vue-fontawesome";
|
|
||||||
import { faCog, faChevronDown, faChevronUp, faChevronLeft } from "@fortawesome/free-solid-svg-icons";
|
|
||||||
import { createPinia } from "pinia";
|
|
||||||
|
|
||||||
export function setupPinia(app: App) {
|
|
||||||
const pinia = createPinia();
|
|
||||||
app.use(pinia);
|
|
||||||
}
|
|
||||||
|
|
||||||
export function setupApp(app: App) {
|
|
||||||
setupPinia(app);
|
|
||||||
|
|
||||||
library.add(faCog);
|
|
||||||
library.add(faChevronDown);
|
|
||||||
library.add(faChevronUp);
|
|
||||||
library.add(faChevronLeft);
|
|
||||||
app.component("font-awesome-icon", FontAwesomeIcon);
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,34 @@
|
||||||
|
{
|
||||||
|
"name": "SzuruChrome",
|
||||||
|
"description": "Import media from various sites into szurubooru",
|
||||||
|
"version": null,
|
||||||
|
"manifest_version": 2,
|
||||||
|
"icons": {
|
||||||
|
"48": "icons/icon_48.png",
|
||||||
|
"128": "icons/icon_128.png"
|
||||||
|
},
|
||||||
|
"browser_action": {
|
||||||
|
"default_title": "SzuruChrome",
|
||||||
|
"default_popup": "popup/popup.html"
|
||||||
|
},
|
||||||
|
"options_page": "options/options.html",
|
||||||
|
"permissions": [
|
||||||
|
"storage",
|
||||||
|
"activeTab"
|
||||||
|
],
|
||||||
|
"content_scripts": [
|
||||||
|
{
|
||||||
|
"matches": [
|
||||||
|
"<all_urls>"
|
||||||
|
],
|
||||||
|
"js": [
|
||||||
|
"scripts/content.js"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"browser_specific_settings": {
|
||||||
|
"gecko": {
|
||||||
|
"id": "szuruchrome@neobooru.com"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -1,67 +0,0 @@
|
||||||
import fs from "fs-extra";
|
|
||||||
import type { Manifest } from "webextension-polyfill";
|
|
||||||
import type PkgType from "../package.json";
|
|
||||||
import { isDev, port, r } from "../scripts/utils";
|
|
||||||
|
|
||||||
export async function getManifest() {
|
|
||||||
const pkg = (await fs.readJSON(r("package.json"))) as typeof PkgType;
|
|
||||||
|
|
||||||
// update this file to update this manifest.json
|
|
||||||
// can also be conditional based on your need
|
|
||||||
const manifest: Manifest.WebExtensionManifest = {
|
|
||||||
name: pkg.displayName,
|
|
||||||
description: pkg.description,
|
|
||||||
version: process.env.VITE_SZ_VERSION || pkg.version,
|
|
||||||
manifest_version: 2,
|
|
||||||
icons: {
|
|
||||||
16: "./assets/icon-128.png",
|
|
||||||
48: "./assets/icon-128.png",
|
|
||||||
128: "./assets/icon-128.png",
|
|
||||||
},
|
|
||||||
browser_action: {
|
|
||||||
default_title: pkg.displayName,
|
|
||||||
default_popup: "./dist/popup/index.html",
|
|
||||||
},
|
|
||||||
options_ui: {
|
|
||||||
page: "./dist/options/index.html",
|
|
||||||
open_in_tab: true,
|
|
||||||
chrome_style: false,
|
|
||||||
},
|
|
||||||
background: {
|
|
||||||
page: "./dist/background/index.html",
|
|
||||||
// persistent: false,
|
|
||||||
},
|
|
||||||
permissions: ["storage", "activeTab", "webRequest", "webRequestBlocking", "notifications", "<all_urls>"],
|
|
||||||
content_scripts: [
|
|
||||||
{
|
|
||||||
matches: ["http://*/*", "https://*/*"],
|
|
||||||
js: ["./dist/contentScripts/index.global.js"],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
};
|
|
||||||
|
|
||||||
if (process.env.SZ_GECKO_ID) {
|
|
||||||
manifest.browser_specific_settings = {
|
|
||||||
gecko: {
|
|
||||||
id: `{${process.env.SZ_GECKO_ID}}`,
|
|
||||||
strict_min_version: "113.0",
|
|
||||||
},
|
|
||||||
gecko_android: {
|
|
||||||
strict_min_version: "113.0",
|
|
||||||
},
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isDev) {
|
|
||||||
// for content script, as browsers will cache them for each reload,
|
|
||||||
// we use a background script to always inject the latest version
|
|
||||||
// see src/background/contentScriptHMR.ts
|
|
||||||
delete manifest.content_scripts;
|
|
||||||
manifest.permissions?.push("webNavigation");
|
|
||||||
|
|
||||||
// this is required on dev for Vite script to load
|
|
||||||
manifest.content_security_policy = `script-src 'self' http://localhost:${port}; object-src 'self'`;
|
|
||||||
}
|
|
||||||
|
|
||||||
return manifest;
|
|
||||||
}
|
|
||||||
|
|
@ -1,205 +0,0 @@
|
||||||
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,
|
|
||||||
) {}
|
|
||||||
|
|
||||||
get name() {
|
|
||||||
return this.names[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
static fromTag(tag: Tag) {
|
|
||||||
const x = new TagDetails(tag.names, tag.category, tag.usages);
|
|
||||||
|
|
||||||
if (tag.implications) {
|
|
||||||
x.implications = tag.implications.map((y) => TagDetails.fromMicroTag(y));
|
|
||||||
}
|
|
||||||
|
|
||||||
return x;
|
|
||||||
}
|
|
||||||
|
|
||||||
static fromMicroTag(tag: MicroTag) {
|
|
||||||
return new TagDetails(tag.names, tag.category, tag.usages);
|
|
||||||
}
|
|
||||||
|
|
||||||
static fromScapedTag(tag: ScrapedTag): TagDetails {
|
|
||||||
return new TagDetails([tag.name], tag.category);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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;
|
|
||||||
reverseSearchResult?: SimpleImageSearchResult;
|
|
||||||
uploadState?: PostUploadInfo;
|
|
||||||
}
|
|
||||||
|
|
||||||
type MappedInstanceSpecificData = {
|
|
||||||
[key: string]: InstanceSpecificData;
|
|
||||||
};
|
|
||||||
|
|
||||||
export class ScrapedPostDetails {
|
|
||||||
id = window.crypto.randomUUID();
|
|
||||||
name = "";
|
|
||||||
tags: TagDetails[] = [];
|
|
||||||
pools: PoolDetails[] = [];
|
|
||||||
notes: ScrapedNote[];
|
|
||||||
contentUrl: string;
|
|
||||||
extraContentUrl: string | undefined;
|
|
||||||
contentSize: number | undefined;
|
|
||||||
pageUrl: string;
|
|
||||||
contentType: ContentType;
|
|
||||||
contentSubType: string | undefined;
|
|
||||||
rating: BooruTypes.SafetyRating;
|
|
||||||
source;
|
|
||||||
uploadMode: UploadMode;
|
|
||||||
referrer?: string;
|
|
||||||
resolution?: [number, number];
|
|
||||||
instanceSpecificData: MappedInstanceSpecificData = {};
|
|
||||||
|
|
||||||
constructor(post: ScrapedPost) {
|
|
||||||
this.contentUrl = post.contentUrl;
|
|
||||||
this.extraContentUrl = post.extraContentUrl;
|
|
||||||
// this.contentSize = post.contentSize;
|
|
||||||
this.pageUrl = post.pageUrl;
|
|
||||||
this.contentType = post.contentType;
|
|
||||||
this.rating = post.rating;
|
|
||||||
this.source = post.sources.join("\n");
|
|
||||||
this.referrer = post.referrer;
|
|
||||||
this.tags = post.tags.map((x) => TagDetails.fromScapedTag(x));
|
|
||||||
this.notes = post.notes;
|
|
||||||
this.resolution = post.resolution;
|
|
||||||
this.uploadMode = post.uploadMode;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface SimpleImageSearchResult {
|
|
||||||
exactPostId?: number;
|
|
||||||
similarPosts: SimpleSimilarPost[];
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface SimpleSimilarPost {
|
|
||||||
distance: number;
|
|
||||||
postId: number;
|
|
||||||
}
|
|
||||||
|
|
||||||
export class SimilarPostInfo {
|
|
||||||
constructor(
|
|
||||||
public readonly id: number,
|
|
||||||
public readonly percentage: number,
|
|
||||||
) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
export type PostUploadState = "uploading" | "uploaded" | "error";
|
|
||||||
|
|
||||||
export class PostUploadInfo {
|
|
||||||
state: PostUploadState = "uploading";
|
|
||||||
error?: string;
|
|
||||||
instancePostId?: number;
|
|
||||||
updateTagsState?: {
|
|
||||||
total: number;
|
|
||||||
current?: number;
|
|
||||||
totalChanged?: number;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export type BrowserCommandName =
|
|
||||||
| "grab_post"
|
|
||||||
| "upload_post"
|
|
||||||
| "set_post_upload_info"
|
|
||||||
| "set_exact_post_id"
|
|
||||||
| "update_post"
|
|
||||||
| "set_post_update_info"
|
|
||||||
| "fetch";
|
|
||||||
|
|
||||||
export class BrowserCommand<T = any> {
|
|
||||||
name: BrowserCommandName;
|
|
||||||
data?: T;
|
|
||||||
|
|
||||||
constructor(name: BrowserCommandName, data?: T) {
|
|
||||||
this.name = name;
|
|
||||||
this.data = data;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class PostUploadCommandData {
|
|
||||||
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,
|
|
||||||
) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class SetExactPostId {
|
|
||||||
constructor(
|
|
||||||
public readonly instanceId: string,
|
|
||||||
public readonly postId: string,
|
|
||||||
public readonly exactPostId: number,
|
|
||||||
) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class PostUpdateCommandData {
|
|
||||||
constructor(
|
|
||||||
public readonly postId: number,
|
|
||||||
public readonly updateRequest: UpdatePostRequest,
|
|
||||||
public readonly selectedSite: SzuruSiteConfig,
|
|
||||||
) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class FetchCommandData {
|
|
||||||
constructor(
|
|
||||||
public readonly url: string,
|
|
||||||
public readonly options: RequestInit | undefined = undefined,
|
|
||||||
) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class SzuruSiteConfig {
|
|
||||||
id = window.crypto.randomUUID();
|
|
||||||
domain = "https://example.com";
|
|
||||||
username = "user";
|
|
||||||
authToken = "";
|
|
||||||
}
|
|
||||||
|
|
||||||
export class TagCategoryColor {
|
|
||||||
constructor(
|
|
||||||
public name: string,
|
|
||||||
public color: string,
|
|
||||||
) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
export const getDefaultTagCategories = () => [
|
|
||||||
new TagCategoryColor("copyright", "#a0a"),
|
|
||||||
new TagCategoryColor("character", "#0a0"),
|
|
||||||
new TagCategoryColor("artist", "#a00"),
|
|
||||||
new TagCategoryColor("meta", "#f80"),
|
|
||||||
];
|
|
||||||
|
|
@ -1,459 +1,134 @@
|
||||||
<script setup lang="ts">
|
|
||||||
import { useColorMode } from "@vueuse/core";
|
|
||||||
import SzuruWrapper from "~/api";
|
|
||||||
import { cfg } from "~/stores";
|
|
||||||
import { getErrorMessage } from "~/utils";
|
|
||||||
|
|
||||||
import TabView from "primevue/tabview";
|
|
||||||
import TabPanel from "primevue/tabpanel";
|
|
||||||
import DataTable from "primevue/datatable";
|
|
||||||
import Column from "primevue/column";
|
|
||||||
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);
|
|
||||||
|
|
||||||
const columns = ref([
|
|
||||||
{ field: "name", header: "Category name" },
|
|
||||||
{ field: "color", header: "CSS color" },
|
|
||||||
]);
|
|
||||||
|
|
||||||
const selectedSite = computed(() => {
|
|
||||||
if (cfg.value.selectedSiteId) {
|
|
||||||
return cfg.value.sites.find((x) => x.id == cfg.value.selectedSiteId);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const mode = useColorMode({ emitAuto: true });
|
|
||||||
|
|
||||||
async function testConnection() {
|
|
||||||
if (
|
|
||||||
!selectedSite.value ||
|
|
||||||
!selectedSite.value.domain ||
|
|
||||||
!selectedSite.value.username ||
|
|
||||||
!selectedSite.value.authToken
|
|
||||||
) {
|
|
||||||
setStatus("Domain, username and authentication token are all required.", "error");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const api = new SzuruWrapper(selectedSite.value.domain, selectedSite.value.username, selectedSite.value.authToken);
|
|
||||||
try {
|
|
||||||
const info = await api.getInfo();
|
|
||||||
const instanceName = info?.config.name;
|
|
||||||
|
|
||||||
if (instanceName == undefined) {
|
|
||||||
setStatus(`Connected to ${selectedSite.value.domain}, but it is not a szurubooru instance.`, "error");
|
|
||||||
} else {
|
|
||||||
setStatus(`Connected to ${info.config.name} at ${selectedSite.value.domain}`);
|
|
||||||
}
|
|
||||||
} catch (ex) {
|
|
||||||
setStatus(`Couldn't connect to ${selectedSite.value.domain}. ${getErrorMessage(ex)}`, "error");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function setStatus(text: string, type: StatusType = "success") {
|
|
||||||
statusText.value = text;
|
|
||||||
statusType.value = "status-" + type;
|
|
||||||
}
|
|
||||||
|
|
||||||
function addSite() {
|
|
||||||
const site = new SzuruSiteConfig();
|
|
||||||
cfg.value.sites.push(site);
|
|
||||||
cfg.value.selectedSiteId = site.id;
|
|
||||||
}
|
|
||||||
|
|
||||||
function removeSelectedSite() {
|
|
||||||
if (selectedSite.value) {
|
|
||||||
const idx = cfg.value.sites.indexOf(selectedSite.value);
|
|
||||||
cfg.value.sites.splice(idx, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// Select first site in list, if it exists.
|
|
||||||
if (cfg.value.sites.length > 0) {
|
|
||||||
cfg.value.selectedSiteId = cfg.value.sites[0].id;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function resetTagCategories() {
|
|
||||||
cfg.value.tagCategories.splice(0);
|
|
||||||
cfg.value.tagCategories.push(...getDefaultTagCategories());
|
|
||||||
}
|
|
||||||
|
|
||||||
function addTagCategory() {
|
|
||||||
cfg.value.tagCategories.push(new TagCategoryColor("category", "#abcdef"));
|
|
||||||
}
|
|
||||||
|
|
||||||
async function importTagCategoriesFromInstance() {
|
|
||||||
const szuruConfig = cfg.value.sites.find((x) => x.id == cfg.value.selectedSiteId)!;
|
|
||||||
const szuru = SzurubooruApi.createFromConfig(szuruConfig);
|
|
||||||
const cats = (await szuru.getTagCategories()).results;
|
|
||||||
|
|
||||||
for (const cat of cats) {
|
|
||||||
if (cat.name == "default") continue;
|
|
||||||
|
|
||||||
if (!cfg.value.tagCategories.find((x) => x.name == cat.name)) {
|
|
||||||
cfg.value.tagCategories.push(new TagCategoryColor(cat.name, cat.color));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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));
|
|
||||||
wnd.szc_set_config_version = (v = 0) => (cfg.value.version = v);
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div class="content-holder">
|
<div class="settings-container">
|
||||||
<div class="content-wrapper flex flex-auto flex-column gap-3">
|
<div class="settings-header">
|
||||||
<div><strong>SzuruChrome Settings</strong></div>
|
<strong>SzuruChrome settings</strong>
|
||||||
|
</div>
|
||||||
|
|
||||||
<TabView>
|
<div class="settings-content">
|
||||||
<TabPanel header="General">
|
|
||||||
<div class="grid">
|
|
||||||
<div class="col-12 md:col-6">
|
|
||||||
<label>
|
<label>
|
||||||
<input type="checkbox" v-model="cfg.addPageUrlToSource" />
|
Szurubooru URL
|
||||||
Add page URL to the source list
|
<br />
|
||||||
|
<input type="text" name="domain" v-model="domain" />
|
||||||
</label>
|
</label>
|
||||||
<p class="hint">
|
<br />
|
||||||
This will add the current page URL (e.g. the booru link) to the source list in addition to the actual
|
|
||||||
source (e.g. twitter/pixiv/artstation). Note: if the source is empty/not detected then the page URL will
|
|
||||||
always be used as fallback source.
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="col-12 md:col-6">
|
|
||||||
<label>
|
<label>
|
||||||
<input type="checkbox" v-model="cfg.addAllParsedTags" />
|
Username
|
||||||
Automatically import all tags on supported pages
|
<br />
|
||||||
|
<input type="text" name="username" v-model="username" />
|
||||||
</label>
|
</label>
|
||||||
<p class="hint">
|
|
||||||
This will automatically import all tags, including their categories, on supported pages such as Danbooru
|
|
||||||
and Zerochan.
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="col-12 md:col-6">
|
|
||||||
<label>
|
<label>
|
||||||
<input type="checkbox" v-model="cfg.alwaysUploadAsContent" />
|
Authentication token
|
||||||
Always upload as content
|
<br />
|
||||||
|
<input type="text" name="token" v-model="authToken" />
|
||||||
</label>
|
</label>
|
||||||
<p class="hint">This will always upload the image content directly, instead of as an URL</p>
|
<br />
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="col-12 md:col-6">
|
|
||||||
<label>
|
<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
|
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" />
|
|
||||||
Show how often a tag is used in the selected szurubooru instance
|
|
||||||
</label>
|
|
||||||
<p class="hint">
|
|
||||||
Shows how often a given tag is used in the selected szurubooru instance. No number will be displayed
|
|
||||||
when the tag does not yet exist.
|
|
||||||
</p>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<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>Selected Szurubooru Instance</label>
|
|
||||||
|
|
||||||
<div class="flex gap-1">
|
|
||||||
<select v-model="cfg.selectedSiteId">
|
|
||||||
<option v-for="site in cfg.sites" :key="site.id" :value="site.id">
|
|
||||||
{{ site.username }} @ {{ site.domain }}
|
|
||||||
</option>
|
|
||||||
</select>
|
|
||||||
|
|
||||||
<button class="primary" @click="addSite">Add</button>
|
|
||||||
<button class="bg-danger" @click="removeSelectedSite">Remove</button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<template v-if="selectedSite">
|
|
||||||
<div class="field col-12">
|
|
||||||
<label>URL</label>
|
|
||||||
<input
|
<input
|
||||||
v-if="selectedSite"
|
type="checkbox"
|
||||||
text="Szurubooru URL"
|
name="autoSearchSimilar"
|
||||||
type="text"
|
v-model="autoSearchSimilar"
|
||||||
name="domain"
|
|
||||||
v-model="selectedSite.domain"
|
|
||||||
/>
|
/>
|
||||||
</div>
|
</label>
|
||||||
|
<br />
|
||||||
<div class="field col-12 md:col-6">
|
<div class="settings-footer-buttons">
|
||||||
<label>Username</label>
|
|
||||||
<input text="Username" type="text" name="username" v-model="selectedSite.username" />
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="field col-12 md:col-6">
|
|
||||||
<label>Authentication token</label>
|
|
||||||
<input text="Authentication token" type="text" name="token" v-model="selectedSite.authToken" />
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="col-12 flex flex-wrap justify-content-between gap-3">
|
|
||||||
<span class="status" :class="statusType">{{ statusText }}</span>
|
|
||||||
<button @click="testConnection">Test connection</button>
|
<button @click="testConnection">Test connection</button>
|
||||||
|
<button @click="saveSettings" class="primary">Save settings</button>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
|
||||||
</div>
|
|
||||||
</TabPanel>
|
|
||||||
|
|
||||||
<TabPanel header="Tags">
|
|
||||||
<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>
|
|
||||||
|
|
||||||
<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>
|
|
||||||
</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>
|
|
||||||
</TabPanel>
|
|
||||||
|
|
||||||
<!-- <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>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<style lang="scss">
|
<script lang="ts">
|
||||||
.content-holder {
|
import Vue from "vue";
|
||||||
padding: 1.5em;
|
import { oc } from "ts-optchain";
|
||||||
display: flex;
|
import SzuruWrapper from "../SzuruWrapper";
|
||||||
justify-content: center;
|
import { Config, SzuruSiteConfig } from "../Config";
|
||||||
|
|
||||||
> .content-wrapper {
|
export default Vue.extend({
|
||||||
// box-sizing: border-box;
|
data() {
|
||||||
text-align: left;
|
return {
|
||||||
// display: inline-block;
|
domain: "",
|
||||||
// flex: auto;
|
username: "",
|
||||||
max-width: 1000px;
|
authToken: "",
|
||||||
// gap: 1rem;
|
autoSearchSimilar: false
|
||||||
padding: 1.8em;
|
};
|
||||||
background: #f5f5f5;
|
},
|
||||||
}
|
methods: {
|
||||||
}
|
async testConnection() {
|
||||||
|
if (!this.domain || !this.username || !this.authToken) {
|
||||||
html.dark .content-wrapper {
|
alert("Domain, username and authentication token are all required.");
|
||||||
background: var(--section-header-bg-color);
|
return;
|
||||||
}
|
|
||||||
|
|
||||||
.hint {
|
|
||||||
margin-top: 0.4em;
|
|
||||||
margin-bottom: 0;
|
|
||||||
color: var(--secondary-text);
|
|
||||||
font-size: 80%;
|
|
||||||
line-height: 120%;
|
|
||||||
}
|
|
||||||
|
|
||||||
.status {
|
|
||||||
align-self: center;
|
|
||||||
}
|
|
||||||
|
|
||||||
.status-error {
|
|
||||||
color: red;
|
|
||||||
}
|
|
||||||
|
|
||||||
.status-success {
|
|
||||||
color: green;
|
|
||||||
}
|
|
||||||
|
|
||||||
.status-quiet {
|
|
||||||
opacity: 0.6;
|
|
||||||
}
|
|
||||||
|
|
||||||
input[type="checkbox"] {
|
|
||||||
margin: 0 3px 0 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
@media only screen and (max-width: 600px) {
|
|
||||||
.content-holder {
|
|
||||||
padding: 0;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
.status {
|
const api = new SzuruWrapper(this.domain, this.username, this.authToken);
|
||||||
flex: 0 0 100%;
|
try {
|
||||||
|
const info = (await api.getInfo()) as any;
|
||||||
|
const instanceName = oc(info).config.domain;
|
||||||
|
|
||||||
|
if (instanceName == undefined) {
|
||||||
|
alert(`Connected to ${this.domain}, but it is not a szurubooru instance.`);
|
||||||
|
} else {
|
||||||
|
alert(`Connected to ${info.config.name} at ${this.domain}`);
|
||||||
}
|
}
|
||||||
|
} catch (ex) {
|
||||||
|
alert("Couldn't connect to " + this.domain + "\n\n" + ex);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
async saveSettings() {
|
||||||
|
let config = await Config.load();
|
||||||
|
console.dir(config);
|
||||||
|
|
||||||
|
// We currently only support one domain
|
||||||
|
let siteConfig = new SzuruSiteConfig();
|
||||||
|
siteConfig.domain = this.domain;
|
||||||
|
siteConfig.username = this.username;
|
||||||
|
siteConfig.authToken = this.authToken;
|
||||||
|
config.sites = [siteConfig];
|
||||||
|
config.autoSearchSimilar = this.autoSearchSimilar;
|
||||||
|
|
||||||
|
await config.save();
|
||||||
|
}
|
||||||
|
},
|
||||||
|
async mounted() {
|
||||||
|
let config = await Config.load();
|
||||||
|
|
||||||
|
console.log(`Loaded ${config.sites.length} sites`);
|
||||||
|
|
||||||
|
// We currently only support one domain
|
||||||
|
if (config.sites.length > 0) {
|
||||||
|
this.domain = config.sites[0].domain;
|
||||||
|
this.username = config.sites[0].username;
|
||||||
|
this.authToken = config.sites[0].authToken;
|
||||||
|
this.autoSearchSimilar = config.autoSearchSimilar;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
.settings-container {
|
||||||
|
margin: 0;
|
||||||
|
position: absolute;
|
||||||
|
top: 50%;
|
||||||
|
left: 50%;
|
||||||
|
-ms-transform: translate(-50%, -50%);
|
||||||
|
transform: translate(-50%, -50%);
|
||||||
|
|
||||||
|
padding: 20px;
|
||||||
|
background-color: #f5f5f5;
|
||||||
}
|
}
|
||||||
|
|
||||||
div.color-preview {
|
.settings-header {
|
||||||
white-space: nowrap;
|
padding: 0 0 20px 0;
|
||||||
position: relative;
|
}
|
||||||
display: flex;
|
|
||||||
|
|
||||||
input {
|
.settings-footer-buttons {
|
||||||
margin-right: 0.5em;
|
padding: 20px 0 0 0;
|
||||||
}
|
float: right;
|
||||||
|
}
|
||||||
|
|
||||||
.preview {
|
.settings-content > label {
|
||||||
display: inline-block;
|
display: inline-block;
|
||||||
text-align: center;
|
|
||||||
padding: 0 0.5em;
|
|
||||||
border: 2px solid #000;
|
|
||||||
|
|
||||||
&::after {
|
|
||||||
content: "A";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
.background-preview {
|
|
||||||
border-right: 0;
|
|
||||||
border-right-color: currentcolor;
|
|
||||||
color: transparent;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|
|
||||||
|
|
@ -1,13 +0,0 @@
|
||||||
<!doctype html>
|
|
||||||
<html lang="en">
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8" />
|
|
||||||
<base target="_blank" />
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
||||||
<title>SzuruChrome Settings</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<div id="app"></div>
|
|
||||||
<script type="module" src="./main.ts"></script>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
|
|
@ -1,12 +0,0 @@
|
||||||
import { createApp } from "vue";
|
|
||||||
import PrimeVue from "primevue/config";
|
|
||||||
import App from "./App.vue";
|
|
||||||
import { setupApp } from "~/logic/common-setup";
|
|
||||||
|
|
||||||
import "../styles/main.scss";
|
|
||||||
import "../styles/primevue.scss";
|
|
||||||
|
|
||||||
const app = createApp(App);
|
|
||||||
setupApp(app);
|
|
||||||
app.use(PrimeVue);
|
|
||||||
app.mount("#app");
|
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<title>SzuruChrome - Options</title>
|
||||||
|
<link rel="stylesheet" href="options.css" />
|
||||||
|
<% if (NODE_ENV === 'development') { %>
|
||||||
|
<!-- Load some resources only in development environment -->
|
||||||
|
<% } %>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<div id="app"></div>
|
||||||
|
<script src="options.js"></script>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
||||||
|
|
@ -0,0 +1,9 @@
|
||||||
|
import '../css/style.css';
|
||||||
|
|
||||||
|
import Vue from 'vue';
|
||||||
|
import App from './App';
|
||||||
|
|
||||||
|
new Vue({
|
||||||
|
el: '#app',
|
||||||
|
render: h => h(App)
|
||||||
|
})
|
||||||
|
|
@ -1,110 +1,435 @@
|
||||||
<script lang="ts" setup>
|
|
||||||
import { onMounted } from "vue";
|
|
||||||
import { isChrome } from "~/env";
|
|
||||||
import { BrowserCommand, SetPostUploadInfoData, SetExactPostId } from "~/models";
|
|
||||||
import { Runtime, WebRequest } from "webextension-polyfill";
|
|
||||||
import { cfg, useMergeStore, usePopupStore } from "~/stores";
|
|
||||||
import { refreshTagCategoryColorMap } from "~/dynamicStyle";
|
|
||||||
|
|
||||||
const pop = usePopupStore();
|
|
||||||
const merge = useMergeStore();
|
|
||||||
|
|
||||||
let el: any;
|
|
||||||
watch(
|
|
||||||
cfg,
|
|
||||||
(value) => {
|
|
||||||
el = refreshTagCategoryColorMap(value.tagCategories, el);
|
|
||||||
},
|
|
||||||
{ immediate: true },
|
|
||||||
);
|
|
||||||
|
|
||||||
onMounted(() => {
|
|
||||||
browser.runtime.onMessage.addListener((cmd: BrowserCommand, _sender: Runtime.MessageSender) => {
|
|
||||||
console.log("Popup received message:");
|
|
||||||
console.dir(cmd);
|
|
||||||
|
|
||||||
switch (cmd.name) {
|
|
||||||
case "set_post_upload_info":
|
|
||||||
{
|
|
||||||
const { postId, instanceId, info } = <SetPostUploadInfoData>cmd.data;
|
|
||||||
let post = pop.posts.find((x) => x.id == postId);
|
|
||||||
if (post) {
|
|
||||||
let instanceSpecificData = post.instanceSpecificData[instanceId];
|
|
||||||
if (instanceSpecificData) {
|
|
||||||
instanceSpecificData.uploadState = info;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case "set_exact_post_id":
|
|
||||||
{
|
|
||||||
const { postId, instanceId, exactPostId } = <SetExactPostId>cmd.data;
|
|
||||||
let post = pop.posts.find((x) => x.id == postId);
|
|
||||||
if (post && pop.selectedPostId) {
|
|
||||||
let instanceSpecificData = post.instanceSpecificData[instanceId];
|
|
||||||
if (instanceSpecificData) {
|
|
||||||
if (instanceSpecificData?.reverseSearchResult) {
|
|
||||||
instanceSpecificData.reverseSearchResult.exactPostId = exactPostId;
|
|
||||||
} else {
|
|
||||||
instanceSpecificData.reverseSearchResult = { exactPostId, similarPosts: [] };
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
case "set_post_update_info":
|
|
||||||
{
|
|
||||||
const data = <SetPostUploadInfoData>cmd.data;
|
|
||||||
let existing = merge.uploadInfo.find((x) => x.instanceId == data.instanceId && x.postId == data.postId);
|
|
||||||
if (existing) {
|
|
||||||
existing.info = data.info;
|
|
||||||
} else {
|
|
||||||
merge.uploadInfo.push(data);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
let extraInfoSpec: WebRequest.OnBeforeSendHeadersOptions[] = ["requestHeaders", "blocking"];
|
|
||||||
if (isChrome) {
|
|
||||||
extraInfoSpec.push("extraHeaders");
|
|
||||||
}
|
|
||||||
|
|
||||||
// Add "Referer" header to all requests if post.referrer is set.
|
|
||||||
// This is needed in some rare cases where websites disallow hotlinking to images.
|
|
||||||
// This will probably stop working in Chrome sometime in 2023 due to the Manifest V3 webRequestBlocking bullshit.
|
|
||||||
// Firefox users should be fine.
|
|
||||||
browser.webRequest.onBeforeSendHeaders.addListener(
|
|
||||||
(details: WebRequest.OnBeforeSendHeadersDetailsType): WebRequest.BlockingResponse => {
|
|
||||||
let requestHeaders = details.requestHeaders ?? [];
|
|
||||||
|
|
||||||
// Find which ScrapedPost this belongs to, if any.
|
|
||||||
const post = pop.getPostForContentUrl(details.url);
|
|
||||||
if (post != undefined && post.referrer) {
|
|
||||||
console.log(`Setting referrer to '${post.referrer}' for request to '${post.contentUrl}'.`);
|
|
||||||
// TODO: If the headers already have a 'Referer' header this will _not_ override that.
|
|
||||||
// As far as I am aware the 'img' tag doesn't set this header, so it shouldn't be a problem.
|
|
||||||
requestHeaders.push({ name: "Referer", value: post.referrer });
|
|
||||||
}
|
|
||||||
|
|
||||||
return {
|
|
||||||
requestHeaders,
|
|
||||||
};
|
|
||||||
},
|
|
||||||
{ urls: ["<all_urls>"] },
|
|
||||||
extraInfoSpec,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<div class="popup-container">
|
<div class="popup-container">
|
||||||
<router-view />
|
<div class="popup-row">
|
||||||
|
<div class="popup-column1" style="margin: 0px 10px;">
|
||||||
|
<ul class="messages">
|
||||||
|
<li
|
||||||
|
v-for="msg in messages"
|
||||||
|
:key="msg.name"
|
||||||
|
:class="getMessageClasses(msg)"
|
||||||
|
v-html="msg.content"
|
||||||
|
></li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="popup-row">
|
||||||
|
<div class="popup-column2">
|
||||||
|
<div class="popup-header">
|
||||||
|
<span>Basic info</span>
|
||||||
|
<i class="fas fa-cog" @click="openSettings"></i>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="popup-content">
|
||||||
|
<div class="popup-block">
|
||||||
|
<span class="block-title">Safety</span>
|
||||||
|
|
||||||
|
<label class="container">
|
||||||
|
<input type="radio" value="safe" v-model="post.rating" />
|
||||||
|
<span class="checkmark"></span>
|
||||||
|
Safe
|
||||||
|
</label>
|
||||||
|
|
||||||
|
<label class="container">
|
||||||
|
<input type="radio" value="sketchy" v-model="post.rating" />
|
||||||
|
<span class="checkmark"></span>
|
||||||
|
Sketchy
|
||||||
|
</label>
|
||||||
|
|
||||||
|
<label class="container">
|
||||||
|
<input type="radio" value="unsafe" v-model="post.rating" />
|
||||||
|
<span class="checkmark"></span>
|
||||||
|
Unsafe
|
||||||
|
</label>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="popup-block">
|
||||||
|
<span class="block-title">Source</span>
|
||||||
|
<input type="text" v-model="post.source" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="popup-header">
|
||||||
|
<span>Tags</span>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="popup-content">
|
||||||
|
<div class="popup-block" style="display:flex;">
|
||||||
|
<input type="text" v-model="addTagInput" v-on:keyup.enter="addTag" />
|
||||||
|
<button
|
||||||
|
class="primary"
|
||||||
|
style="margin-left: 5px; height: auto;"
|
||||||
|
@click="addTag"
|
||||||
|
>Add</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="popup-block">
|
||||||
|
<ul class="compact-tags">
|
||||||
|
<li v-for="tag in post.tags" :key="tag.name">
|
||||||
|
<a class="remove-tag" @click="removeTag(tag)">x</a>
|
||||||
|
<span :class="getTagClasses(tag)">{{ tag.name }}</span>
|
||||||
|
<span class="tag-usages">0</span>
|
||||||
|
</li>
|
||||||
|
</ul>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="popup-column2">
|
||||||
|
<img id="imgFull" :src="post.imageUrl" :title="postTooltip" />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="popup-row">
|
||||||
|
<div class="popup-column2">
|
||||||
|
<select disabled>
|
||||||
|
<option>{{ activeSite != null ? activeSite.domain : "(no site available)" }}</option>
|
||||||
|
</select>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<div class="popup-column2" style="display: flex;">
|
||||||
|
<button class="primary" @click="findSimilar">Find similar</button>
|
||||||
|
<button class="primary full" style="margin-left: 5px;" @click="upload">Import</button>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<style lang="scss">
|
<script lang="ts">
|
||||||
@use "~/styles/main.scss";
|
import Vue from "vue";
|
||||||
@use "~/styles/popup.scss";
|
import { browser } from "webextension-polyfill-ts";
|
||||||
|
import { ScrapedPost, ScrapedTag, ScrapeResults } from "neo-scraper";
|
||||||
|
import SzuruWrapper from "../SzuruWrapper";
|
||||||
|
import { Post, SzuruError } from "../SzuruTypes";
|
||||||
|
import { Config, SzuruSiteConfig } from "../Config";
|
||||||
|
import axios from "axios";
|
||||||
|
|
||||||
|
type MessageType = "error" | "info" | "success";
|
||||||
|
|
||||||
|
class Message {
|
||||||
|
content: string;
|
||||||
|
type: MessageType;
|
||||||
|
|
||||||
|
constructor(content: string, type: MessageType = "info") {
|
||||||
|
this.content = content;
|
||||||
|
this.type = type;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default Vue.extend({
|
||||||
|
data() {
|
||||||
|
return {
|
||||||
|
activeSite: null as SzuruSiteConfig | null,
|
||||||
|
szuru: null as SzuruWrapper | null,
|
||||||
|
post: new ScrapedPost(),
|
||||||
|
messages: [] as Message[],
|
||||||
|
addTagInput: ""
|
||||||
|
};
|
||||||
|
},
|
||||||
|
methods: {
|
||||||
|
// Try to scrape the post from the page
|
||||||
|
async grabPost() {
|
||||||
|
// Get active tab
|
||||||
|
const activeTab = (await browser.tabs.query({
|
||||||
|
active: true,
|
||||||
|
currentWindow: true
|
||||||
|
}))[0];
|
||||||
|
|
||||||
|
// Send 'grab_post' to the content script on the active tab
|
||||||
|
const x = await browser.tabs.sendMessage(activeTab.id!, "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);
|
||||||
|
const post = res.posts.length > 0 ? res.posts[0] : null;
|
||||||
|
|
||||||
|
if (post) {
|
||||||
|
this.post = post;
|
||||||
|
console.dir(post);
|
||||||
|
} else {
|
||||||
|
this.pushError("Couldn't grab post");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
// Try to upload the post to the selected szurubooru instance
|
||||||
|
async upload() {
|
||||||
|
this.clearMessages();
|
||||||
|
window.scrollTo(0, 0);
|
||||||
|
|
||||||
|
if (!this.post || !this.post.imageUrl) {
|
||||||
|
this.pushError("There is no post to upload!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!this.szuru) {
|
||||||
|
this.pushError("No szurubooru server configured!");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
// Create and upload post
|
||||||
|
let uploadMsg = new Message("Uploading...");
|
||||||
|
this.messages.push(uploadMsg);
|
||||||
|
const createdPost = await this.szuru.createPost(this.post);
|
||||||
|
// TODO: Clicking a link doesn't actually open it in a new tab,
|
||||||
|
// see https://stackoverflow.com/questions/8915845
|
||||||
|
uploadMsg.content = `<a href='${this.getPostUrl(createdPost)}' target='_blank'>Uploaded post</a>`;
|
||||||
|
uploadMsg.type = "success";
|
||||||
|
|
||||||
|
// Find tags with "default" category and update it
|
||||||
|
// TODO: Make all these categories configurable
|
||||||
|
const tagsWithCategory = this.post.tags.filter(x => x.category);
|
||||||
|
const unsetCategoryTags = createdPost.tags
|
||||||
|
.filter(x => x.category == "default")
|
||||||
|
.filter(x => tagsWithCategory.some(y => x.names.includes(y.name)));
|
||||||
|
|
||||||
|
if (unsetCategoryTags.length != 0) {
|
||||||
|
let tagsMsg = new Message(`${unsetCategoryTags.length} tags need a different category`);
|
||||||
|
this.messages.push(tagsMsg);
|
||||||
|
|
||||||
|
// unsetCategoryTags is of type MicroTag[] and we need a Tag resource to update it, so let's get those
|
||||||
|
const query = "?query=" + unsetCategoryTags.map(x => this.encodeTagName(x.names[0]));
|
||||||
|
const tags = (await this.szuru.getTags(query)).results;
|
||||||
|
|
||||||
|
for (let i in tags) {
|
||||||
|
tagsMsg.content = `Updating tag ${i}/${unsetCategoryTags.length}`;
|
||||||
|
tags[i].category = tagsWithCategory.find(x => tags[i].names.includes(x.name))!.category!;
|
||||||
|
await this.szuru.updateTag(tags[i]);
|
||||||
|
}
|
||||||
|
|
||||||
|
tagsMsg.content = `Updated ${tags.length} tags`;
|
||||||
|
tagsMsg.type = "success";
|
||||||
|
}
|
||||||
|
} catch (ex) {
|
||||||
|
const error = ex as SzuruError;
|
||||||
|
|
||||||
|
if (error) {
|
||||||
|
this.clearMessages();
|
||||||
|
this.pushError(error.description);
|
||||||
|
} else {
|
||||||
|
// Probably too long for a message
|
||||||
|
alert(ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
// Open extension settings page in new tab
|
||||||
|
async openSettings() {
|
||||||
|
const url = browser.extension.getURL("options/options.html");
|
||||||
|
window.open(url);
|
||||||
|
},
|
||||||
|
getTagClasses(tag: ScrapedTag): string[] {
|
||||||
|
let classes: string[] = [];
|
||||||
|
|
||||||
|
if (tag.category) {
|
||||||
|
classes.push("tag-" + tag.category);
|
||||||
|
} else {
|
||||||
|
classes.push("tag-general");
|
||||||
|
}
|
||||||
|
|
||||||
|
return classes;
|
||||||
|
},
|
||||||
|
// Remove tag from post's taglist
|
||||||
|
removeTag(tag: ScrapedTag) {
|
||||||
|
if (this.post) {
|
||||||
|
const idx = this.post.tags.indexOf(tag);
|
||||||
|
if (idx != -1) {
|
||||||
|
this.post.tags.splice(idx, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
getMessageClasses(message: Message) {
|
||||||
|
let classes: string[] = [];
|
||||||
|
|
||||||
|
switch (message.type) {
|
||||||
|
case "error":
|
||||||
|
classes.push("message-error");
|
||||||
|
break;
|
||||||
|
case "success":
|
||||||
|
classes.push("message-success");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
return classes;
|
||||||
|
},
|
||||||
|
// Add info message
|
||||||
|
pushInfo(message: string): Message {
|
||||||
|
let msg = new Message(message);
|
||||||
|
this.messages.push(msg);
|
||||||
|
return msg;
|
||||||
|
},
|
||||||
|
// Add error message
|
||||||
|
pushError(message: string): Message {
|
||||||
|
let msg = new Message(message, "error");
|
||||||
|
this.messages.push(msg);
|
||||||
|
return msg;
|
||||||
|
},
|
||||||
|
clearMessages() {
|
||||||
|
this.messages = [];
|
||||||
|
},
|
||||||
|
removeMessage(message: Message) {
|
||||||
|
const idx = this.messages.indexOf(message);
|
||||||
|
if (idx != -1) this.messages.splice(idx, 1);
|
||||||
|
},
|
||||||
|
getPostUrl(post: Post): string {
|
||||||
|
if (!this.activeSite) return "";
|
||||||
|
return this.activeSite.domain + "/post/" + post.id;
|
||||||
|
},
|
||||||
|
encodeTagName(tagName: string) {
|
||||||
|
// Searching for posts with re:zero will show an error message about unknown named token.
|
||||||
|
// Searching for posts with re\:zero will show posts tagged with re:zero.
|
||||||
|
return encodeURIComponent(tagName.replace(/\:/g, "\\:"));
|
||||||
|
},
|
||||||
|
// Add tag to the post's taglist
|
||||||
|
addTag() {
|
||||||
|
if (this.post) {
|
||||||
|
const tagName = this.addTagInput;
|
||||||
|
this.addTagInput = "";
|
||||||
|
if (this.post.tags.find(x => x.name == tagName) == undefined) {
|
||||||
|
this.post.tags.push(new ScrapedTag(tagName));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
// Find posts similar to the grabbed post (this.post)
|
||||||
|
async findSimilar() {
|
||||||
|
if (!this.post || !this.szuru) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Remove messages that contain "Post already uploaded" or "No similar posts found"
|
||||||
|
// but keep the other messages.
|
||||||
|
for (let i = 0; i < this.messages.length; i++) {
|
||||||
|
if (
|
||||||
|
this.messages[i].content.indexOf("No similar posts found") != -1 ||
|
||||||
|
this.messages[i].content.indexOf("Post already uploaded") != -1
|
||||||
|
) {
|
||||||
|
// Decrement i to negate the i++, because if we remove one object the items
|
||||||
|
// shift one place to the left, aka index--
|
||||||
|
// If we don't do i-- we'll skip the next item in our for loop
|
||||||
|
this.messages.splice(i--, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const msgSearchSimilar = this.pushInfo("Searching for similar posts...");
|
||||||
|
const res = await this.szuru.reverseSearch(this.post.imageUrl);
|
||||||
|
this.removeMessage(msgSearchSimilar);
|
||||||
|
|
||||||
|
if (!res.exactPost && res.similarPosts.length == 0) {
|
||||||
|
this.pushInfo("No similar posts found");
|
||||||
|
} else {
|
||||||
|
if (res.exactPost) {
|
||||||
|
this.pushError(
|
||||||
|
`<a href='${this.getPostUrl(res.exactPost)}' target='_blank'>
|
||||||
|
Post already uploaded (${res.exactPost.id})</a>`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (const similarPost of res.similarPosts) {
|
||||||
|
// Don't show this message for the exactPost (because we have a different message for that)
|
||||||
|
if (res.exactPost && res.exactPost.id == similarPost.post.id) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
this.pushInfo(
|
||||||
|
`<a href='${this.getPostUrl(similarPost.post)}' target='_blank'>Post ${similarPost.post.id}
|
||||||
|
looks ${Math.round(100 - similarPost.distance * 100)}% similar</a>`
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
|
formatByteCount(bytes: number, decimals = 2) {
|
||||||
|
if (bytes === 0) return "0 Bytes";
|
||||||
|
|
||||||
|
const k = 1024;
|
||||||
|
const dm = decimals < 0 ? 0 : decimals;
|
||||||
|
const sizes = ["Bytes", "KiB", "MiB"];
|
||||||
|
|
||||||
|
const i = Math.floor(Math.log(bytes) / Math.log(k));
|
||||||
|
|
||||||
|
return parseFloat((bytes / Math.pow(k, i)).toFixed(dm)) + " " + sizes[i];
|
||||||
|
}
|
||||||
|
},
|
||||||
|
computed: {
|
||||||
|
// Get post tooltip which contains the image size and dimensions
|
||||||
|
async postTooltip() {
|
||||||
|
// HACK: Workaround
|
||||||
|
// Probably has to do with https://github.com/vuejs/vue/issues/8721
|
||||||
|
const _this = this as any;
|
||||||
|
|
||||||
|
const img = document.getElementById("imgFull") as HTMLImageElement;
|
||||||
|
if (!img || !_this.post) return "";
|
||||||
|
|
||||||
|
const resp = await axios.head(_this.post.imageUrl);
|
||||||
|
const byteSize = resp.headers["Content-Length"];
|
||||||
|
|
||||||
|
return `${_this.formatByteCount(byteSize)} (${img.naturalWidth}x${img.naturalHeight})`;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
async mounted() {
|
||||||
|
const cfg = await Config.load();
|
||||||
|
this.activeSite = cfg.sites.length > 0 ? cfg.sites[0] : null;
|
||||||
|
|
||||||
|
if (!this.activeSite) {
|
||||||
|
this.pushError("No szurubooru server configured!");
|
||||||
|
} else {
|
||||||
|
this.szuru = await SzuruWrapper.createFromConfig(this.activeSite);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Always call grabPost, even when there is no activeSite
|
||||||
|
this.grabPost().then(() => {
|
||||||
|
if (cfg.autoSearchSimilar) {
|
||||||
|
// No need to check if any vars are unset, findSimilar does that internally
|
||||||
|
this.findSimilar();
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
.popup-container {
|
||||||
|
width: 700px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.popup-row {
|
||||||
|
display: flex;
|
||||||
|
}
|
||||||
|
|
||||||
|
.popup-column1 {
|
||||||
|
flex: 100%;
|
||||||
|
margin: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.popup-column2 {
|
||||||
|
flex: 50%;
|
||||||
|
margin: 10px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.popup-header {
|
||||||
|
height: 30px;
|
||||||
|
padding: 0 0 0 10px;
|
||||||
|
background-color: #eee;
|
||||||
|
margin: 0 0 10px 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.popup-content {
|
||||||
|
margin: 5px;
|
||||||
|
}
|
||||||
|
|
||||||
|
.popup-header > span {
|
||||||
|
vertical-align: middle;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Horrible hack + guesswork */
|
||||||
|
.popup-header > i {
|
||||||
|
float: right;
|
||||||
|
margin: 8px 5px;
|
||||||
|
cursor: pointer;
|
||||||
|
}
|
||||||
|
|
||||||
|
.popup-block {
|
||||||
|
margin: 0 0 10px 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
.block-title {
|
||||||
|
display: block;
|
||||||
|
margin: 0 0 5px 0;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|
|
||||||
|
|
@ -1,13 +0,0 @@
|
||||||
<!doctype html>
|
|
||||||
<html lang="en">
|
|
||||||
<head>
|
|
||||||
<meta charset="UTF-8" />
|
|
||||||
<base target="_blank" />
|
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
|
||||||
<title>Popup</title>
|
|
||||||
</head>
|
|
||||||
<body>
|
|
||||||
<div id="app"></div>
|
|
||||||
<script type="module" src="./main.ts"></script>
|
|
||||||
</body>
|
|
||||||
</html>
|
|
||||||
|
|
@ -1,29 +0,0 @@
|
||||||
import { createApp } from "vue";
|
|
||||||
import { setupApp } from "~/logic/common-setup";
|
|
||||||
import { isChrome, isMobile } from "~/env";
|
|
||||||
import { createRouter, createWebHashHistory } from "vue-router";
|
|
||||||
|
|
||||||
import App from "./App.vue";
|
|
||||||
import PopupMain from "./pages/PopupMain.vue";
|
|
||||||
import MergePost from "./pages/MergePost.vue";
|
|
||||||
|
|
||||||
// Firefox automatically makes the popup as large as can be, Chrome doesn't.
|
|
||||||
// However setting the width to 780px gives us horizontal scrollbars on Firefox.
|
|
||||||
if (isChrome && !isMobile) {
|
|
||||||
document.documentElement.style.width = "780px";
|
|
||||||
}
|
|
||||||
|
|
||||||
const routes = [
|
|
||||||
{ path: "/", component: PopupMain },
|
|
||||||
{ name: "merge", path: "/merge/:siteId/:postId", component: MergePost, props: true },
|
|
||||||
];
|
|
||||||
|
|
||||||
const router = createRouter({
|
|
||||||
history: createWebHashHistory(),
|
|
||||||
routes,
|
|
||||||
});
|
|
||||||
|
|
||||||
const app = createApp(App);
|
|
||||||
setupApp(app);
|
|
||||||
app.use(router);
|
|
||||||
app.mount("#app");
|
|
||||||
|
|
@ -1,402 +0,0 @@
|
||||||
<script lang="ts" setup>
|
|
||||||
import { isEqual, cloneDeep } from "lodash";
|
|
||||||
import SzurubooruApi from "~/api";
|
|
||||||
import { Post, UpdatePostRequest } from "~/api/models";
|
|
||||||
import { isMobile } from "~/env";
|
|
||||||
import { BrowserCommand, PostUpdateCommandData, TagDetails } from "~/models";
|
|
||||||
import { cfg, useMergeStore, usePopupStore } from "~/stores";
|
|
||||||
import { emptyPost, ensurePostHasContentToken, getUrl } from "~/utils";
|
|
||||||
|
|
||||||
const props = defineProps(["siteId", "postId"]);
|
|
||||||
const merge = useMergeStore();
|
|
||||||
const pop = usePopupStore();
|
|
||||||
|
|
||||||
// Why is this cloneDeep needed? Or maybe it isn't?
|
|
||||||
const szuruConfig = cloneDeep(cfg.value.sites.find((x) => x.id == props.siteId))!;
|
|
||||||
const szuru = SzurubooruApi.createFromConfig(szuruConfig);
|
|
||||||
|
|
||||||
// Used for the update post request.
|
|
||||||
// Should NOT be edited by the user, as this is used to compute the changeset, e.g. "Tags: +1 / -3".
|
|
||||||
let existingPostRo: Post | undefined = undefined;
|
|
||||||
|
|
||||||
// Editable existing post.
|
|
||||||
const post = ref<Post>(emptyPost());
|
|
||||||
|
|
||||||
// Readonly scraped post.
|
|
||||||
const scrapedPost = pop.selectedPost;
|
|
||||||
|
|
||||||
// Stuff which the user can change.
|
|
||||||
const imageToKeep = ref<"current" | "new">("new");
|
|
||||||
const tagsToAdd = reactive([] as TagDetails[]);
|
|
||||||
|
|
||||||
// Computed stuff.
|
|
||||||
const sourcesAdded = ref(0);
|
|
||||||
const sourcesRemoved = ref(0);
|
|
||||||
// tagsAdded = tagsToAdd.length
|
|
||||||
const tagsRemoved = ref(0);
|
|
||||||
|
|
||||||
const contentUrl = computed(() => (imageToKeep.value == "current" ? post.value.contentUrl : scrapedPost?.contentUrl));
|
|
||||||
|
|
||||||
function removeExistingTag(tag: TagDetails) {
|
|
||||||
const idx = post.value.tags.findIndex((x) => x.names == tag.names);
|
|
||||||
if (idx != -1) {
|
|
||||||
tagsRemoved.value++;
|
|
||||||
post.value.tags.splice(idx, 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function addTag(tag: TagDetails) {
|
|
||||||
// Only add tag if it doesn't already exist
|
|
||||||
if (
|
|
||||||
tag.name.length > 0 &&
|
|
||||||
tagsToAdd.find((x) => x.name == tag.name) == undefined &&
|
|
||||||
post.value.tags.find((x) => x.names.indexOf(tag.name) != -1) == undefined
|
|
||||||
) {
|
|
||||||
tagsToAdd.push(tag);
|
|
||||||
|
|
||||||
// Add implications for the tag
|
|
||||||
if (cfg.value.addTagImplications) {
|
|
||||||
tagsToAdd.push(...tag.implications);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function removeTagToAdd(tag: TagDetails) {
|
|
||||||
const idx = tagsToAdd.indexOf(tag);
|
|
||||||
if (idx != -1) {
|
|
||||||
tagsToAdd.splice(idx, 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function appendSources() {
|
|
||||||
if (scrapedPost) {
|
|
||||||
const re = /\r?\n/;
|
|
||||||
const sources = post.value.source?.split(re) ?? [];
|
|
||||||
for (const source of scrapedPost.source.split(re)) {
|
|
||||||
if (sources.indexOf(source) == -1) {
|
|
||||||
sources.push(source);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
post.value.source = sources.join("\n");
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function addMissingTags() {
|
|
||||||
if (scrapedPost) {
|
|
||||||
for (const tag of scrapedPost.tags) {
|
|
||||||
if (post.value.tags.find((x) => x.names.indexOf(tag.name) != -1) == undefined) {
|
|
||||||
tagsToAdd.push(tag);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function mergeSafety() {
|
|
||||||
if (scrapedPost) {
|
|
||||||
post.value.safety = scrapedPost.rating;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function getPostUrl(postId: number): string {
|
|
||||||
return getUrl(szuruConfig.domain, "post", postId.toString());
|
|
||||||
}
|
|
||||||
|
|
||||||
async function mergeChanges() {
|
|
||||||
merge.genericError = undefined;
|
|
||||||
|
|
||||||
if (!scrapedPost) {
|
|
||||||
// Should never happend.
|
|
||||||
merge.genericError = "Invalid state. 'scrapedPost' is falsy.";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!existingPostRo) {
|
|
||||||
// Should only happen with an network error I guess?
|
|
||||||
merge.genericError = "Invalid state. 'existingPostRo' is falsy.";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
const req = <UpdatePostRequest>{
|
|
||||||
version: existingPostRo?.version,
|
|
||||||
};
|
|
||||||
|
|
||||||
if (post.value.source != existingPostRo.source) {
|
|
||||||
req.source = post.value.source;
|
|
||||||
}
|
|
||||||
|
|
||||||
const newTags = post.value.tags.map((x) => x.names[0]).concat(tagsToAdd.map((x) => x.name));
|
|
||||||
|
|
||||||
if (
|
|
||||||
!isEqual(
|
|
||||||
newTags,
|
|
||||||
existingPostRo.tags.map((x) => x.names[0]),
|
|
||||||
)
|
|
||||||
) {
|
|
||||||
req.tags = newTags;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (post.value.safety != existingPostRo.safety) {
|
|
||||||
req.safety = post.value.safety;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (imageToKeep.value == "new") {
|
|
||||||
// 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) {
|
|
||||||
merge.genericError = "No changes to merge.";
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
merge.uploadInfo.push({
|
|
||||||
instanceId: szuruConfig.id,
|
|
||||||
postId: `merge-${existingPostRo.id}`,
|
|
||||||
info: { state: "uploading", instancePostId: existingPostRo.id },
|
|
||||||
});
|
|
||||||
|
|
||||||
const cmdData = new PostUpdateCommandData(existingPostRo.id, req, szuruConfig);
|
|
||||||
const cmd = new BrowserCommand("update_post", cmdData);
|
|
||||||
await browser.runtime.sendMessage(cmd);
|
|
||||||
}
|
|
||||||
|
|
||||||
watch(
|
|
||||||
() => post.value.source,
|
|
||||||
(value, oldValue) => {
|
|
||||||
// TODO: Implement this.
|
|
||||||
sourcesAdded.value = 0;
|
|
||||||
sourcesRemoved.value = 0;
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
watch(
|
|
||||||
() => cfg.value.merge.addMissingTags,
|
|
||||||
(value) => {
|
|
||||||
if (value) {
|
|
||||||
addMissingTags();
|
|
||||||
} else if (existingPostRo) {
|
|
||||||
tagsToAdd.splice(0);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
watch(
|
|
||||||
() => cfg.value.merge.appendSource,
|
|
||||||
(value) => {
|
|
||||||
if (value) {
|
|
||||||
appendSources();
|
|
||||||
} else if (existingPostRo) {
|
|
||||||
post.value.source = existingPostRo.source;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
watch(
|
|
||||||
() => cfg.value.merge.mergeSafety,
|
|
||||||
(value) => {
|
|
||||||
if (value) {
|
|
||||||
mergeSafety();
|
|
||||||
} else if (existingPostRo) {
|
|
||||||
post.value.safety = existingPostRo.safety;
|
|
||||||
}
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
onMounted(async () => {
|
|
||||||
// Clear upload info and error.
|
|
||||||
merge.uploadInfo.splice(0);
|
|
||||||
merge.genericError = undefined;
|
|
||||||
|
|
||||||
// Load existing post data.
|
|
||||||
existingPostRo = await szuru?.getPost(props.postId);
|
|
||||||
|
|
||||||
if (existingPostRo) {
|
|
||||||
post.value = cloneDeep(existingPostRo);
|
|
||||||
if (cfg.value.merge.appendSource) appendSources();
|
|
||||||
if (cfg.value.merge.addMissingTags) addMissingTags();
|
|
||||||
if (cfg.value.merge.mergeSafety) mergeSafety();
|
|
||||||
|
|
||||||
if (scrapedPost?.resolution) {
|
|
||||||
// Set imageToKeep depending on which image has the largest resolution.
|
|
||||||
// TODO: Would be neat if this also uses the fileSize.
|
|
||||||
const scrapedPx = scrapedPost.resolution[0] * scrapedPost.resolution[1];
|
|
||||||
const existingPx = existingPostRo.canvasHeight * existingPostRo.canvasWidth;
|
|
||||||
|
|
||||||
if (existingPx >= scrapedPx) {
|
|
||||||
imageToKeep.value = "current";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<template>
|
|
||||||
<div class="popup-messages">
|
|
||||||
<div class="messages">
|
|
||||||
<div v-if="merge.genericError" class="bg-danger">
|
|
||||||
<span>{{ merge.genericError }}</span>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<template v-for="data in merge.uploadInfo">
|
|
||||||
<div :key="data.postId" v-if="data.info.state == 'uploaded' && data.info.instancePostId" class="bg-success">
|
|
||||||
<a :href="getPostUrl(data.info.instancePostId)">Merged into post {{ data.info.instancePostId }}</a>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div :key="data.postId" v-if="data.info.state == 'uploading'"><span>Merging...</span></div>
|
|
||||||
|
|
||||||
<!-- Only show error when it's not undefined and not empty. -->
|
|
||||||
<div :key="data.postId" v-if="data.info.error?.length" class="bg-danger">
|
|
||||||
<span>Couldn't merge into post. {{ data.info.error }}</span>
|
|
||||||
</div>
|
|
||||||
</template>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="popup-columns" :class="{ mobile: isMobile }">
|
|
||||||
<div class="popup-left">
|
|
||||||
<div class="popup-section">
|
|
||||||
<div style="display: flex; flex-direction: column; gap: 5px">
|
|
||||||
<div style="display: flex; gap: 5px">
|
|
||||||
<button class="bg-danger" @click="$router.back()">
|
|
||||||
<font-awesome-icon icon="fa-solid fa-chevron-left" class="cursor-pointer" @click="undefined" />
|
|
||||||
Back
|
|
||||||
</button>
|
|
||||||
<div class="section-header full">
|
|
||||||
<span>Merge into post #{{ props.postId }}</span>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<button class="primary full" @click="mergeChanges">Merge changes</button>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<PopupSection header="Changes">
|
|
||||||
<div class="section-row">
|
|
||||||
<ul class="compact-tags">
|
|
||||||
<li v-show="tagsToAdd.length != 0 || tagsRemoved != 0">
|
|
||||||
Tags: <span class="success" v-show="tagsToAdd.length != 0">+{{ tagsToAdd.length }}</span>
|
|
||||||
<span class="danger" v-show="tagsRemoved != 0">-{{ tagsRemoved }}</span>
|
|
||||||
</li>
|
|
||||||
<li v-show="sourcesAdded != 0 || sourcesRemoved != 0">
|
|
||||||
Sources: <span class="success" v-show="sourcesAdded != 0">+2</span>
|
|
||||||
<span class="danger" v-show="sourcesRemoved != 0">-1</span>
|
|
||||||
</li>
|
|
||||||
<li v-show="existingPostRo?.safety != post.safety">
|
|
||||||
Safety: <s class="danger">{{ existingPostRo?.safety }}</s> <span class="success">{{ post.safety }}</span>
|
|
||||||
</li>
|
|
||||||
<li v-show="imageToKeep == 'new'">
|
|
||||||
<span class="success">Updating post content</span>
|
|
||||||
</li>
|
|
||||||
|
|
||||||
<!-- TODO: Add "No changes" text. -->
|
|
||||||
</ul>
|
|
||||||
</div>
|
|
||||||
</PopupSection>
|
|
||||||
|
|
||||||
<PopupSection header="Basic info">
|
|
||||||
<div class="section-row">
|
|
||||||
<span class="section-label">Safety</span>
|
|
||||||
|
|
||||||
<div style="display: flex; gap: 10px">
|
|
||||||
<label>
|
|
||||||
<input type="radio" value="safe" v-model="post.safety" />
|
|
||||||
Safe
|
|
||||||
</label>
|
|
||||||
|
|
||||||
<label>
|
|
||||||
<input type="radio" value="sketchy" v-model="post.safety" />
|
|
||||||
Sketchy
|
|
||||||
</label>
|
|
||||||
|
|
||||||
<label>
|
|
||||||
<input type="radio" value="unsafe" v-model="post.safety" />
|
|
||||||
Unsafe
|
|
||||||
</label>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="section-row">
|
|
||||||
<span class="section-label">Source</span>
|
|
||||||
<!-- TODO: Fix this -->
|
|
||||||
<textarea v-model="post.source"></textarea>
|
|
||||||
</div>
|
|
||||||
</PopupSection>
|
|
||||||
|
|
||||||
<PopupSection header="Existing tags" toggleable v-model="cfg.merge.expandExistingTags">
|
|
||||||
<div class="section-row">
|
|
||||||
<CompactTags
|
|
||||||
:tags="post.tags.map((x) => TagDetails.fromMicroTag(x))"
|
|
||||||
:show-remove-tag="true"
|
|
||||||
:show-usages="true"
|
|
||||||
@remove-tag="removeExistingTag"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</PopupSection>
|
|
||||||
|
|
||||||
<PopupSection header="Tags to add" toggleable v-model="cfg.merge.expandAddTags">
|
|
||||||
<div class="section-row">
|
|
||||||
<TagInput :szuru="szuru" @add-tag="addTag" />
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="section-row">
|
|
||||||
<CompactTags :tags="tagsToAdd" :show-remove-tag="true" :show-usages="true" @remove-tag="removeTagToAdd" />
|
|
||||||
</div>
|
|
||||||
</PopupSection>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="popup-right">
|
|
||||||
<PopupSection header="Content">
|
|
||||||
<div class="section-row">
|
|
||||||
<div style="display: flex; gap: 10px; margin: 10px 0">
|
|
||||||
<label>
|
|
||||||
<input type="radio" value="current" v-model="imageToKeep" />
|
|
||||||
Current ({{ post.canvasWidth }}x{{ post.canvasHeight }})
|
|
||||||
</label>
|
|
||||||
|
|
||||||
<!-- TODO: Also display filesize -->
|
|
||||||
|
|
||||||
<label>
|
|
||||||
<input type="radio" value="new" v-model="imageToKeep" />
|
|
||||||
New ({{ scrapedPost?.resolution?.[0] }}x{{ scrapedPost?.resolution?.[1] }})
|
|
||||||
</label>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<PostContentDisplay :content-url="contentUrl" content-type="image" />
|
|
||||||
</div>
|
|
||||||
</PopupSection>
|
|
||||||
|
|
||||||
<PopupSection header="Merge options" toggleable v-model="cfg.merge.expandOptions">
|
|
||||||
<div class="section-row">
|
|
||||||
<div style="display: flex; flex-direction: column">
|
|
||||||
<label>
|
|
||||||
<input type="checkbox" v-model="cfg.merge.addMissingTags" />
|
|
||||||
Add missing tags
|
|
||||||
</label>
|
|
||||||
|
|
||||||
<label>
|
|
||||||
<input type="checkbox" v-model="cfg.merge.appendSource" />
|
|
||||||
Append source
|
|
||||||
</label>
|
|
||||||
|
|
||||||
<label>
|
|
||||||
<input type="checkbox" v-model="cfg.merge.mergeSafety" />
|
|
||||||
Merge safety
|
|
||||||
</label>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</PopupSection>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</template>
|
|
||||||
|
|
||||||
<style scoped lang="scss">
|
|
||||||
.popup-left {
|
|
||||||
flex: 1 1 auto;
|
|
||||||
}
|
|
||||||
|
|
||||||
.popup-right {
|
|
||||||
flex: 1 0 300px;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
|
|
@ -1,677 +0,0 @@
|
||||||
<script setup lang="ts">
|
|
||||||
import { useDark } from "@vueuse/core";
|
|
||||||
import { cloneDeep } from "lodash";
|
|
||||||
import { ScrapeResults } from "neo-scraper";
|
|
||||||
import { getUrl, encodeTagName, getErrorMessage, getPostInfoSummary, ensurePostHasContentToken } from "~/utils";
|
|
||||||
import {
|
|
||||||
BrowserCommand,
|
|
||||||
ScrapedPostDetails,
|
|
||||||
TagDetails,
|
|
||||||
SimilarPostInfo,
|
|
||||||
SimpleSimilarPost,
|
|
||||||
SimpleImageSearchResult,
|
|
||||||
PostUploadCommandData,
|
|
||||||
SzuruSiteConfig,
|
|
||||||
PoolDetails,
|
|
||||||
} from "~/models";
|
|
||||||
import { isMobile } from "~/env";
|
|
||||||
import { DeepReadonly } from "vue";
|
|
||||||
import { cfg, usePopupStore } from "~/stores";
|
|
||||||
import SzurubooruApi from "~/api";
|
|
||||||
|
|
||||||
const pop = usePopupStore();
|
|
||||||
const isSearchingForSimilarPosts = ref<number>(0);
|
|
||||||
const enableAutoSearch = ref(true);
|
|
||||||
|
|
||||||
const selectedSite = computed(() => {
|
|
||||||
if (cfg.value.selectedSiteId) {
|
|
||||||
return cfg.value.sites.find((x) => x.id == cfg.value.selectedSiteId);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const szuru = computed(() => {
|
|
||||||
return selectedSite.value ? SzurubooruApi.createFromConfig(selectedSite.value) : undefined;
|
|
||||||
});
|
|
||||||
|
|
||||||
// (Reactive) shorthand for `main.selectedPost.instanceSpecificData.get(config.selectedSiteId)`.
|
|
||||||
// This should only be used as if it were readonly, because the instanceId might change.
|
|
||||||
const instanceSpecificData = readonly(
|
|
||||||
computed(() => {
|
|
||||||
if (pop.selectedPost && cfg.value.selectedSiteId) {
|
|
||||||
return pop.selectedPost.instanceSpecificData[cfg.value.selectedSiteId];
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|
||||||
watch(
|
|
||||||
() => pop.selectedPostId,
|
|
||||||
(value) => {
|
|
||||||
// Call findSimilar if wanted.
|
|
||||||
if (cfg.value.autoSearchSimilar && enableAutoSearch.value) {
|
|
||||||
let selectedPost = pop.posts.find((x) => x.id == value);
|
|
||||||
if (selectedPost) findSimilar(selectedPost);
|
|
||||||
}
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
watch(
|
|
||||||
() => cfg.value.selectedSiteId,
|
|
||||||
async (value, oldValue) => {
|
|
||||||
// If changed
|
|
||||||
if (value != oldValue) {
|
|
||||||
// Update selectedSiteId in config
|
|
||||||
cfg.value.selectedSiteId = value;
|
|
||||||
// Call findSimilar if wanted.
|
|
||||||
if (cfg.value.autoSearchSimilar && pop.selectedPost) {
|
|
||||||
findSimilar(pop.selectedPost);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
function openOptionsPage() {
|
|
||||||
browser.runtime.openOptionsPage();
|
|
||||||
}
|
|
||||||
|
|
||||||
async function getSelectedTabIds(): Promise<number[]> {
|
|
||||||
const selectedTabs = await browser.tabs.query({
|
|
||||||
highlighted: true,
|
|
||||||
currentWindow: true,
|
|
||||||
});
|
|
||||||
|
|
||||||
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 selectedTabIds = await getSelectedTabIds();
|
|
||||||
|
|
||||||
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);
|
|
||||||
|
|
||||||
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 scrapeResults.results) {
|
|
||||||
for (const i in result.posts) {
|
|
||||||
const vm = new ScrapedPostDetails(result.posts[i]);
|
|
||||||
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
|
|
||||||
if (cfg.value.addPageUrlToSource || vm.source == "") {
|
|
||||||
if (vm.source != "") vm.source += "\n";
|
|
||||||
vm.source += vm.pageUrl;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Initialize instanceSpecificData with an empty object.
|
|
||||||
for (const site of cfg.value.sites) {
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (pop.posts.length > 0) {
|
|
||||||
// Hack so that findSimilar does not trigger when we set selectedPostId, due to the watch(...).
|
|
||||||
// This because we might change the contentUrl in fetchPostInfo, after which we'd need to call findSimilar again.
|
|
||||||
// In which case it would be better to just call it once.
|
|
||||||
enableAutoSearch.value = false;
|
|
||||||
pop.selectedPostId = pop.posts[0].id;
|
|
||||||
|
|
||||||
if (cfg.value.loadTagCounts) {
|
|
||||||
loadTagCounts();
|
|
||||||
}
|
|
||||||
|
|
||||||
if (cfg.value.fetchPostInfo) {
|
|
||||||
await fetchPostsInfo();
|
|
||||||
}
|
|
||||||
|
|
||||||
// Delayed call to findSimilar, as fetchPostInfo might change the post's contentUrl.
|
|
||||||
enableAutoSearch.value = true;
|
|
||||||
if (cfg.value.autoSearchSimilar) {
|
|
||||||
findSimilar(pop.selectedPost);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function upload() {
|
|
||||||
if (!cfg.value.selectedSiteId) return;
|
|
||||||
let instanceSpecificData = pop.selectedPost?.instanceSpecificData[cfg.value.selectedSiteId];
|
|
||||||
|
|
||||||
// Don't try to upload if an exact copy is already on the server.
|
|
||||||
if (instanceSpecificData?.reverseSearchResult?.exactPostId) return;
|
|
||||||
|
|
||||||
if (instanceSpecificData) {
|
|
||||||
instanceSpecificData.uploadState = { state: "uploading" };
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
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) {
|
|
||||||
if (instanceSpecificData?.uploadState) {
|
|
||||||
instanceSpecificData.uploadState.state = "error";
|
|
||||||
instanceSpecificData.uploadState.error = ex.toString();
|
|
||||||
}
|
|
||||||
throw ex;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function removeTag(tag: TagDetails) {
|
|
||||||
if (pop.selectedPost) {
|
|
||||||
const idx = pop.selectedPost.tags.indexOf(tag);
|
|
||||||
if (idx != -1) {
|
|
||||||
pop.selectedPost.tags.splice(idx, 1);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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());
|
|
||||||
}
|
|
||||||
|
|
||||||
function getSimilarPosts(data?: DeepReadonly<SimpleImageSearchResult>): SimilarPostInfo[] {
|
|
||||||
if (!data) return [];
|
|
||||||
|
|
||||||
const lst: SimilarPostInfo[] = [];
|
|
||||||
|
|
||||||
for (const similarPost of data.similarPosts) {
|
|
||||||
if (data.exactPostId == similarPost.postId) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
lst.push(new SimilarPostInfo(similarPost.postId, Math.round(100 - similarPost.distance * 100)));
|
|
||||||
}
|
|
||||||
|
|
||||||
return lst;
|
|
||||||
}
|
|
||||||
|
|
||||||
function addTag(tag: TagDetails) {
|
|
||||||
if (pop.selectedPost) {
|
|
||||||
// Only add tag if it doesn't already exist
|
|
||||||
if (tag.name.length > 0 && pop.selectedPost.tags.find((x) => x.name == tag.name) == undefined) {
|
|
||||||
pop.selectedPost.tags.push(tag);
|
|
||||||
|
|
||||||
// Add implications for the tag
|
|
||||||
if (cfg.value.addTagImplications) {
|
|
||||||
pop.selectedPost.tags.push(...tag.implications);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
|
|
||||||
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];
|
|
||||||
|
|
||||||
if (!instanceSpecificData) {
|
|
||||||
console.error("instanceSpecificData is undefined. This should never happen!");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Don't load this again if it is already loaded.
|
|
||||||
if (instanceSpecificData.reverseSearchResult) return;
|
|
||||||
|
|
||||||
isSearchingForSimilarPosts.value++;
|
|
||||||
|
|
||||||
try {
|
|
||||||
await ensurePostHasContentToken(selectedInstance, post, cfg);
|
|
||||||
|
|
||||||
const res = await selectedInstance.reverseSearchToken(instanceSpecificData.contentToken!);
|
|
||||||
|
|
||||||
instanceSpecificData.reverseSearchResult = {
|
|
||||||
exactPostId: res.exactPost?.id,
|
|
||||||
similarPosts: res.similarPosts.map((x) => <SimpleSimilarPost>{ postId: x.post.id, distance: x.distance }),
|
|
||||||
};
|
|
||||||
} catch (ex: any) {
|
|
||||||
instanceSpecificData.genericError = "Couldn't reverse search. " + getErrorMessage(ex);
|
|
||||||
}
|
|
||||||
|
|
||||||
isSearchingForSimilarPosts.value--;
|
|
||||||
}
|
|
||||||
|
|
||||||
async function loadTagCounts() {
|
|
||||||
const allTags = pop.posts.flatMap((x) => x.tags);
|
|
||||||
|
|
||||||
for (let i = 0; i < allTags.length; i += 100) {
|
|
||||||
const query = allTags
|
|
||||||
.slice(i, i + 101)
|
|
||||||
.map((x) => encodeTagName(x.name))
|
|
||||||
.join();
|
|
||||||
|
|
||||||
const resp = await szuru.value?.getTags(query);
|
|
||||||
|
|
||||||
// Not the best code, but it works I guess?
|
|
||||||
if (resp) {
|
|
||||||
for (let post of pop.posts)
|
|
||||||
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 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) {
|
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Cool people would use https://kazupon.github.io/vue-i18n/guide/pluralization.html
|
|
||||||
function getUpdatedTagsText(count: number) {
|
|
||||||
return `Updated ${count} tag${count > 1 ? "s" : ""}`;
|
|
||||||
}
|
|
||||||
|
|
||||||
function onResolutionLoaded(res: any) {
|
|
||||||
// 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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
onMounted(() => {
|
|
||||||
if (pop.posts.length == 0) {
|
|
||||||
grabPost();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
useDark();
|
|
||||||
</script>
|
|
||||||
|
|
||||||
<template>
|
|
||||||
<div class="popup-messages">
|
|
||||||
<div class="messages">
|
|
||||||
<!--
|
|
||||||
Error messages
|
|
||||||
-->
|
|
||||||
|
|
||||||
<div v-if="cfg.sites.length == 0" class="bg-danger">
|
|
||||||
<span>No szurubooru servers configured!</span>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div v-if="!szuru && cfg.sites.length != 0" class="bg-danger">
|
|
||||||
<span>No target server selected!</span>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div v-if="instanceSpecificData?.reverseSearchResult?.exactPostId" class="bg-danger has-merge-button">
|
|
||||||
<a :href="getActiveSitePostUrl(instanceSpecificData.reverseSearchResult?.exactPostId)">
|
|
||||||
Post already uploaded ({{ instanceSpecificData.reverseSearchResult.exactPostId }})
|
|
||||||
</a>
|
|
||||||
|
|
||||||
<!-- TODO: Maybe a 5px gap here? -->
|
|
||||||
|
|
||||||
<div style="display: flex; background: purple; border-radius: 0 2px 2px 0">
|
|
||||||
<router-link
|
|
||||||
:to="{
|
|
||||||
name: 'merge',
|
|
||||||
params: {
|
|
||||||
siteId: cfg.selectedSiteId,
|
|
||||||
postId: instanceSpecificData?.reverseSearchResult?.exactPostId,
|
|
||||||
},
|
|
||||||
}"
|
|
||||||
style="padding: 5px 20px"
|
|
||||||
>Merge</router-link
|
|
||||||
>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!--
|
|
||||||
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>
|
|
||||||
|
|
||||||
<div v-if="instanceSpecificData?.genericError" class="bg-danger">
|
|
||||||
<span>{{ instanceSpecificData.genericError }}</span>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!--
|
|
||||||
Success messages
|
|
||||||
-->
|
|
||||||
|
|
||||||
<!-- TODO: Clicking a link doesn't actually open it in a new tab, see https://stackoverflow.com/questions/8915845 -->
|
|
||||||
<div
|
|
||||||
v-if="
|
|
||||||
instanceSpecificData?.uploadState?.state == 'uploaded' && instanceSpecificData?.uploadState.instancePostId
|
|
||||||
"
|
|
||||||
class="bg-success"
|
|
||||||
>
|
|
||||||
<a :href="getActiveSitePostUrl(instanceSpecificData.uploadState.instancePostId)"
|
|
||||||
>Uploaded post {{ instanceSpecificData.uploadState.instancePostId }}</a
|
|
||||||
>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div v-if="instanceSpecificData?.uploadState?.updateTagsState?.totalChanged" class="bg-success">
|
|
||||||
<span>{{ getUpdatedTagsText(instanceSpecificData.uploadState?.updateTagsState?.totalChanged) }}</span>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<!--
|
|
||||||
Info messages
|
|
||||||
-->
|
|
||||||
|
|
||||||
<div v-if="instanceSpecificData?.uploadState?.state == 'uploading'"><span>Uploading...</span></div>
|
|
||||||
|
|
||||||
<div
|
|
||||||
v-if="
|
|
||||||
instanceSpecificData?.uploadState?.updateTagsState?.total &&
|
|
||||||
instanceSpecificData.uploadState?.updateTagsState?.current == undefined
|
|
||||||
"
|
|
||||||
>
|
|
||||||
<span>{{ instanceSpecificData.uploadState?.updateTagsState?.total }} tags need a different category</span>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div
|
|
||||||
v-if="
|
|
||||||
instanceSpecificData?.uploadState?.updateTagsState?.current &&
|
|
||||||
instanceSpecificData?.uploadState?.updateTagsState?.totalChanged == undefined
|
|
||||||
"
|
|
||||||
>
|
|
||||||
<span
|
|
||||||
>Updating tag {{ instanceSpecificData.uploadState?.updateTagsState?.current }}/{{
|
|
||||||
instanceSpecificData.uploadState?.updateTagsState?.total
|
|
||||||
}}</span
|
|
||||||
>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div v-if="isSearchingForSimilarPosts > 0 && instanceSpecificData?.uploadState == undefined">
|
|
||||||
<span>Searching for similar posts...</span>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div
|
|
||||||
v-if="
|
|
||||||
instanceSpecificData?.reverseSearchResult?.similarPosts.length == 0 &&
|
|
||||||
instanceSpecificData.uploadState == undefined
|
|
||||||
"
|
|
||||||
>
|
|
||||||
<span>No similar posts found</span>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div
|
|
||||||
v-for="similarPost in getSimilarPosts(instanceSpecificData?.reverseSearchResult)"
|
|
||||||
:key="similarPost.id"
|
|
||||||
class="has-merge-button"
|
|
||||||
>
|
|
||||||
<a :href="getActiveSitePostUrl(similarPost.id)"
|
|
||||||
>Post {{ similarPost.id }} looks {{ similarPost.percentage }}% similar</a
|
|
||||||
>
|
|
||||||
|
|
||||||
<!-- TODO: Maybe a 5px gap here? -->
|
|
||||||
|
|
||||||
<div style="display: flex; background: purple; border-radius: 0 2px 2px 0">
|
|
||||||
<router-link
|
|
||||||
:to="{
|
|
||||||
name: 'merge',
|
|
||||||
params: {
|
|
||||||
siteId: cfg.selectedSiteId,
|
|
||||||
postId: similarPost.id,
|
|
||||||
},
|
|
||||||
}"
|
|
||||||
style="padding: 5px 20px"
|
|
||||||
>Merge</router-link
|
|
||||||
>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div v-if="pop.selectedPost" class="popup-columns" :class="{ mobile: isMobile }">
|
|
||||||
<div class="popup-left">
|
|
||||||
<PopupSection>
|
|
||||||
<template #header>
|
|
||||||
<span>Basic info</span>
|
|
||||||
<font-awesome-icon icon="fa-solid fa-cog" class="cursor-pointer" @click="openOptionsPage" />
|
|
||||||
</template>
|
|
||||||
|
|
||||||
<div class="section-row" v-if="pop.selectedPost?.resolution || pop.selectedPost?.contentSize">
|
|
||||||
<PostInfoSummary v-model="pop.selectedPost" />
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="section-row">
|
|
||||||
<span class="section-label">Safety</span>
|
|
||||||
|
|
||||||
<div style="display: flex; gap: 10px">
|
|
||||||
<label>
|
|
||||||
<input type="radio" value="safe" v-model="pop.selectedPost.rating" />
|
|
||||||
Safe
|
|
||||||
</label>
|
|
||||||
|
|
||||||
<label>
|
|
||||||
<input type="radio" value="sketchy" v-model="pop.selectedPost.rating" />
|
|
||||||
Sketchy
|
|
||||||
</label>
|
|
||||||
|
|
||||||
<label>
|
|
||||||
<input type="radio" value="unsafe" v-model="pop.selectedPost.rating" />
|
|
||||||
Unsafe
|
|
||||||
</label>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<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" toggleable v-model="cfg.popup.expandTags">
|
|
||||||
<div class="section-row">
|
|
||||||
<TagInput :szuru="szuru" @add-tag="addTag" />
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="section-row">
|
|
||||||
<CompactTags
|
|
||||||
:tags="pop.selectedPost.tags"
|
|
||||||
:show-remove-tag="true"
|
|
||||||
:show-usages="cfg.loadTagCounts"
|
|
||||||
@remove-tag="removeTag"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</PopupSection>
|
|
||||||
|
|
||||||
<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">
|
|
||||||
{{ site.domain }}
|
|
||||||
</option>
|
|
||||||
</select>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="popup-right">
|
|
||||||
<div class="popup-section">
|
|
||||||
<select v-model="pop.selectedPostId">
|
|
||||||
<!--    is used because duplicate space characters are usually removed by the browser. -->
|
|
||||||
<option v-for="post in pop.posts" :key="post.id" :value="post.id">
|
|
||||||
{{ post.name }}  -  {{ getPostInfoSummary(post) }}
|
|
||||||
</option>
|
|
||||||
</select>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="popup-section">
|
|
||||||
<div style="display: flex; gap: 5px">
|
|
||||||
<button v-if="!cfg.autoSearchSimilar" class="primary" @click="clickFindSimilar">Find similar</button>
|
|
||||||
<button class="primary full" @click="upload">Import</button>
|
|
||||||
<!-- TODO: Add button to open this popup in fullscreen. -->
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div class="popup-section">
|
|
||||||
<PostContentDisplay
|
|
||||||
:content-url="pop.selectedPost.contentUrl"
|
|
||||||
:content-type="pop.selectedPost.contentType"
|
|
||||||
@on-resolution-loaded="onResolutionLoaded"
|
|
||||||
/>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
</div>
|
|
||||||
|
|
||||||
<div v-else class="p-3 flex items-center gap-3">
|
|
||||||
<span>No content found</span>
|
|
||||||
<font-awesome-icon icon="fa-solid fa-cog" class="cursor-pointer" @click="openOptionsPage" />
|
|
||||||
</div>
|
|
||||||
</template>
|
|
||||||
|
|
||||||
<style scoped lang="scss">
|
|
||||||
.popup-left {
|
|
||||||
flex: 1 0 300px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.popup-right {
|
|
||||||
flex: 1 1 auto;
|
|
||||||
}
|
|
||||||
|
|
||||||
.has-merge-button {
|
|
||||||
display: flex;
|
|
||||||
justify-content: space-between;
|
|
||||||
}
|
|
||||||
</style>
|
|
||||||
|
|
@ -0,0 +1,18 @@
|
||||||
|
<!DOCTYPE html>
|
||||||
|
<html lang="en">
|
||||||
|
|
||||||
|
<head>
|
||||||
|
<meta charset="UTF-8" />
|
||||||
|
<title>Title</title>
|
||||||
|
<link rel="stylesheet" href="popup.css" />
|
||||||
|
<% if (NODE_ENV === 'development') { %>
|
||||||
|
<!-- Load some resources only in development environment -->
|
||||||
|
<% } %>
|
||||||
|
</head>
|
||||||
|
|
||||||
|
<body>
|
||||||
|
<div id="app"></div>
|
||||||
|
<script src="popup.js"></script>
|
||||||
|
</body>
|
||||||
|
|
||||||
|
</html>
|
||||||
|
|
@ -0,0 +1,11 @@
|
||||||
|
import '../css/style.css';
|
||||||
|
import '../css/fontawesome.css';
|
||||||
|
import '../css/solid.css';
|
||||||
|
|
||||||
|
import Vue from 'vue';
|
||||||
|
import App from './App';
|
||||||
|
|
||||||
|
new Vue({
|
||||||
|
el: '#app',
|
||||||
|
render: h => h(App)
|
||||||
|
})
|
||||||
|
|
@ -0,0 +1,2 @@
|
||||||
|
// Dummy file so that webpack-extension-reloader doesn't complain when running `npm run watch:dev`
|
||||||
|
// This file is excluded when not running in development mode
|
||||||
|
|
@ -0,0 +1,15 @@
|
||||||
|
import { NeoScraper, ScrapeResults } from "neo-scraper";
|
||||||
|
import { browser, Runtime } from "webextension-polyfill-ts";
|
||||||
|
|
||||||
|
function grabPost(): ScrapeResults {
|
||||||
|
var scraper = new NeoScraper();
|
||||||
|
return scraper.scrapeDocument(document, undefined, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
async function messageHandler(message: any, sender: Runtime.MessageSender): Promise<any> {
|
||||||
|
switch (message) {
|
||||||
|
case "grab_post": return grabPost();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
browser.runtime.onMessage.addListener(messageHandler);
|
||||||
|
|
@ -1,96 +0,0 @@
|
||||||
import { defineStore } from "pinia";
|
|
||||||
import deepMerge from "deepmerge";
|
|
||||||
import {
|
|
||||||
type TagDetails,
|
|
||||||
getDefaultTagCategories,
|
|
||||||
type ScrapedPostDetails,
|
|
||||||
type SetPostUploadInfoData,
|
|
||||||
type SzuruSiteConfig,
|
|
||||||
type TagCategoryColor,
|
|
||||||
} from "~/models";
|
|
||||||
import { useStorageLocal } from "~/composables/useStorageLocal";
|
|
||||||
|
|
||||||
export const cfg = useStorageLocal(
|
|
||||||
"config",
|
|
||||||
{
|
|
||||||
version: 0,
|
|
||||||
addPageUrlToSource: true,
|
|
||||||
alwaysUploadAsContent: false,
|
|
||||||
sendUploadNotifications: false,
|
|
||||||
autoSearchSimilar: false,
|
|
||||||
loadTagCounts: true,
|
|
||||||
fetchPostInfo: true,
|
|
||||||
useContentTokens: true,
|
|
||||||
sites: [] as Array<SzuruSiteConfig>,
|
|
||||||
selectedSiteId: undefined as string | undefined,
|
|
||||||
addTagImplications: true,
|
|
||||||
addAllParsedTags: true,
|
|
||||||
merge: {
|
|
||||||
expandOptions: true,
|
|
||||||
expandExistingTags: false,
|
|
||||||
expandAddTags: true,
|
|
||||||
addMissingTags: true,
|
|
||||||
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) {
|
|
||||||
const cfg = deepMerge(defaults, storageValue);
|
|
||||||
const oldVersion = cfg.version;
|
|
||||||
|
|
||||||
// Crappy config migration.
|
|
||||||
switch (cfg.version) {
|
|
||||||
case 0:
|
|
||||||
cfg.version++;
|
|
||||||
|
|
||||||
// Don't clear the existing tagCategories and don't add duplicates.
|
|
||||||
for (const cat of getDefaultTagCategories()) {
|
|
||||||
if (!cfg.tagCategories.find((x) => x.name == cat.name)) {
|
|
||||||
cfg.tagCategories.push(cat);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (oldVersion != cfg.version) {
|
|
||||||
console.log(`Migrated config from version ${oldVersion} to ${cfg.version}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return cfg;
|
|
||||||
},
|
|
||||||
},
|
|
||||||
);
|
|
||||||
|
|
||||||
export const usePopupStore = defineStore("popup", {
|
|
||||||
state: () => ({
|
|
||||||
posts: [] as ScrapedPostDetails[],
|
|
||||||
selectedPostId: undefined as string | undefined,
|
|
||||||
isSearchingForSimilarPosts: 0,
|
|
||||||
}),
|
|
||||||
getters: {
|
|
||||||
selectedPost: (state) => {
|
|
||||||
return state.posts.find((x) => x.id == state.selectedPostId);
|
|
||||||
},
|
|
||||||
},
|
|
||||||
actions: {
|
|
||||||
getPostForContentUrl(contentUrl: string): ScrapedPostDetails | undefined {
|
|
||||||
return this.posts.find((x) => x.contentUrl == contentUrl);
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
export const useMergeStore = defineStore("merge", {
|
|
||||||
state: () => ({
|
|
||||||
uploadInfo: [] as SetPostUploadInfoData[],
|
|
||||||
genericError: undefined as string | undefined,
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
|
|
@ -1,276 +0,0 @@
|
||||||
@use "sass:color";
|
|
||||||
|
|
||||||
@use "primeflex/primeflex.scss";
|
|
||||||
|
|
||||||
/* open-sans-regular - latin */
|
|
||||||
@font-face {
|
|
||||||
font-family: "Open Sans";
|
|
||||||
font-style: normal;
|
|
||||||
font-weight: 400;
|
|
||||||
src:
|
|
||||||
local(""),
|
|
||||||
url("/assets/open-sans-v34-latin-regular.woff2") format("woff2");
|
|
||||||
}
|
|
||||||
|
|
||||||
*,
|
|
||||||
*:before,
|
|
||||||
*:after {
|
|
||||||
box-sizing: border-box;
|
|
||||||
}
|
|
||||||
|
|
||||||
body {
|
|
||||||
font-family: "Open Sans", sans-serif;
|
|
||||||
font-size: 16px;
|
|
||||||
}
|
|
||||||
|
|
||||||
$primary-color: #24aadd;
|
|
||||||
$danger-color: #f44336;
|
|
||||||
|
|
||||||
$primary-color-dark: color.scale($primary-color, $lightness: -30%);
|
|
||||||
$danger-color-dark: color.scale($danger-color, $lightness: -50%);
|
|
||||||
// TODO: Darker success color
|
|
||||||
|
|
||||||
:root {
|
|
||||||
--button-bg-color: #a2a2a2;
|
|
||||||
--primary-color: #{$primary-color};
|
|
||||||
--bg-main-color: #fff;
|
|
||||||
--bg-secondary-color: #fafafa;
|
|
||||||
--border-color: #eee;
|
|
||||||
--secondary-text: #888;
|
|
||||||
--danger-color: #{$danger-color};
|
|
||||||
--success-color: #4caf50;
|
|
||||||
--section-header-fg-color: #4c4c4c;
|
|
||||||
--section-header-bg-color: #eee;
|
|
||||||
--text-color: #111;
|
|
||||||
--tab-color: rgba(0, 0, 0, 0.06);
|
|
||||||
}
|
|
||||||
|
|
||||||
html.dark {
|
|
||||||
--button-bg-color: #666;
|
|
||||||
--primary-color: #{$primary-color-dark};
|
|
||||||
--bg-main-color: #1a1a1a;
|
|
||||||
--bg-secondary-color: #3e3e3e;
|
|
||||||
--border-color: #474747;
|
|
||||||
--danger-color: #{$danger-color-dark};
|
|
||||||
--section-header-fg-color: #d9d9d9;
|
|
||||||
--section-header-bg-color: #272727;
|
|
||||||
--text-color: #e6e6e6;
|
|
||||||
--tab-color: rgba(255, 255, 255, 0.06);
|
|
||||||
color: var(--text-color);
|
|
||||||
background-color: var(--bg-main-color);
|
|
||||||
color-scheme: dark;
|
|
||||||
}
|
|
||||||
|
|
||||||
body {
|
|
||||||
margin: 0px;
|
|
||||||
display: block;
|
|
||||||
}
|
|
||||||
|
|
||||||
input[type="email"],
|
|
||||||
input[type="number"],
|
|
||||||
input[type="password"],
|
|
||||||
input[type="text"],
|
|
||||||
select,
|
|
||||||
button {
|
|
||||||
height: 30px;
|
|
||||||
}
|
|
||||||
|
|
||||||
button {
|
|
||||||
background-color: var(--button-bg-color);
|
|
||||||
border: none;
|
|
||||||
color: #fff;
|
|
||||||
|
|
||||||
cursor: pointer;
|
|
||||||
padding: 0 10px;
|
|
||||||
white-space: nowrap;
|
|
||||||
}
|
|
||||||
|
|
||||||
button.primary {
|
|
||||||
background-color: var(--primary-color);
|
|
||||||
}
|
|
||||||
|
|
||||||
input[type="email"],
|
|
||||||
input[type="number"],
|
|
||||||
input[type="password"],
|
|
||||||
input[type="text"],
|
|
||||||
select {
|
|
||||||
color: var(--text-color);
|
|
||||||
background-color: var(--bg-secondary-color);
|
|
||||||
border: 2px solid var(--border-color);
|
|
||||||
|
|
||||||
padding: 0.2em 0.3em;
|
|
||||||
// box-sizing: border-box;
|
|
||||||
|
|
||||||
width: 100%;
|
|
||||||
|
|
||||||
&:focus {
|
|
||||||
outline: var(--primary-color) solid 2px;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
input[type="radio"] {
|
|
||||||
margin: 3px 3px 0 5px;
|
|
||||||
}
|
|
||||||
|
|
||||||
textarea {
|
|
||||||
color: var(--text-color);
|
|
||||||
background-color: var(--bg-secondary-color);
|
|
||||||
border: 2px solid var(--border-color);
|
|
||||||
width: 100%;
|
|
||||||
white-space: pre;
|
|
||||||
overflow-wrap: normal;
|
|
||||||
overflow-x: scroll;
|
|
||||||
text-overflow: ellipsis;
|
|
||||||
resize: vertical;
|
|
||||||
|
|
||||||
&:focus {
|
|
||||||
outline: var(--primary-color) solid 2px;
|
|
||||||
// box-sizing: content-box;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
img {
|
|
||||||
max-width: 100%;
|
|
||||||
max-height: 100%;
|
|
||||||
}
|
|
||||||
|
|
||||||
.flex {
|
|
||||||
display: flex;
|
|
||||||
}
|
|
||||||
|
|
||||||
// DEPRECATED: Replace with primeflex w-full
|
|
||||||
.full {
|
|
||||||
width: 100%;
|
|
||||||
}
|
|
||||||
|
|
||||||
ul.compact {
|
|
||||||
list-style-type: none;
|
|
||||||
padding: 0;
|
|
||||||
display: inline;
|
|
||||||
}
|
|
||||||
|
|
||||||
.compact-tags,
|
|
||||||
.compact-pools {
|
|
||||||
list-style-type: none;
|
|
||||||
padding: 0;
|
|
||||||
display: inline;
|
|
||||||
|
|
||||||
li {
|
|
||||||
display: flex;
|
|
||||||
align-items: center;
|
|
||||||
gap: 0.5em;
|
|
||||||
line-height: 1.2rem;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
.tag-general {
|
|
||||||
color: var(--primary-color);
|
|
||||||
}
|
|
||||||
|
|
||||||
.remove-tag {
|
|
||||||
color: var(--secondary-text);
|
|
||||||
}
|
|
||||||
|
|
||||||
.tag-usages {
|
|
||||||
color: var(--secondary-text);
|
|
||||||
font-size: 90%;
|
|
||||||
text-indent: initial;
|
|
||||||
}
|
|
||||||
|
|
||||||
a.remove-tag {
|
|
||||||
text-decoration: none;
|
|
||||||
cursor: pointer;
|
|
||||||
}
|
|
||||||
|
|
||||||
.messages {
|
|
||||||
> div {
|
|
||||||
display: flex;
|
|
||||||
border-radius: 2px;
|
|
||||||
background: var(--primary-color);
|
|
||||||
color: #fff;
|
|
||||||
|
|
||||||
a,
|
|
||||||
span {
|
|
||||||
padding: 5px;
|
|
||||||
}
|
|
||||||
|
|
||||||
a {
|
|
||||||
color: #fff;
|
|
||||||
text-decoration: none;
|
|
||||||
}
|
|
||||||
|
|
||||||
&:first-child {
|
|
||||||
margin-top: 10px;
|
|
||||||
}
|
|
||||||
|
|
||||||
&:not(:last-child) {
|
|
||||||
margin-bottom: 5px;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
.bg-danger {
|
|
||||||
background: var(--danger-color) !important;
|
|
||||||
}
|
|
||||||
|
|
||||||
.bg-success {
|
|
||||||
background: var(--success-color) !important;
|
|
||||||
}
|
|
||||||
|
|
||||||
.danger {
|
|
||||||
color: var(--danger-color);
|
|
||||||
}
|
|
||||||
|
|
||||||
.success {
|
|
||||||
color: var(--success-color);
|
|
||||||
}
|
|
||||||
|
|
||||||
.cursor-pointer {
|
|
||||||
cursor: pointer;
|
|
||||||
}
|
|
||||||
|
|
||||||
.popup-section {
|
|
||||||
margin: 10px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.section-header {
|
|
||||||
height: 30px;
|
|
||||||
padding: 0 6px 0 10px;
|
|
||||||
background-color: var(--section-header-bg-color);
|
|
||||||
display: flex;
|
|
||||||
justify-content: space-between;
|
|
||||||
align-items: center;
|
|
||||||
|
|
||||||
> span {
|
|
||||||
font-size: 1.2em;
|
|
||||||
vertical-align: middle;
|
|
||||||
color: var(--section-header-fg-color);
|
|
||||||
}
|
|
||||||
|
|
||||||
&.toggleable {
|
|
||||||
cursor: pointer;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
.section-row {
|
|
||||||
margin: 10px 5px 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
.section-label {
|
|
||||||
display: block;
|
|
||||||
margin: 0 0 5px 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
.section-block {
|
|
||||||
display: block;
|
|
||||||
}
|
|
||||||
|
|
||||||
.color-primary {
|
|
||||||
color: var(--primary-color);
|
|
||||||
}
|
|
||||||
|
|
||||||
.separated-content > * + * {
|
|
||||||
padding-top: 1rem;
|
|
||||||
margin-top: 1.5rem;
|
|
||||||
border-top: 3px solid #333;
|
|
||||||
}
|
|
||||||
|
|
@ -1,35 +0,0 @@
|
||||||
.popup-container {
|
|
||||||
display: flex;
|
|
||||||
flex-direction: column;
|
|
||||||
}
|
|
||||||
|
|
||||||
.popup-messages {
|
|
||||||
grid-column: 1 / 3;
|
|
||||||
margin: 0 10px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.popup-columns {
|
|
||||||
display: flex;
|
|
||||||
flex-direction: row;
|
|
||||||
|
|
||||||
&.mobile {
|
|
||||||
flex-direction: column-reverse;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
video {
|
|
||||||
width: 100%;
|
|
||||||
}
|
|
||||||
|
|
||||||
textarea,
|
|
||||||
select {
|
|
||||||
font-size: 90%;
|
|
||||||
}
|
|
||||||
|
|
||||||
label {
|
|
||||||
flex: 1 0 auto;
|
|
||||||
|
|
||||||
input[type="radio"] {
|
|
||||||
margin: 3px 3px 0 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,27 +0,0 @@
|
||||||
.p-tabview {
|
|
||||||
.p-tabview-nav {
|
|
||||||
margin: 1em 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
.p-tabview-header {
|
|
||||||
span {
|
|
||||||
padding: 0 1.2em;
|
|
||||||
line-height: 2.3em;
|
|
||||||
}
|
|
||||||
|
|
||||||
&.p-highlight {
|
|
||||||
background-color: var(--tab-color);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
.p-datatable {
|
|
||||||
margin: -0.4em;
|
|
||||||
|
|
||||||
.p-datatable-table {
|
|
||||||
th,
|
|
||||||
td {
|
|
||||||
padding: 0.4em;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,7 +0,0 @@
|
||||||
import { describe, expect, it } from "vitest";
|
|
||||||
|
|
||||||
describe("Demo", () => {
|
|
||||||
it("should work", () => {
|
|
||||||
expect(1 + 1).toBe(2);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
130
src/utils.ts
|
|
@ -1,130 +0,0 @@
|
||||||
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(/\/+$/, "");
|
|
||||||
for (const part of parts) {
|
|
||||||
url += "/" + part.replace(/\/+$/, "");
|
|
||||||
}
|
|
||||||
return url;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function encodeTagName(tagName: string) {
|
|
||||||
// Searching for posts with re:zero will show an error message about unknown named token.
|
|
||||||
// Searching for posts with re\:zero will show posts tagged with re:zero.
|
|
||||||
return tagName.replace(/:/g, "\\:");
|
|
||||||
}
|
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
||||||
export function getErrorMessage(ex: any) {
|
|
||||||
return ex.description ?? ex.message ?? ex;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function emptyMicroUser(): MicroUser {
|
|
||||||
return {
|
|
||||||
name: "",
|
|
||||||
avatarUrl: "",
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function emptyPost(): Post {
|
|
||||||
return {
|
|
||||||
id: 0,
|
|
||||||
version: 0,
|
|
||||||
creationTime: new Date(),
|
|
||||||
lastEditTime: new Date(),
|
|
||||||
safety: "safe",
|
|
||||||
source: "",
|
|
||||||
type: "",
|
|
||||||
mimeType: "",
|
|
||||||
checksum: "",
|
|
||||||
fileSize: 0,
|
|
||||||
canvasWidth: 0,
|
|
||||||
canvasHeight: 0,
|
|
||||||
contentUrl: "",
|
|
||||||
thumbnailUrl: "",
|
|
||||||
flags: [],
|
|
||||||
tags: [],
|
|
||||||
relations: [],
|
|
||||||
user: emptyMicroUser(),
|
|
||||||
score: 0,
|
|
||||||
ownScore: 0,
|
|
||||||
ownFavorite: false,
|
|
||||||
tagCount: 0,
|
|
||||||
favoriteCount: 0,
|
|
||||||
commentCount: 0,
|
|
||||||
noteCount: 0,
|
|
||||||
relationCount: 0,
|
|
||||||
featureCount: 0,
|
|
||||||
lastFeatureTime: new Date(),
|
|
||||||
favoritedBy: [],
|
|
||||||
hasCustomThumbnail: false,
|
|
||||||
notes: [],
|
|
||||||
comments: [],
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getTagClasses(tag: TagDetails): string[] {
|
|
||||||
const classes: string[] = [];
|
|
||||||
|
|
||||||
if (tag.category && tag.category != "default") {
|
|
||||||
classes.push("tag-" + tag.category);
|
|
||||||
} else {
|
|
||||||
classes.push("tag-general");
|
|
||||||
}
|
|
||||||
|
|
||||||
return classes;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function breakTagName(tagName: string) {
|
|
||||||
// Based on https://stackoverflow.com/a/6316913
|
|
||||||
return tagName.replace(/_/g, "_<wbr>");
|
|
||||||
}
|
|
||||||
|
|
||||||
export function resolutionToString(resolution: [number, number]) {
|
|
||||||
if (resolution && resolution.length == 2) {
|
|
||||||
return resolution[0] + "x" + resolution[1];
|
|
||||||
}
|
|
||||||
return "";
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getPostInfoSummary(post: ScrapedPostDetails) {
|
|
||||||
const parts = [];
|
|
||||||
if (post.contentSize) {
|
|
||||||
parts.push(byteSize(post.contentSize));
|
|
||||||
}
|
|
||||||
if (post.contentSubType) {
|
|
||||||
parts.push(post.contentSubType);
|
|
||||||
}
|
|
||||||
if (post.resolution) {
|
|
||||||
parts.push(resolutionToString(post.resolution));
|
|
||||||
}
|
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,4 @@
|
||||||
|
declare module "*.vue" {
|
||||||
|
import Vue from "vue";
|
||||||
|
export default Vue;
|
||||||
|
}
|
||||||
|
|
@ -1,22 +1,25 @@
|
||||||
{
|
{
|
||||||
"compilerOptions": {
|
"compilerOptions": {
|
||||||
"baseUrl": ".",
|
"outDir": "./dist/",
|
||||||
"module": "ESNext",
|
"sourceMap": true,
|
||||||
"target": "es2016",
|
|
||||||
"lib": ["DOM", "ESNext"],
|
|
||||||
"strict": true,
|
"strict": true,
|
||||||
"esModuleInterop": true,
|
"module": "es2015",
|
||||||
"incremental": false,
|
|
||||||
"skipLibCheck": true,
|
|
||||||
"jsx": "preserve",
|
|
||||||
"moduleResolution": "node",
|
"moduleResolution": "node",
|
||||||
"resolveJsonModule": true,
|
"target": "es5",
|
||||||
"noUnusedLocals": true,
|
"skipLibCheck": true,
|
||||||
"forceConsistentCasingInFileNames": true,
|
"esModuleInterop": true,
|
||||||
"types": ["vite/client"],
|
"experimentalDecorators": true,
|
||||||
"paths": {
|
"lib": [
|
||||||
"~/*": ["src/*"]
|
"es2017",
|
||||||
|
"dom"
|
||||||
|
],
|
||||||
|
"plugins": [
|
||||||
|
{
|
||||||
|
"transform": "ts-optchain/transform"
|
||||||
}
|
}
|
||||||
|
]
|
||||||
},
|
},
|
||||||
"exclude": ["dist", "node_modules"]
|
"include": [
|
||||||
|
"./src/**/*"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|
|
||||||
12
updates.json
|
|
@ -1,12 +0,0 @@
|
||||||
{
|
|
||||||
"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"
|
|
||||||
}
|
|
||||||
]
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
@ -1,33 +0,0 @@
|
||||||
import { defineConfig } from "vite";
|
|
||||||
import { sharedConfig } from "./vite.config";
|
|
||||||
import { isDev, r } from "./scripts/utils";
|
|
||||||
import packageJson from "./package.json";
|
|
||||||
|
|
||||||
// bundling the content script using Vite
|
|
||||||
export default defineConfig({
|
|
||||||
...sharedConfig,
|
|
||||||
define: {
|
|
||||||
__DEV__: isDev,
|
|
||||||
// https://github.com/vitejs/vite/issues/9320
|
|
||||||
// https://github.com/vitejs/vite/issues/9186
|
|
||||||
"process.env.NODE_ENV": JSON.stringify(isDev ? "development" : "production"),
|
|
||||||
},
|
|
||||||
build: {
|
|
||||||
watch: isDev ? {} : undefined,
|
|
||||||
outDir: r("extension/dist/contentScripts"),
|
|
||||||
cssCodeSplit: false,
|
|
||||||
emptyOutDir: false,
|
|
||||||
sourcemap: isDev ? "inline" : false,
|
|
||||||
lib: {
|
|
||||||
entry: r("src/contentScripts/index.ts"),
|
|
||||||
name: packageJson.name,
|
|
||||||
formats: ["iife"],
|
|
||||||
},
|
|
||||||
rollupOptions: {
|
|
||||||
output: {
|
|
||||||
entryFileNames: "index.global.js",
|
|
||||||
extend: true,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
@ -1,86 +0,0 @@
|
||||||
/// <reference types="vitest" />
|
|
||||||
|
|
||||||
import { dirname, relative } from "path";
|
|
||||||
import type { UserConfig } from "vite";
|
|
||||||
import { defineConfig } from "vite";
|
|
||||||
import Vue from "@vitejs/plugin-vue";
|
|
||||||
import AutoImport from "unplugin-auto-import/vite";
|
|
||||||
import Components from "unplugin-vue-components/vite";
|
|
||||||
import { isDev, port, r } from "./scripts/utils";
|
|
||||||
|
|
||||||
export const sharedConfig: UserConfig = {
|
|
||||||
root: r("src"),
|
|
||||||
resolve: {
|
|
||||||
alias: {
|
|
||||||
"~/": `${r("src")}/`,
|
|
||||||
},
|
|
||||||
},
|
|
||||||
define: {
|
|
||||||
__DEV__: isDev,
|
|
||||||
},
|
|
||||||
plugins: [
|
|
||||||
Vue(),
|
|
||||||
|
|
||||||
AutoImport({
|
|
||||||
imports: [
|
|
||||||
"vue",
|
|
||||||
{
|
|
||||||
"webextension-polyfill": [["*", "browser"]],
|
|
||||||
},
|
|
||||||
],
|
|
||||||
dts: r("src/auto-imports.d.ts"),
|
|
||||||
}),
|
|
||||||
|
|
||||||
Components({
|
|
||||||
dirs: [r("src/components")],
|
|
||||||
// generate `components.d.ts` for ts support with Volar
|
|
||||||
dts: r("src/components.d.ts"),
|
|
||||||
resolvers: [
|
|
||||||
// auto import icons
|
|
||||||
// IconsResolver({
|
|
||||||
// componentPrefix: "",
|
|
||||||
// }),
|
|
||||||
],
|
|
||||||
}),
|
|
||||||
|
|
||||||
// rewrite assets to use relative path
|
|
||||||
{
|
|
||||||
name: "assets-rewrite",
|
|
||||||
enforce: "post",
|
|
||||||
apply: "build",
|
|
||||||
transformIndexHtml(html, { path }) {
|
|
||||||
return html.replace(/"\/assets\//g, `"${relative(dirname(path), "/assets")}/`);
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
optimizeDeps: {
|
|
||||||
include: ["vue", "@vueuse/core", "webextension-polyfill"],
|
|
||||||
},
|
|
||||||
};
|
|
||||||
|
|
||||||
export default defineConfig(({ command }) => ({
|
|
||||||
...sharedConfig,
|
|
||||||
base: command === "serve" ? `http://localhost:${port}/` : "/dist/",
|
|
||||||
server: {
|
|
||||||
port,
|
|
||||||
hmr: {
|
|
||||||
host: "localhost",
|
|
||||||
},
|
|
||||||
},
|
|
||||||
build: {
|
|
||||||
outDir: r("extension/dist"),
|
|
||||||
emptyOutDir: false,
|
|
||||||
sourcemap: isDev ? "inline" : false,
|
|
||||||
rollupOptions: {
|
|
||||||
input: {
|
|
||||||
background: r("src/background/index.html"),
|
|
||||||
options: r("src/options/index.html"),
|
|
||||||
popup: r("src/popup/index.html"),
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
test: {
|
|
||||||
globals: true,
|
|
||||||
environment: "jsdom",
|
|
||||||
},
|
|
||||||
}));
|
|
||||||
|
|
@ -0,0 +1,119 @@
|
||||||
|
const webpack = require('webpack');
|
||||||
|
const ejs = require('ejs');
|
||||||
|
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
|
||||||
|
const CopyWebpackPlugin = require('copy-webpack-plugin');
|
||||||
|
const ExtensionReloader = require('webpack-extension-reloader');
|
||||||
|
const { VueLoaderPlugin } = require('vue-loader');
|
||||||
|
const { version } = require('./package.json');
|
||||||
|
|
||||||
|
const config = {
|
||||||
|
mode: process.env.NODE_ENV,
|
||||||
|
context: __dirname + '/src',
|
||||||
|
entry: {
|
||||||
|
'scripts/content': './scripts/content.ts',
|
||||||
|
'popup/popup': './popup/popup.ts',
|
||||||
|
'options/options': './options/options.ts',
|
||||||
|
},
|
||||||
|
output: {
|
||||||
|
path: __dirname + '/dist',
|
||||||
|
filename: '[name].js',
|
||||||
|
},
|
||||||
|
resolve: {
|
||||||
|
extensions: ['.ts', '.js', '.vue'],
|
||||||
|
},
|
||||||
|
devtool: 'inline-source-map',
|
||||||
|
module: {
|
||||||
|
rules: [
|
||||||
|
{
|
||||||
|
test: /\.vue$/,
|
||||||
|
loaders: 'vue-loader',
|
||||||
|
},
|
||||||
|
{
|
||||||
|
test: /\.tsx?$/,
|
||||||
|
loader: 'ts-loader',
|
||||||
|
exclude: /node_modules/,
|
||||||
|
options: {
|
||||||
|
appendTsSuffixTo: [/\.vue$/],
|
||||||
|
}
|
||||||
|
},
|
||||||
|
{
|
||||||
|
test: /\.css$/,
|
||||||
|
use: [MiniCssExtractPlugin.loader, 'css-loader'],
|
||||||
|
},
|
||||||
|
{
|
||||||
|
test: /\.(png|jpg|gif|svg|ico)$/,
|
||||||
|
loader: 'file-loader',
|
||||||
|
options: {
|
||||||
|
name: '[name].[ext]?emitFile=false',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
test: /\.(woff(2)?|ttf|eot|svg)(\?v=\d+\.\d+\.\d+)?$/,
|
||||||
|
loader: 'file-loader',
|
||||||
|
options: {
|
||||||
|
name: '[name].[ext]?emitFile=false',
|
||||||
|
outputPath: '/fonts/'
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
plugins: [
|
||||||
|
new webpack.DefinePlugin({
|
||||||
|
global: 'window',
|
||||||
|
}),
|
||||||
|
new VueLoaderPlugin(),
|
||||||
|
new MiniCssExtractPlugin({
|
||||||
|
filename: '[name].css',
|
||||||
|
}),
|
||||||
|
new CopyWebpackPlugin([
|
||||||
|
{ from: 'icons', to: 'icons', ignore: ['icon.xcf'] },
|
||||||
|
{ from: 'popup/popup.html', to: 'popup/popup.html', transform: transformHtml },
|
||||||
|
{ from: 'options/options.html', to: 'options/options.html', transform: transformHtml },
|
||||||
|
{
|
||||||
|
from: 'manifest.json',
|
||||||
|
to: 'manifest.json',
|
||||||
|
transform: (content) => {
|
||||||
|
const jsonContent = JSON.parse(content);
|
||||||
|
jsonContent.version = version;
|
||||||
|
|
||||||
|
if (config.mode === 'development') {
|
||||||
|
jsonContent['content_security_policy'] = "script-src 'self' 'unsafe-eval'; object-src 'self'";
|
||||||
|
}
|
||||||
|
|
||||||
|
return JSON.stringify(jsonContent, null, 2);
|
||||||
|
},
|
||||||
|
},
|
||||||
|
]),
|
||||||
|
]
|
||||||
|
};
|
||||||
|
|
||||||
|
if (config.mode === 'production') {
|
||||||
|
config.plugins = (config.plugins || []).concat([
|
||||||
|
new webpack.DefinePlugin({
|
||||||
|
'process.env': {
|
||||||
|
NODE_ENV: '"production"',
|
||||||
|
},
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (process.env.HMR === 'true') {
|
||||||
|
config.entry['scripts/background'] = './scripts/background.ts';
|
||||||
|
|
||||||
|
config.plugins = (config.plugins || []).concat([
|
||||||
|
new ExtensionReloader({
|
||||||
|
entries: {
|
||||||
|
contentScript: 'scripts/content',
|
||||||
|
background: 'scripts/background'
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
|
||||||
|
function transformHtml(content) {
|
||||||
|
return ejs.render(content.toString(), {
|
||||||
|
...process.env,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = config;
|
||||||