Add support for rule34.xxx

feature-show_image_resolution
neobooru 2019-08-05 00:58:38 +02:00
parent 817f9a7ef2
commit c1d4f899b2
1 changed files with 35 additions and 19 deletions

View File

@ -1,11 +1,18 @@
import ILoader from "./ILoader" import ILoader from "./ILoader"
import { SzuruPost, SzuruTag, SzuruCategory } from "../SzuruTypes"; import { SzuruPost, SzuruTag, SzuruCategory } from "../SzuruTypes";
// Small hack to avoid screwing with semver
enum Version {
v020 = 1, // 0.2.0
v025, // 0.2.5
}
export default class Gelbooru implements ILoader { export default class Gelbooru implements ILoader {
canImport(location: Location): boolean { canImport(location: Location): boolean {
return ( return (
location.host == "safebooru.org" || location.host == "safebooru.org" ||
location.host == "gelbooru.com" location.host == "gelbooru.com" ||
location.host == "rule34.xxx"
); );
} }
@ -14,26 +21,26 @@ export default class Gelbooru implements ILoader {
* Make sure to use the `dom` variable and NOT `document`! * Make sure to use the `dom` variable and NOT `document`!
*/ */
// TODO: Replace this with version checking, instead of just one 'special case' domain let version: Version;
let isSafebooru: boolean; switch (dom.location.host) {
const safebooruSiteTitle = dom.querySelector("#site-title > a") as HTMLAnchorElement; case "safebooru.org":
case "rule34.xxx":
if (safebooruSiteTitle && safebooruSiteTitle.innerText == "Safebooru") { version = Version.v020;
// If safebooru (and maybe other sites?) break;
isSafebooru = true; case "gelbooru.com":
} else { version = Version.v025;
// If gelbooru (version 0.2.5) break;
isSafebooru = false; default:
console.error("Couldn't guess version");
return null;
} }
console.log("isSafebooru: " + isSafebooru); console.log("Gelbooru guessed version: " + version);
let post = new SzuruPost(); let post = new SzuruPost();
// Set source to the current page (NOT a direct link to the image)
post.source = dom.location.href; post.source = dom.location.href;
// Get image url (direct url to image) // Set image url (direct url to image)
const originalImageElements = dom.querySelectorAll("li > a"); const originalImageElements = dom.querySelectorAll("li > a");
for (const idx in originalImageElements) { for (const idx in originalImageElements) {
const el = originalImageElements[idx] as HTMLAnchorElement; const el = originalImageElements[idx] as HTMLAnchorElement;
@ -48,7 +55,7 @@ export default class Gelbooru implements ILoader {
const safetyExp = new RegExp("Rating: (.*)"); const safetyExp = new RegExp("Rating: (.*)");
let safetyElements: NodeListOf<Element>; let safetyElements: NodeListOf<Element>;
if (isSafebooru) { if (version == Version.v020) {
safetyElements = dom.querySelectorAll("#stats > ul > li"); safetyElements = dom.querySelectorAll("#stats > ul > li");
} else { } else {
safetyElements = dom.querySelectorAll("#tag-list > div > li"); safetyElements = dom.querySelectorAll("#tag-list > div > li");
@ -80,7 +87,7 @@ export default class Gelbooru implements ILoader {
// Set tags // Set tags
let tagElements: NodeListOf<Element>; let tagElements: NodeListOf<Element>;
if (isSafebooru) { if (version == Version.v020) {
tagElements = dom.querySelectorAll("#tag-sidebar > li"); tagElements = dom.querySelectorAll("#tag-sidebar > li");
} else { } else {
tagElements = dom.querySelectorAll("#tag-list > div > li[class^='tag-type']"); tagElements = dom.querySelectorAll("#tag-list > div > li[class^='tag-type']");
@ -91,8 +98,17 @@ export default class Gelbooru implements ILoader {
if (el && el.innerText) { if (el && el.innerText) {
let tagName: string; let tagName: string;
if (isSafebooru) { if (version == Version.v020) {
tagName = el.getElementsByTagName("a")[0].innerText; const nameElements = el.getElementsByTagName("a");
// Some <li/> don't actually contain a name.
// E.g. the sub-headers ("Copyright", "Character", "General", "Meta") on rule34.xxx
if (nameElements.length > 0) {
tagName = el.getElementsByTagName("a")[0].innerText;
}
else {
continue;
}
} else { } else {
// First <a> is "?" // First <a> is "?"
// Second <a> is "actual_tag_name" // Second <a> is "actual_tag_name"