pull/9/head
neobooru 2020-03-21 01:12:15 +01:00
parent 5908a5cad5
commit f0259f84d3
6 changed files with 85 additions and 57 deletions

View File

@ -23,7 +23,7 @@ button.primary {
input[type="text"] { input[type="text"] {
background-color: #fafafa; background-color: #fafafa;
border: 1px solid #eee; border: 1px solid #eee;
height: 30px; height: 26px;
width: 100%; width: 100%;
} }

View File

@ -26,6 +26,12 @@
] ]
} }
], ],
"background": {
"scripts": [
"scripts/background.js"
],
"persistent": false
},
"browser_specific_settings": { "browser_specific_settings": {
"gecko": { "gecko": {
"id": "{8cc2afc8-6c4a-4d2b-9513-46e46e5b4208}" "id": "{8cc2afc8-6c4a-4d2b-9513-46e46e5b4208}"

View File

@ -81,6 +81,14 @@
</div> </div>
<div class="popup-right"> <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"> <div class="popup-section">
<img :src="selectedPost.imageUrl" /> <img :src="selectedPost.imageUrl" />
@ -106,27 +114,43 @@ type MessageType = "error" | "info" | "success";
class Message { class Message {
readonly id: number; readonly id: number;
content: string; content: string;
type: MessageType; level: MessageType;
category: string;
private static ids = 0; private static ids = 0;
constructor(content: string, type: MessageType = "info") { constructor(content: string, level: MessageType = "info", category: string | null = null) {
this.id = Message.ids++; this.id = Message.ids++;
this.content = content; this.content = content;
this.type = type; this.level = level;
this.category = category ?? "none";
} }
} }
class ScrapedPostViewModel extends ScrapedPost {
name: string = "";
}
export default Vue.extend({ export default Vue.extend({
data() { data() {
return { return {
config: null as Config | null,
activeSite: null as SzuruSiteConfig | null, activeSite: null as SzuruSiteConfig | null,
szuru: null as SzuruWrapper | null, szuru: null as SzuruWrapper | null,
selectedPost: 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[], messages: [] as Message[],
addTagInput: "" 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: { methods: {
// Try to scrape the post from the page // Try to scrape the post from the page
async grabPost() { async grabPost() {
@ -143,17 +167,28 @@ export default Vue.extend({
// We need to create a new ScrapeResults object and fill it with our data, because the get_posts() // 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) // 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 res = Object.assign(new ScrapeResults(), x);
const post = res.posts.length > 0 ? res.posts[0] : null;
if (post) { // Clear current posts array
this.selectedPost = post; this.posts = [];
console.dir(post);
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 { } else {
this.pushError("Couldn't grab post"); this.pushInfo("No posts found.");
} }
}, },
// Try to upload the post to the selected szurubooru instance // Try to upload the post to the selected szurubooru instance
async upload() {}, async upload() {
// Outsource to background.ts
},
// Open extension settings page in new tab // Open extension settings page in new tab
async openSettings() { async openSettings() {
const url = browser.extension.getURL("options/options.html"); const url = browser.extension.getURL("options/options.html");
@ -182,7 +217,7 @@ export default Vue.extend({
getMessageClasses(message: Message) { getMessageClasses(message: Message) {
let classes: string[] = []; let classes: string[] = [];
switch (message.type) { switch (message.level) {
case "error": case "error":
classes.push("message-error"); classes.push("message-error");
break; break;
@ -194,23 +229,31 @@ export default Vue.extend({
return classes; return classes;
}, },
// Add info message // Add info message
pushInfo(message: string): Message { pushInfo(message: string, category: string | null = null): Message {
let msg = new Message(message); let msg = new Message(message, "info", category);
this.messages.push(msg); this.messages.push(msg);
return msg; return msg;
}, },
// Add error message // Add error message
pushError(message: string): Message { pushError(message: string, category: string | null = null): Message {
let msg = new Message(message, "error"); let msg = new Message(message, "error", category);
this.messages.push(msg); this.messages.push(msg);
return msg; return msg;
}, },
clearMessages() { // Clear messages of given category. When category is empty clear all messages.
this.messages = []; clearMessages(category: string | null) {
}, if (category) {
removeMessage(message: Message) { for (let i = 0; i < this.messages.length; i++) {
const idx = this.messages.indexOf(message); if (this.messages[i].category == category) {
if (idx != -1) this.messages.splice(idx, 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);
}
}
} else {
this.messages = [];
}
}, },
getPostUrl(post: Post): string { getPostUrl(post: Post): string {
if (!this.activeSite) return ""; if (!this.activeSite) return "";
@ -237,32 +280,18 @@ export default Vue.extend({
return; return;
} }
// Remove messages that contain "Post already uploaded" or "No similar posts found" this.clearMessages("FIND_SIMILAR");
// 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 msg = this.pushInfo("Searching for similar posts...", "FIND_SIMILAR");
const res = await this.szuru.reverseSearch(this.selectedPost.imageUrl); const res = await this.szuru.reverseSearch(this.selectedPost.imageUrl);
this.removeMessage(msgSearchSimilar);
if (!res.exactPost && res.similarPosts.length == 0) { if (!res.exactPost && res.similarPosts.length == 0) {
this.pushInfo("No similar posts found"); msg.content = "No similar posts found";
} else { } else {
if (res.exactPost) { if (res.exactPost) {
this.pushError( msg.content = `<a href='${this.getPostUrl(res.exactPost)}' target='_blank'>
`<a href='${this.getPostUrl(res.exactPost)}' target='_blank'> Post already uploaded (${res.exactPost.id})</a>`;
Post already uploaded (${res.exactPost.id})</a>` msg.level = "error";
);
} }
for (const similarPost of res.similarPosts) { for (const similarPost of res.similarPosts) {
@ -273,15 +302,16 @@ export default Vue.extend({
this.pushInfo( this.pushInfo(
`<a href='${this.getPostUrl(similarPost.post)}' target='_blank'>Post ${similarPost.post.id} `<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() { async mounted() {
const cfg = await Config.load(); this.config = await Config.load();
this.activeSite = cfg.sites.length > 0 ? cfg.sites[0] : null; this.activeSite = this.config.sites.length > 0 ? this.config.sites[0] : null;
if (!this.activeSite) { if (!this.activeSite) {
this.pushError("No szurubooru server configured!"); this.pushError("No szurubooru server configured!");
@ -290,12 +320,7 @@ export default Vue.extend({
} }
// Always call grabPost, even when there is no activeSite // Always call grabPost, even when there is no activeSite
this.grabPost().then(() => { this.grabPost();
if (cfg.autoSearchSimilar) {
// No need to check if any vars are unset, findSimilar does that internally
this.findSimilar();
}
});
} }
}); });
</script> </script>
@ -304,7 +329,7 @@ export default Vue.extend({
.popup-container { .popup-container {
width: 700px; width: 700px;
display: grid; display: grid;
grid-template-columns: 400px 300px; grid-template-columns: 300px 400px;
} }
.popup-messages { .popup-messages {

View File

@ -1,2 +1 @@
// Dummy file so that webpack-extension-reloader doesn't complain when running `npm run watch:dev` // TODO: Upload posts here (and add tag categories if needed).
// This file is excluded when not running in development mode

View File

@ -5,7 +5,7 @@
"strict": true, "strict": true,
"module": "es2015", "module": "es2015",
"moduleResolution": "node", "moduleResolution": "node",
"target": "es5", "target": "es6",
"skipLibCheck": true, "skipLibCheck": true,
"esModuleInterop": true, "esModuleInterop": true,
"experimentalDecorators": true, "experimentalDecorators": true,

View File

@ -98,8 +98,6 @@ if (config.mode === 'production') {
} }
if (process.env.HMR === 'true') { if (process.env.HMR === 'true') {
config.entry['scripts/background'] = './scripts/background.ts';
config.plugins = (config.plugins || []).concat([ config.plugins = (config.plugins || []).concat([
new ExtensionReloader({ new ExtensionReloader({
entries: { entries: {