From 817f9a7ef21503cb19e0069d5524085e272b71d4 Mon Sep 17 00:00:00 2001 From: neobooru <50623835+neobooru@users.noreply.github.com> Date: Sun, 4 Aug 2019 21:43:08 +0200 Subject: [PATCH] Finish Danbooru loader + small tweaks --- src/SzuruTypes.ts | 2 +- src/css/style.css | 4 +++ src/loaders/Danbooru.ts | 67 +++++++++++++++++++++++++++++++++++++++-- src/loaders/Gelbooru.ts | 4 +-- src/popup/App.vue | 2 ++ 5 files changed, 73 insertions(+), 6 deletions(-) diff --git a/src/SzuruTypes.ts b/src/SzuruTypes.ts index 36da50b..31e25fe 100644 --- a/src/SzuruTypes.ts +++ b/src/SzuruTypes.ts @@ -20,7 +20,7 @@ export class SzuruTag { category?: SzuruCategory; constructor(name: string, category?: SzuruCategory) { - this.name = name; + this.name = name.replace(/\s/g, "_"); // Replace all spaces with underscores this.category = category; } } diff --git a/src/css/style.css b/src/css/style.css index cbf7a21..912539e 100644 --- a/src/css/style.css +++ b/src/css/style.css @@ -48,6 +48,10 @@ select { display: inline; } +.tag-general { + color: #24aadd; +} + .tag-copyright { color: rgb(170, 0, 170); } diff --git a/src/loaders/Danbooru.ts b/src/loaders/Danbooru.ts index 5217fb6..cb67836 100644 --- a/src/loaders/Danbooru.ts +++ b/src/loaders/Danbooru.ts @@ -11,11 +11,72 @@ export default class Danbooru implements ILoader { async grabPost(dom: Document): Promise { let post = new SzuruPost(); - post.source = dom.location.href; - // Get image url + // Set image url + const originalImageElements = Array.from(dom.querySelectorAll("#post-information > ul > li > a")) + .map(x => x as HTMLAnchorElement) + .filter(x => x.parentElement && x.parentElement.innerText.startsWith("Size")); + if (originalImageElements.length > 0) { + post.imageUrl = originalImageElements[0].href; + } + + // Set safety + const safetyExp = new RegExp("Rating: (.*)"); + const safetyElements = Array.from(document.querySelectorAll("#post-information > ul > li")) + .map(x => x as HTMLLIElement) + .filter(x => x.innerText.startsWith("Rating")); + + if (safetyElements.length > 0) { + const matches = safetyElements[0].innerText.match(safetyExp); + if (matches && matches.length > 0) { + switch (matches[1].toLowerCase()) { + case "safe": + post.safety = "safe"; + break; + case "questionable": + post.safety = "sketchy"; + break; + case "explicit": + post.safety = "unsafe"; + break; + } + } + } + + // Set tags + const tagElements = Array.from(dom.querySelectorAll("#tag-list > ul > li")).map(x => x as HTMLLIElement); + for (const tagElement of tagElements) { + let tagName: string | undefined; + const tagNameElement = tagElement.querySelector(".search-tag") as HTMLAnchorElement; + if (tagNameElement) { + tagName = tagNameElement.innerText; + } + + let category: SzuruCategory | undefined; + switch (tagElement.className) { + case "category-3": + category = SzuruCategory.Copyright; + break; + case "category-4": + category = SzuruCategory.Character; + break; + case "category-1": + category = SzuruCategory.Artist; + break; + case "category-5": + category = SzuruCategory.Meta; + break; + } + + if (tagName) { + let tag = new SzuruTag(tagName, category); + post.tags.push(tag); + } + } + + // console.dir(post); // If post has enough filled fields to be considered a valid post if (post.imageUrl) { @@ -24,4 +85,4 @@ export default class Danbooru implements ILoader { return null; } } -} \ No newline at end of file +} diff --git a/src/loaders/Gelbooru.ts b/src/loaders/Gelbooru.ts index 132a7f5..33194f5 100644 --- a/src/loaders/Gelbooru.ts +++ b/src/loaders/Gelbooru.ts @@ -92,11 +92,11 @@ export default class Gelbooru implements ILoader { let tagName: string; if (isSafebooru) { - tagName = el.getElementsByTagName("a")[0].innerText.replace(/\s/g, "_"); + tagName = el.getElementsByTagName("a")[0].innerText; } else { // First is "?" // Second is "actual_tag_name" - tagName = el.getElementsByTagName("a")[1].innerText.replace(/\s/g, "_"); + tagName = el.getElementsByTagName("a")[1].innerText; } let category: SzuruCategory | undefined; diff --git a/src/popup/App.vue b/src/popup/App.vue index cd885a8..c2ad165 100644 --- a/src/popup/App.vue +++ b/src/popup/App.vue @@ -171,6 +171,8 @@ export default Vue.extend({ if (tag.category) { classes.push("tag-" + tag.category.name); + } else { + classes.push("tag-general"); } return classes;