-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathao3.js
More file actions
116 lines (88 loc) · 3.29 KB
/
ao3.js
File metadata and controls
116 lines (88 loc) · 3.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import { addContributor, cleanText, collectObject, fetchHTML, getFormattedText, remapKeys } from "../shared/utils.js";
import { Extractor } from "./AbstractExtractor.js"
class archiveOfOurOwnScraper extends Extractor {
get _name() { return "Archive of Our Own Extractor"; }
needsReload = false;
_sitePatterns = [
/https:\/\/archiveofourown\.org\/+works\/+(\d+)/
];
async getDetails() {
return collectObject([
{
"Publisher": "Archive of Our Own",
"Reading Format": "Ebook",
"Edition Format": "Web Novel",
},
getMetadata(),
getDescription(),
]);
}
}
async function getDescription() {
const baseUrl = document.location.href.match(/https:\/\/archiveofourown\.org\/+works\/+\d+/)[0]
const doc = await fetchHTML(`${baseUrl}?view_full_work=true`);
if (!doc) return {};
const summery = doc.querySelector(`#workskin .summary blockquote`);
return { "Description": getFormattedText(summery) };
}
function getMetadata() {
let details = {};
const contributors = details["Contributors"] ?? [];
val = document.querySelector(`#workskin a[rel="author"]`)?.textContent;
if (val) addContributor(contributors, val, "Author");
details["Contributors"] = contributors;
val = document.querySelector(`#workskin>.preface .title`)?.textContent;
if (val) details["Title"] = val;
const metaInfo = document.querySelector(`.work div.wrapper>dl`);
if (metaInfo) {
for (let i = 0; i < metaInfo.children.length; i += 2) {
const titleEl = metaInfo.children[i];
const valueEl = metaInfo.children[i + 1];
if (!titleEl || !valueEl) continue;
let title = cleanText(titleEl?.textContent?.replace(":", "") ?? "")
let value = cleanText(valueEl?.textContent ?? "")
if (title === "Archive Warning" && value === "Creator Chose Not To Use Archive Warnings") continue;
if (valueEl.querySelector(`ul`)) {
value = [...valueEl.querySelectorAll(`li`)].map(i => cleanText(i.textContent));
}
if (title === "Stats") {
const statInfo = valueEl.querySelector(`dl.stats`);
if (!statInfo) continue;
for (let j = 0; j < metaInfo.children.length; j += 2) {
const statTitleEl = statInfo.children[j];
const statValueEl = statInfo.children[j + 1];
if (!statTitleEl || !statValueEl) continue;
title = cleanText(statTitleEl?.textContent?.replace(":", "") ?? "")
value = cleanText(statValueEl?.textContent ?? "")
if (title === "Chapters") {
if (value.indexOf("?") === -1) details["Status"] = "Completed";
else details["Status"] = "Incomplete";
value = value.split("/")[0]
}
details[title] = value;
}
continue;
}
details[title] = value;
}
}
details = remapKeys({
"Characters": undefined,
"Additional Tags": undefined,
"Fandoms": undefined,
"Fandom": undefined,
"Categories": undefined,
"Relationships": undefined,
"Category": undefined,
"Series": undefined,
"Hits": undefined,
"Bookmarks": undefined,
"Kudos": undefined,
"Comments": undefined,
"Updated": undefined,
"Published": "Publication date",
"Archive Warnings": "Content Warnings"
}, details);
return details;
}
export { archiveOfOurOwnScraper };