-
Notifications
You must be signed in to change notification settings - Fork 3
/
series-csv.user.js
79 lines (73 loc) · 2.63 KB
/
series-csv.user.js
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
// ==UserScript==
// @name series csv
// @namespace http://tampermonkey.net/
// @version 0.1
// @description goodreads data in tabular and csv format
// @author Josh Parker
// @match https://www.goodreads.com/series/*
// @icon https://www.google.com/s2/favicons?domain=goodreads.com
// @grant none
// ==/UserScript==
(function userScript() {
let csv = 'title,author,rating,ratings,reviews,date,editions';
const table = document.createElement('table');
const thead = document.createElement('thead');
const tbody = document.createElement('tbody');
const headerRow = document.createElement('tr');
csv.split(',').forEach((heading) => {
const th = document.createElement('th');
th.textContent = heading;
headerRow.appendChild(th);
});
thead.appendChild(headerRow);
table.appendChild(thead);
table.appendChild(tbody);
[...document.querySelectorAll('.responsiveBook')]
.map((rb) => {
const title = rb.querySelector('.gr-h3 span[itemprop="name"]').textContent;
const author = rb.querySelector('span[itemprop="author"]').textContent;
const rating = rb.querySelector('.communityRating__starsWrapper ~ .gr-metaText').textContent;
const allTextContent = rb.textContent;
const matches = allTextContent.match(
/(?<ratings>[\d,]+) Ratings[^\d\w]*((?<reviews>[\d,]+) Reviews)?[^\d\w]*(published (?<date>\d+))?[^\d\w]*((?<editions>\d+) editions)?/,
);
return {
title,
author,
rating,
...matches.groups,
};
})
.map(
({
title, author, rating, ratings, reviews, date, editions,
}) => `\n"${title}","${author}","${rating}","${ratings}","${reviews || 0}","${date || 'unknown'}","${
editions || 1
}"`,
)
.forEach((row) => {
csv += row;
const bodyRow = document.createElement('tr');
row.split('","').forEach((d) => {
const td = document.createElement('td');
td.textContent = d.replaceAll('"', '');
bodyRow.appendChild(td);
});
tbody.appendChild(bodyRow);
});
document.querySelector('.responsiveMainContentContainer').appendChild(table);
const downloadButton = document.createElement('button');
downloadButton.textContent = 'Save table as csv';
downloadButton.addEventListener('click', () => {
const blob = new Blob([csv], { type: 'text/csv' });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = `${document.title.split('Series')[0].trim().replace(/\s+/g, '-')}-series.csv`;
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
URL.revokeObjectURL(url);
});
document.querySelector('.responsiveMainContentContainer').appendChild(downloadButton);
}());