Import posts in the background (#9)
parent
af83ce454e
commit
864523392e
File diff suppressed because it is too large
Load Diff
30
package.json
30
package.json
|
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "szuru-chrome",
|
||||
"version": "0.2.4",
|
||||
"version": "0.3.0",
|
||||
"description": "Import media from various sites into szurubooru",
|
||||
"author": "neobooru",
|
||||
"license": "MIT",
|
||||
|
|
@ -12,31 +12,31 @@
|
|||
"watch:dev": "cross-env HMR=true npm run build:dev -- --watch"
|
||||
},
|
||||
"dependencies": {
|
||||
"axios": "^0.19.0",
|
||||
"axios": "^0.19.2",
|
||||
"neo-scraper": "^0.2.3",
|
||||
"ts-optchain": "^0.1.8",
|
||||
"vue": "^2.6.10",
|
||||
"vue": "^2.6.11",
|
||||
"webextension-polyfill": "^0.3.1"
|
||||
},
|
||||
"devDependencies": {
|
||||
"archiver": "^3.1.1",
|
||||
"copy-webpack-plugin": "^5.1.1",
|
||||
"core-js": "^3.0.1",
|
||||
"cross-env": "^5.2.0",
|
||||
"core-js": "^3.6.5",
|
||||
"cross-env": "^5.2.1",
|
||||
"css-loader": "^2.1.1",
|
||||
"ejs": "^2.6.1",
|
||||
"ejs": "^2.7.4",
|
||||
"file-loader": "^1.1.11",
|
||||
"mini-css-extract-plugin": "^0.4.4",
|
||||
"node-sass": "^4.13.1",
|
||||
"sass-loader": "^7.1.0",
|
||||
"ts-loader": "^6.0.4",
|
||||
"typescript": "^3.5.3",
|
||||
"vue-loader": "^15.4.2",
|
||||
"vue-template-compiler": "^2.6.10",
|
||||
"node-sass": "^4.14.1",
|
||||
"sass-loader": "^7.3.1",
|
||||
"ts-loader": "^6.2.2",
|
||||
"typescript": "^3.8.3",
|
||||
"vue-loader": "^15.9.2",
|
||||
"vue-template-compiler": "^2.6.11",
|
||||
"web-ext-types": "^2.1.0",
|
||||
"webextension-polyfill-ts": "^0.9.1",
|
||||
"webpack": "^4.39.1",
|
||||
"webpack-cli": "^3.1.2",
|
||||
"webpack-extension-reloader": "^1.1.0"
|
||||
"webpack": "^4.43.0",
|
||||
"webpack-cli": "^3.3.11",
|
||||
"webpack-extension-reloader": "^1.1.4"
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,24 @@
|
|||
export type BrowserCommandName = "grab_post" | "upload_post" | "push_message" | "remove_messages";
|
||||
export type MessageType = "error" | "info" | "success";
|
||||
|
||||
export class BrowserCommand {
|
||||
name: BrowserCommandName;
|
||||
data: any;
|
||||
|
||||
constructor(name: BrowserCommandName, data: any = null) {
|
||||
this.name = name;
|
||||
this.data = data;
|
||||
}
|
||||
}
|
||||
|
||||
export class Message {
|
||||
content: string;
|
||||
level: MessageType;
|
||||
category: string;
|
||||
|
||||
constructor(content: string, level: MessageType = "info", category: string | null = null) {
|
||||
this.content = content;
|
||||
this.level = level;
|
||||
this.category = category ?? "none";
|
||||
}
|
||||
}
|
||||
|
|
@ -23,7 +23,7 @@ button.primary {
|
|||
input[type="text"] {
|
||||
background-color: #fafafa;
|
||||
border: 1px solid #eee;
|
||||
height: 30px;
|
||||
height: 26px;
|
||||
width: 100%;
|
||||
}
|
||||
|
||||
|
|
@ -41,7 +41,6 @@ select {
|
|||
border: 1px solid #eee;
|
||||
height: 30px;
|
||||
width: 100%;
|
||||
font-family: "Open Sans", sans-serif;
|
||||
}
|
||||
|
||||
.compact-tags {
|
||||
|
|
|
|||
|
|
@ -26,9 +26,15 @@
|
|||
]
|
||||
}
|
||||
],
|
||||
"background": {
|
||||
"scripts": [
|
||||
"scripts/background.js"
|
||||
],
|
||||
"persistent": false
|
||||
},
|
||||
"browser_specific_settings": {
|
||||
"gecko": {
|
||||
"id": "szuruchrome@neobooru.com"
|
||||
"id": "{8cc2afc8-6c4a-4d2b-9513-46e46e5b4208}"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,7 +1,6 @@
|
|||
<template>
|
||||
<div class="popup-container">
|
||||
<div class="popup-row">
|
||||
<div class="popup-column1" style="margin: 0px 10px;">
|
||||
<div class="popup-messages">
|
||||
<ul class="messages">
|
||||
<li
|
||||
v-for="msg in messages"
|
||||
|
|
@ -11,50 +10,48 @@
|
|||
></li>
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="popup-row">
|
||||
<div class="popup-column2">
|
||||
<div class="popup-header">
|
||||
<div class="popup-left">
|
||||
<div class="popup-section">
|
||||
<div class="section-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>
|
||||
<div class="section-row">
|
||||
<span class="section-label">Safety</span>
|
||||
|
||||
<label class="container">
|
||||
<input type="radio" value="safe" v-model="post.rating" />
|
||||
<input type="radio" value="safe" v-model="selectedPost.rating" />
|
||||
<span class="checkmark"></span>
|
||||
Safe
|
||||
</label>
|
||||
|
||||
<label class="container">
|
||||
<input type="radio" value="sketchy" v-model="post.rating" />
|
||||
<input type="radio" value="sketchy" v-model="selectedPost.rating" />
|
||||
<span class="checkmark"></span>
|
||||
Sketchy
|
||||
</label>
|
||||
|
||||
<label class="container">
|
||||
<input type="radio" value="unsafe" v-model="post.rating" />
|
||||
<input type="radio" value="unsafe" v-model="selectedPost.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 class="section-row">
|
||||
<span class="section-label">Source</span>
|
||||
<input type="text" v-model="selectedPost.source" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="popup-header">
|
||||
<div class="popup-section">
|
||||
<div class="section-header">
|
||||
<span>Tags</span>
|
||||
</div>
|
||||
|
||||
<div class="popup-content">
|
||||
<div class="popup-block" style="display:flex;">
|
||||
<div class="section-row" style="display:flex;">
|
||||
<input type="text" v-model="addTagInput" v-on:keyup.enter="addTag" />
|
||||
<button
|
||||
class="primary"
|
||||
|
|
@ -63,9 +60,9 @@
|
|||
>Add</button>
|
||||
</div>
|
||||
|
||||
<div class="popup-block">
|
||||
<div class="section-row">
|
||||
<ul class="compact-tags">
|
||||
<li v-for="tag in post.tags" :key="tag.name">
|
||||
<li v-for="tag in selectedPost.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>
|
||||
|
|
@ -73,141 +70,114 @@
|
|||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="popup-column2">
|
||||
<img :src="post.imageUrl" />
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="popup-row">
|
||||
<div class="popup-column2">
|
||||
<div class="popup-section">
|
||||
<div class="section-row">
|
||||
<select disabled>
|
||||
<option>{{ activeSite != null ? activeSite.domain : "(no site available)" }}</option>
|
||||
</select>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div class="popup-column2" style="display: flex;">
|
||||
<div class="popup-right">
|
||||
<div class="popup-section">
|
||||
<select v-model="selectedPost">
|
||||
<option
|
||||
v-for="post in posts"
|
||||
v-bind:key="post"
|
||||
v-bind:value="post"
|
||||
>{{ post.name }}</option>
|
||||
</select>
|
||||
</div>
|
||||
|
||||
<div class="popup-section">
|
||||
<img :src="selectedPost.imageUrl" />
|
||||
|
||||
<div 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>
|
||||
|
||||
<script lang="ts">
|
||||
import Vue from "vue";
|
||||
import { browser } from "webextension-polyfill-ts";
|
||||
import { browser, Runtime } 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 { BrowserCommand, Message } from "../Common";
|
||||
|
||||
type MessageType = "error" | "info" | "success";
|
||||
|
||||
class Message {
|
||||
content: string;
|
||||
type: MessageType;
|
||||
|
||||
constructor(content: string, type: MessageType = "info") {
|
||||
this.content = content;
|
||||
this.type = type;
|
||||
}
|
||||
class ScrapedPostViewModel extends ScrapedPost {
|
||||
name: string = "";
|
||||
}
|
||||
|
||||
export default Vue.extend({
|
||||
data() {
|
||||
return {
|
||||
config: null as Config | null,
|
||||
activeSite: null as SzuruSiteConfig | null,
|
||||
szuru: null as SzuruWrapper | null,
|
||||
post: new ScrapedPost(),
|
||||
posts: [] as ScrapedPostViewModel[],
|
||||
selectedPost: new ScrapedPost(), // Shouldn't be null because VueJS gets a mental breakdown when it sees a null.
|
||||
messages: [] as Message[],
|
||||
addTagInput: ""
|
||||
};
|
||||
},
|
||||
watch: {
|
||||
selectedPost() {
|
||||
if (this.config && this.config.autoSearchSimilar) {
|
||||
// No need to check if any vars are unset, findSimilar does that internally
|
||||
this.findSimilar();
|
||||
}
|
||||
}
|
||||
},
|
||||
methods: {
|
||||
// Try to scrape the post from the page
|
||||
async grabPost() {
|
||||
// Get active tab
|
||||
const activeTab = (await browser.tabs.query({
|
||||
const activeTab = (
|
||||
await browser.tabs.query({
|
||||
active: true,
|
||||
currentWindow: true
|
||||
}))[0];
|
||||
})
|
||||
)[0];
|
||||
|
||||
// Send 'grab_post' to the content script on the active tab
|
||||
const x = await browser.tabs.sendMessage(activeTab.id!, "grab_post");
|
||||
const x = await browser.tabs.sendMessage(activeTab.id!, new BrowserCommand("grab_post"));
|
||||
// We need to create a new ScrapeResults object and fill it with our data, because the get_posts()
|
||||
// method gets 'lost' when sent from the contentscript to the popup (it gets JSON.stringified and any prototype defines are lost there)
|
||||
const res = Object.assign(new ScrapeResults(), x);
|
||||
const post = res.posts.length > 0 ? res.posts[0] : null;
|
||||
|
||||
if (post) {
|
||||
this.post = post;
|
||||
console.dir(post);
|
||||
// Clear current posts array
|
||||
this.posts = [];
|
||||
|
||||
for (var result of res.results) {
|
||||
for (var i in result.posts) {
|
||||
const vm = Object.assign(new ScrapedPostViewModel(), result.posts[i]);
|
||||
vm.name = `[${result.engine}] Post ${parseInt(i) + 1}`; // parseInt() is required!
|
||||
this.posts.push(vm);
|
||||
}
|
||||
}
|
||||
|
||||
if (this.posts.length > 0) {
|
||||
this.selectedPost = this.posts[0];
|
||||
} else {
|
||||
this.pushError("Couldn't grab post");
|
||||
this.pushInfo("No posts found.");
|
||||
}
|
||||
},
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
// Outsource to background.ts
|
||||
const cmd = new BrowserCommand("upload_post", this.selectedPost);
|
||||
await browser.runtime.sendMessage(cmd);
|
||||
// TODO: Handle errors/status update
|
||||
},
|
||||
// Open extension settings page in new tab
|
||||
async openSettings() {
|
||||
|
|
@ -227,17 +197,17 @@ export default Vue.extend({
|
|||
},
|
||||
// Remove tag from post's taglist
|
||||
removeTag(tag: ScrapedTag) {
|
||||
if (this.post) {
|
||||
const idx = this.post.tags.indexOf(tag);
|
||||
if (this.selectedPost) {
|
||||
const idx = this.selectedPost.tags.indexOf(tag);
|
||||
if (idx != -1) {
|
||||
this.post.tags.splice(idx, 1);
|
||||
this.selectedPost.tags.splice(idx, 1);
|
||||
}
|
||||
}
|
||||
},
|
||||
getMessageClasses(message: Message) {
|
||||
let classes: string[] = [];
|
||||
|
||||
switch (message.type) {
|
||||
switch (message.level) {
|
||||
case "error":
|
||||
classes.push("message-error");
|
||||
break;
|
||||
|
|
@ -249,75 +219,64 @@ export default Vue.extend({
|
|||
return classes;
|
||||
},
|
||||
// Add info message
|
||||
pushInfo(message: string): Message {
|
||||
let msg = new Message(message);
|
||||
pushInfo(message: string, category: string | null = null): Message {
|
||||
let msg = new Message(message, "info", category);
|
||||
this.messages.push(msg);
|
||||
return msg;
|
||||
},
|
||||
// Add error message
|
||||
pushError(message: string): Message {
|
||||
let msg = new Message(message, "error");
|
||||
pushError(message: string, category: string | null = null): Message {
|
||||
let msg = new Message(message, "error", category);
|
||||
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.
|
||||
// Clear messages of given category. When category is empty clear all messages.
|
||||
clearMessages(category: string | null) {
|
||||
if (category) {
|
||||
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
|
||||
) {
|
||||
if (this.messages[i].category == category) {
|
||||
// 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);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
this.messages = [];
|
||||
}
|
||||
},
|
||||
getPostUrl(post: Post): string {
|
||||
if (!this.activeSite) return "";
|
||||
return this.activeSite.domain + "/post/" + post.id;
|
||||
},
|
||||
// Add tag to the post's taglist
|
||||
addTag() {
|
||||
if (this.selectedPost) {
|
||||
const tagName = this.addTagInput;
|
||||
this.addTagInput = "";
|
||||
if (this.selectedPost.tags.find(x => x.name == tagName) == undefined) {
|
||||
this.selectedPost.tags.push(new ScrapedTag(tagName));
|
||||
}
|
||||
}
|
||||
},
|
||||
// Find posts similar to the grabbed post (this.selectedPost)
|
||||
async findSimilar() {
|
||||
if (!this.selectedPost || !this.szuru) {
|
||||
return;
|
||||
}
|
||||
|
||||
const msgSearchSimilar = this.pushInfo("Searching for similar posts...");
|
||||
const res = await this.szuru.reverseSearch(this.post.imageUrl);
|
||||
this.removeMessage(msgSearchSimilar);
|
||||
this.clearMessages("FIND_SIMILAR");
|
||||
|
||||
const msg = this.pushInfo("Searching for similar posts...", "FIND_SIMILAR");
|
||||
const res = await this.szuru.reverseSearch(this.selectedPost.imageUrl);
|
||||
|
||||
if (!res.exactPost && res.similarPosts.length == 0) {
|
||||
this.pushInfo("No similar posts found");
|
||||
msg.content = "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>`
|
||||
);
|
||||
msg.content = `<a href='${this.getPostUrl(res.exactPost)}' target='_blank'>
|
||||
Post already uploaded (${res.exactPost.id})</a>`;
|
||||
msg.level = "error";
|
||||
}
|
||||
|
||||
for (const similarPost of res.similarPosts) {
|
||||
|
|
@ -328,15 +287,16 @@ export default Vue.extend({
|
|||
|
||||
this.pushInfo(
|
||||
`<a href='${this.getPostUrl(similarPost.post)}' target='_blank'>Post ${similarPost.post.id}
|
||||
looks ${Math.round(100 - similarPost.distance * 100)}% similar</a>`
|
||||
looks ${Math.round(100 - similarPost.distance * 100)}% similar</a>`,
|
||||
"FIND_SIMILAR"
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
async mounted() {
|
||||
const cfg = await Config.load();
|
||||
this.activeSite = cfg.sites.length > 0 ? cfg.sites[0] : null;
|
||||
this.config = await Config.load();
|
||||
this.activeSite = this.config.sites.length > 0 ? this.config.sites[0] : null;
|
||||
|
||||
if (!this.activeSite) {
|
||||
this.pushError("No szurubooru server configured!");
|
||||
|
|
@ -344,13 +304,22 @@ export default Vue.extend({
|
|||
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();
|
||||
browser.runtime.onMessage.addListener((cmd: BrowserCommand, sender: Runtime.MessageSender) => {
|
||||
switch (cmd.name) {
|
||||
case "push_message":
|
||||
this.messages.push(cmd.data);
|
||||
break;
|
||||
case "remove_messages":
|
||||
for (let i = 0; i < cmd.data; i++) {
|
||||
if (this.messages.length == 0) break;
|
||||
this.messages.pop();
|
||||
}
|
||||
break;
|
||||
}
|
||||
});
|
||||
|
||||
// Always call grabPost, even when there is no activeSite
|
||||
this.grabPost();
|
||||
}
|
||||
});
|
||||
</script>
|
||||
|
|
@ -358,49 +327,47 @@ export default Vue.extend({
|
|||
<style scoped>
|
||||
.popup-container {
|
||||
width: 700px;
|
||||
display: grid;
|
||||
grid-template-columns: 300px 400px;
|
||||
}
|
||||
|
||||
.popup-row {
|
||||
display: flex;
|
||||
.popup-messages {
|
||||
grid-column: 1 / 3;
|
||||
margin: 0 10px;
|
||||
}
|
||||
|
||||
.popup-column1 {
|
||||
flex: 100%;
|
||||
.popup-left {
|
||||
grid-column: 1;
|
||||
}
|
||||
|
||||
.popup-right {
|
||||
grid-column: 2;
|
||||
}
|
||||
|
||||
.popup-section {
|
||||
margin: 10px;
|
||||
}
|
||||
|
||||
.popup-column2 {
|
||||
flex: 50%;
|
||||
margin: 10px;
|
||||
}
|
||||
|
||||
.popup-header {
|
||||
.section-header {
|
||||
height: 30px;
|
||||
padding: 0 0 0 10px;
|
||||
padding: 0 6px 0 10px;
|
||||
background-color: #eee;
|
||||
margin: 0 0 10px 0;
|
||||
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
}
|
||||
|
||||
.popup-content {
|
||||
margin: 5px;
|
||||
}
|
||||
|
||||
.popup-header > span {
|
||||
.section-header > span {
|
||||
font-size: 1.2em;
|
||||
vertical-align: middle;
|
||||
}
|
||||
|
||||
/* Horrible hack + guesswork */
|
||||
.popup-header > i {
|
||||
float: right;
|
||||
margin: 8px 5px;
|
||||
cursor: pointer;
|
||||
.section-row {
|
||||
margin: 10px 5px 0;
|
||||
}
|
||||
|
||||
.popup-block {
|
||||
margin: 0 0 10px 0;
|
||||
}
|
||||
|
||||
.block-title {
|
||||
.section-label {
|
||||
display: block;
|
||||
margin: 0 0 5px 0;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,2 +1,72 @@
|
|||
// 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
|
||||
import { ScrapedPost } from "neo-scraper";
|
||||
import { browser, Runtime } from "webextension-polyfill-ts";
|
||||
import { BrowserCommand, Message } from "../Common";
|
||||
import { Config } from "../Config.ts";
|
||||
import SzuruWrapper from "../SzuruWrapper.ts";
|
||||
import { SzuruError } from "../SzuruTypes";
|
||||
|
||||
async function uploadPost(post: ScrapedPost) {
|
||||
// TODO: Upload post and send status updates to popup
|
||||
console.dir(post);
|
||||
|
||||
const cfg = await Config.load();
|
||||
const szuru = (await SzuruWrapper.createFromConfig(cfg.sites[0]))!;
|
||||
|
||||
try {
|
||||
// Create and upload post
|
||||
browser.runtime.sendMessage(new BrowserCommand("remove_messages", 10));
|
||||
browser.runtime.sendMessage(new BrowserCommand("push_message", new Message("Uploading...")));
|
||||
const createdPost = await szuru.createPost(post);
|
||||
browser.runtime.sendMessage(new BrowserCommand("remove_messages", 1));
|
||||
browser.runtime.sendMessage(new BrowserCommand("push_message", new Message("Uploaded post " + createdPost.id, "success")));
|
||||
|
||||
// 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 = 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) {
|
||||
browser.runtime.sendMessage(new BrowserCommand("push_message", new Message(`${unsetCategoryTags.length} tags need a different category`)));
|
||||
// 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 => encodeTagName(x.names[0]));
|
||||
const tags = (await szuru.getTags(query)).results;
|
||||
for (let i in tags) {
|
||||
browser.runtime.sendMessage(new BrowserCommand("remove_messages", 1));
|
||||
browser.runtime.sendMessage(new BrowserCommand("push_message", new Message(`Updating tag ${i}/${unsetCategoryTags.length}`)));
|
||||
tags[i].category = tagsWithCategory.find(x => tags[i].names.includes(x.name))!.category!;
|
||||
await szuru.updateTag(tags[i]);
|
||||
}
|
||||
browser.runtime.sendMessage(new BrowserCommand("remove_messages", 1));
|
||||
browser.runtime.sendMessage(new BrowserCommand("push_message", new Message(`Updated ${tags.length} tags`, "success")));
|
||||
}
|
||||
} catch (ex) {
|
||||
const error = ex as SzuruError;
|
||||
if (error) {
|
||||
console.error(error);
|
||||
browser.runtime.sendMessage(new BrowserCommand("push_message", new Message(ex, "error")));
|
||||
} else {
|
||||
console.error(ex);
|
||||
browser.runtime.sendMessage(new BrowserCommand("push_message", new Message(ex, "error")));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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 encodeURIComponent(tagName.replace(/\:/g, "\\:"));
|
||||
}
|
||||
|
||||
async function messageHandler(cmd: BrowserCommand, sender: Runtime.MessageSender): Promise<any> {
|
||||
switch (cmd.name) {
|
||||
case "upload_post": return uploadPost(cmd.data);
|
||||
}
|
||||
}
|
||||
|
||||
browser.runtime.onMessage.addListener(messageHandler);
|
||||
|
|
|
|||
|
|
@ -1,13 +1,14 @@
|
|||
import { NeoScraper, ScrapeResults } from "neo-scraper";
|
||||
import { browser, Runtime } from "webextension-polyfill-ts";
|
||||
import { BrowserCommand } from "../Common";
|
||||
|
||||
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) {
|
||||
async function messageHandler(cmd: BrowserCommand, sender: Runtime.MessageSender): Promise<any> {
|
||||
switch (cmd.name) {
|
||||
case "grab_post": return grabPost();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -5,7 +5,7 @@
|
|||
"strict": true,
|
||||
"module": "es2015",
|
||||
"moduleResolution": "node",
|
||||
"target": "es5",
|
||||
"target": "es6",
|
||||
"skipLibCheck": true,
|
||||
"esModuleInterop": true,
|
||||
"experimentalDecorators": true,
|
||||
|
|
|
|||
|
|
@ -11,6 +11,7 @@ const config = {
|
|||
context: __dirname + '/src',
|
||||
entry: {
|
||||
'scripts/content': './scripts/content.ts',
|
||||
'scripts/background': './scripts/background.ts',
|
||||
'popup/popup': './popup/popup.ts',
|
||||
'options/options': './options/options.ts',
|
||||
},
|
||||
|
|
@ -98,8 +99,6 @@ if (config.mode === 'production') {
|
|||
}
|
||||
|
||||
if (process.env.HMR === 'true') {
|
||||
config.entry['scripts/background'] = './scripts/background.ts';
|
||||
|
||||
config.plugins = (config.plugins || []).concat([
|
||||
new ExtensionReloader({
|
||||
entries: {
|
||||
|
|
|
|||
Loading…
Reference in New Issue