Add crappy messaging support

pull/9/head
neobooru 2020-05-12 18:54:57 +02:00
parent 535b83fc68
commit bacef37943
4 changed files with 29 additions and 9 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "szuru-chrome", "name": "szuru-chrome",
"version": "0.2.4", "version": "0.3.0",
"description": "Import media from various sites into szurubooru", "description": "Import media from various sites into szurubooru",
"author": "neobooru", "author": "neobooru",
"license": "MIT", "license": "MIT",

View File

@ -1,4 +1,4 @@
export type BrowserCommandName = "grab_post" | "upload_post"; export type BrowserCommandName = "grab_post" | "upload_post" | "push_message" | "remove_messages";
export type MessageType = "error" | "info" | "success"; export type MessageType = "error" | "info" | "success";
export class BrowserCommand { export class BrowserCommand {

View File

@ -105,7 +105,7 @@
<script lang="ts"> <script lang="ts">
import Vue from "vue"; 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 { ScrapedPost, ScrapedTag, ScrapeResults } from "neo-scraper";
import SzuruWrapper from "../SzuruWrapper"; import SzuruWrapper from "../SzuruWrapper";
import { Post, SzuruError } from "../SzuruTypes"; import { Post, SzuruError } from "../SzuruTypes";
@ -304,6 +304,20 @@ export default Vue.extend({
this.szuru = await SzuruWrapper.createFromConfig(this.activeSite); this.szuru = await SzuruWrapper.createFromConfig(this.activeSite);
} }
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 // Always call grabPost, even when there is no activeSite
this.grabPost(); this.grabPost();
} }

View File

@ -1,6 +1,6 @@
import { ScrapedPost } from "neo-scraper"; import { ScrapedPost } from "neo-scraper";
import { browser, Runtime } from "webextension-polyfill-ts"; import { browser, Runtime } from "webextension-polyfill-ts";
import { BrowserCommand } from "../Common"; import { BrowserCommand, Message } from "../Common";
import { Config } from "../Config.ts"; import { Config } from "../Config.ts";
import SzuruWrapper from "../SzuruWrapper.ts"; import SzuruWrapper from "../SzuruWrapper.ts";
import { SzuruError } from "../SzuruTypes"; import { SzuruError } from "../SzuruTypes";
@ -14,9 +14,11 @@ async function uploadPost(post: ScrapedPost) {
try { try {
// Create and upload post // Create and upload post
console.log("Uploading..."); browser.runtime.sendMessage(new BrowserCommand("remove_messages", 10));
browser.runtime.sendMessage(new BrowserCommand("push_message", new Message("Uploading...")));
const createdPost = await szuru.createPost(post); const createdPost = await szuru.createPost(post);
console.log("Uploaded post " + createdPost.id); 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, // TODO: Clicking a link doesn't actually open it in a new tab,
// see https://stackoverflow.com/questions/8915845 // see https://stackoverflow.com/questions/8915845
@ -30,23 +32,27 @@ async function uploadPost(post: ScrapedPost) {
.filter(x => x.category == "default") .filter(x => x.category == "default")
.filter(x => tagsWithCategory.some(y => x.names.includes(y.name))); .filter(x => tagsWithCategory.some(y => x.names.includes(y.name)));
if (unsetCategoryTags.length != 0) { if (unsetCategoryTags.length != 0) {
console.log(`${unsetCategoryTags.length} tags need a different category`); 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 // 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 query = "?query=" + unsetCategoryTags.map(x => encodeTagName(x.names[0]));
const tags = (await szuru.getTags(query)).results; const tags = (await szuru.getTags(query)).results;
for (let i in tags) { for (let i in tags) {
console.log(`Updating tag ${i}/${unsetCategoryTags.length}`); 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!; tags[i].category = tagsWithCategory.find(x => tags[i].names.includes(x.name))!.category!;
await szuru.updateTag(tags[i]); await szuru.updateTag(tags[i]);
} }
console.log(`Updated ${tags.length} tags`); browser.runtime.sendMessage(new BrowserCommand("remove_messages", 1));
browser.runtime.sendMessage(new BrowserCommand("push_message", new Message(`Updated ${tags.length} tags`, "success")));
} }
} catch (ex) { } catch (ex) {
const error = ex as SzuruError; const error = ex as SzuruError;
if (error) { if (error) {
console.error(error); console.error(error);
browser.runtime.sendMessage(new BrowserCommand("push_message", new Message(ex, "error")));
} else { } else {
console.error(ex); console.error(ex);
browser.runtime.sendMessage(new BrowserCommand("push_message", new Message(ex, "error")));
} }
} }
} }