-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgoodreads-libby.user.js
190 lines (178 loc) · 6.01 KB
/
goodreads-libby.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// ==UserScript==
// @name Goodreads Libby Results
// @namespace https://github.com/Dylancyclone/goodreads-libby-userscript
// @version 1.2.1
// @description Searches for the book you are looking at on Goodreads across all your libby libraries
// @author Dylancyclone
// @match https://libbyapp.com/interview/menu
// @include /^https?://.*\.goodreads\.com/book/show.*$/
// @icon https://www.google.com/s2/favicons?sz=64&domain=libbyapp.com
// @grant GM.setValue
// @grant GM.getValue
// @license MIT
// ==/UserScript==
window.addEventListener(
"load",
function () {
;(function () {
"use strict"
const syncLibraries = () => {
// Grab libraries from libby and remove circular references
let libraries = unsafeWindow.APP.libraries.all.map((library) => {
return {
baseKey: library.baseKey,
_: { activeKey: library._.activeKey, name: library._.name },
}
})
libraries = JSON.stringify(libraries)
GM.setValue("libraries", libraries)
}
const createLibbyButton = () => {
let builderDiv = document.createElement("div")
builderDiv.innerHTML = `
<div class="summary-list-action">
<button class="summary-list-action-add-library halo" role="button" type="button">
<span role="text">Save Libraries (userscript)</span>
</button>
</div>
`.trim()
let libbySyncButton = builderDiv.firstChild
libbySyncButton.onclick = syncLibraries
return libbySyncButton
}
const renderSearchResults = async (
libraries,
searchString,
targetDiv,
openInOverdrive
) => {
targetDiv.innerHTML = ""
if (libraries.length === 0) {
targetDiv.innerHTML = `No libraries found, please visit <a href="https://libbyapp.com/interview/menu" target="_blank">here</a> to sync your libraries.`
}
libraries.map((library) => {
let libraryKey = library._.activeKey || library.baseKey
let url = `https://thunder.api.overdrive.com/v2/libraries/${libraryKey}/media?query=${searchString}`
fetch(url)
.then((response) => response.json())
.then((result) => {
let ebookCount = result.items.filter(
(item) => item.type.id === "ebook"
).length
let audiobookCount = result.items.filter(
(item) => item.type.id === "audiobook"
).length
let link = openInOverdrive
? `https://${library.baseKey}.overdrive.com/search?query=${searchString}`
: `https://libbyapp.com/search/${library.baseKey}/search/query-${searchString}/page-1`
targetDiv.innerHTML += `
<tr>
<td style="padding-right: 20px;">${library._.name}</td>
<td>
<a href="${link}" target="_blank">
${ebookCount || "-"} 📕 / ${audiobookCount || "-"} 🎧
</a>
</td>
</tr>`
})
})
}
const createGoodreadsResults = async () => {
let builderDiv = document.createElement("div")
let bookTitle = document
.querySelector("[data-testid='bookTitle']")
.innerHTML.trim()
let bookAuthor = document
.querySelector("[data-testid='name']")
.innerHTML.trim()
let searchString = encodeURIComponent(`${bookTitle} ${bookAuthor}`)
let libraries = JSON.parse(await GM.getValue("libraries", "[]"))
let openInOverdrive = JSON.parse(
await GM.getValue("openInOverdrive", "false")
)
builderDiv.innerHTML = `
<div style="
background-color: #ececec;
border: 1px solid black;
margin-top: 25px;
padding: 1em;"
>
<h3>Libby results</h3>
<table id="libby-results">
<tr>
<th style="margin-right: 20px;">Library</th>
<th>Results</th>
</tr>
</table>
<div style="margin-top: 10px;">
<p> Open links in: </p>
<input type="radio" id="openInLibbyButton" name="openLinksIn" value="libby" ${
!openInOverdrive ? "checked" : ""
}>
<label for="libby">Libby</label>
<input type="radio" id="openInOverdriveButton" name="openLinksIn" value="overdrive" ${
openInOverdrive ? "checked" : ""
}>
<label for="overdrive">Overdrive</label>
</div>
</div>
`.trim()
let libbyResults = builderDiv.querySelector("#libby-results")
let openInLibbyButton = builderDiv.querySelector("#openInLibbyButton")
openInLibbyButton.onclick = () => {
GM.setValue("openInOverdrive", "false")
renderSearchResults(libraries, searchString, libbyResults, false)
}
let openInOverdriveButton = builderDiv.querySelector(
"#openInOverdriveButton"
)
openInOverdriveButton.onclick = () => {
GM.setValue("openInOverdrive", "true")
renderSearchResults(libraries, searchString, libbyResults, true)
}
renderSearchResults(
libraries,
searchString,
libbyResults,
openInOverdrive
)
return builderDiv.firstChild
}
/**
* Add the buttons
* Might outrun the rest of the dom,
* so keep retrying until the container is ready
*/
const addLibbyButton = () => {
let container = document.getElementsByClassName("summary-list-section-actions")
if (container && container[0]) {
container[0].parentNode.insertBefore(
createLibbyButton(),
container[0].nextSibling
)
} else {
setTimeout(addLibbyButton, 10)
}
}
const addGoodreadsResults = async () => {
let container = document.getElementsByClassName("BookDetails")
if (container && container[0]) {
createGoodreadsResults().then((goodreadsResults) => {
let test = container[0].parentNode.insertBefore(
goodreadsResults,
container[0].nextSibling
)
})
} else {
setTimeout(addGoodreadsResults, 10)
}
}
if (unsafeWindow.location.host == "libbyapp.com") {
addLibbyButton()
} else if (unsafeWindow.location.host == "www.goodreads.com") {
addGoodreadsResults()
}
})()
},
false
)