forked from Dylancyclone/goodreads-libby-userscript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibby-availability.user.js
More file actions
315 lines (293 loc) · 12.4 KB
/
Copy pathlibby-availability.user.js
File metadata and controls
315 lines (293 loc) · 12.4 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// ==UserScript==
// @name Goodreads and Amazon Libby Results
// @namespace https://github.com/holyspiritomb
// @version 2.0.5
// @description Searches for the book you are looking at on Goodreads or Amazon across all your libby libraries with cards. Originally forked from Dylancyclone's Goodreads Libby Results script.
// @author holyspiritomb
// @updateURL https://raw.githubusercontent.com/holyspiritomb/libby-userscript/main/libby-availability.user.js
// @match https://libbyapp.com/interview/menu
// @include /^https?://.*\.goodreads\.com/book/show.*$/
// @include *://*.amazon.tld/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=libbyapp.com
// @grant GM.setValue
// @grant GM.getValue
// @grant GM_addStyle
// @run-at document-idle
// @license MIT
// ==/UserScript==
(function () {
"use strict";
const syncLibraries = () => {
// Grab libraries from libby and remove circular references
// Use current cards instead of all libraries in history
const libraries = unsafeWindow.APP.patron.cards.all.map((card) => {
return {
baseKey: card.library.baseKey,
_: { activeKey: card.library._.activeKey, name: card.library._.name },
};
});
console.log("library sync button clicked");
console.log(libraries);
const stringylibraries = JSON.stringify(libraries);
GM.setValue("libraries", stringylibraries);
};
function currentSite() {
if (unsafeWindow.location.host == "www.amazon.com") {
return "amazon";
} else if (unsafeWindow.location.host == "www.goodreads.com") {
return "gr";
} else if (unsafeWindow.location.host == "libbyapp.com" ) {
return "libby";
}
}
const site = currentSite();
function getApiString(title, author) {
let searchString;
if (author != undefined) {
searchString = encodeURIComponent(title) + "&creator=" + encodeURIComponent(author);
// return searchString;
} else {
searchString = encodeURIComponent(title);
}
return searchString;
}
function getUrlString(title, author) {
let searchString;
if (author != undefined) {
searchString = encodeURIComponent(title) + encodeURIComponent(" ") + encodeURIComponent(author);
// return searchString;
} else {
searchString = encodeURIComponent(title);
}
return searchString;
}
async function findAnchor() {
let anchorEl;
if (site == "amazon") {
const findAmznBox = async () => document.getElementById("shopAllFormats_feature_div") || document.getElementById("bookDescription_feature_div") || document.getElementById("tmmSwatches");
anchorEl = await findAmznBox();
} else if (site == "gr") {
anchorEl = document.querySelector(".BookPageMetadataSection__description");
if (anchorEl == null) {
let findGrBox = () => document.querySelector("[itemprop='description']") || document.getElementById("descriptionContainer");
anchorEl = findGrBox();
}
} else {
return;
}
return anchorEl;
}
function sanitize(t) {
let sanitized = t.replace(/\(.*\)/, "").replace(/^\s+|\s+$/g, '').replace(/[&|,]/g, ' ').replace(/: .*/, '').replace(/[ ]+/, ' ');
return sanitized;
}
const getTitle = async () => {
if (site == "amazon") {
let findAmTitleEl = () => document.querySelector("#ebooksTitle") || document.querySelector("span#productTitle") || document.querySelector("#title");
const bookTitleEl = findAmTitleEl();
return bookTitleEl.innerText;
} else if (site == "gr") {
const bookTitleEl = document.querySelector("[data-testid='bookTitle']");
return bookTitleEl.innerText;
}
}
const getAuthor = async () => {
if (site == "amazon") {
let findAuthorEl = () => document.querySelector("div#bylineInfo > span.author > a") || document.querySelector("div#bylineInfo > a#bylineContributor");
const authorEl = findAuthorEl();
if (authorEl == null) {
return undefined;
} else {
return authorEl.innerText;
}
} else if (site == "gr") {
const findAuthorEl = () => document.querySelector("[aria-label^='By: ']") || document.querySelector("span.ContributorLink__name");
const authorEl = findAuthorEl();
return authorEl.innerText;
}
}
async function createResultsDiv() {
const libbyContainer = document.createElement("div");
libbyContainer.id = "grLibbyBoxforked";
libbyContainer.style.margin = "10px";
let libbyResultsHeader;
if (site == "amazon") {
libbyResultsHeader = document.createElement("h3");
libbyResultsHeader.className = "rpi-header a-spacing-small";
} else if (site == "gr") {
libbyResultsHeader = document.createElement("h4");
libbyResultsHeader.className = "Text__title4";
}
libbyResultsHeader.innerHTML = "Libby Userscript Results";
libbyContainer.appendChild(libbyResultsHeader);
let libbyResultsContainer = document.createElement("div");
libbyResultsContainer.id = "libby-results-forked";
libbyResultsContainer.style.padding = "5px";
libbyResultsContainer.style.lineHeight = "1.5em";
libbyResultsContainer.style.height = "auto";
if (site == "gr") {
libbyResultsContainer.style.marginLeft = "1em";
libbyResultsContainer.style.overflowY = "auto";
libbyResultsContainer.style.maxHeight = "30vh";
}
libbyResultsContainer.style.display = "flex";
libbyResultsContainer.style.flexDirection = "column";
libbyContainer.appendChild(libbyResultsContainer);
return libbyContainer;
}
function insertContainer(el, prevContainer) {
var position;
if (site == "gr") {
position = "afterend"
} else if (site == "amazon") {
position = "beforebegin"
}
prevContainer.insertAdjacentElement(position, el);
}
const createLibbyButton = () => {
let builderDiv = document.createElement("li");
builderDiv.innerHTML = `
<li class="summary-list-action">
<button class="summary-list-action-add-library halo" role="button" type="button">
<span role="text" id="libby-script-forked">Save Libraries (userscript)</span>
</button>
</li>
`.trim();
let libbySyncButton = builderDiv.firstChild;
libbySyncButton.onclick = syncLibraries;
return libbySyncButton;
};
/**
* Add the button
* Might outrun the rest of the dom,
* so keep retrying until the container is ready
*/
const addLibbyButton = () => {
// let container = document.getElementsByClassName("menu-library-buttons");
let container = document.getElementsByClassName("summary-list-action-add-library");
if (container && container[0]) {
container[0].parentNode.parentNode.insertBefore(
createLibbyButton(),
container[0].parentNode.nextSibling
);
} else {
setTimeout(addLibbyButton, 10);
}
};
const addGoodreadsResults = async () => {
const anchor = await findAnchor();
if (anchor && anchor != undefined) {
let libbyResults = await createResultsDiv();
insertContainer(libbyResults, anchor);
}
const bookAuthorStr = await getAuthor();
const bookTitle = await getTitle();
let searchTitle = sanitize(bookTitle);
let apiSearchString = getApiString(searchTitle, bookAuthorStr);
let urlSearchString = getUrlString(searchTitle, bookAuthorStr);
let libraries = JSON.parse(await GM.getValue("libraries", "[]"));
if (libraries.length === 0) {
document.getElementById(
"libby-results-forked"
).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=${apiSearchString}`;
fetch(url)
.then((response) => response.json())
.then((result) => {
if (result.totalItems === 0){
console.log(`none found at ${library.baseKey}`);
let noresultsElem = document.createElement('div');
noresultsElem.className=library.baseKey;
noresultsElem.style.paddingBottom="5px";
noresultsElem.style.display = "flex";
noresultsElem.style.flexDirection = "row";
let noresultsElementLink = document.createElement("a");
noresultsElementLink.id = `libby-forked-${library.baseKey}`;
noresultsElementLink.href = `https://libbyapp.com/search/${libraryKey}/search/query-${urlSearchString}/page-1`;
noresultsElementLink.style.color = "#555";
noresultsElementLink.innerText = "none found";
noresultsElem.appendChild(noresultsElementLink);
const libbyResults = () => document.getElementById("libby-results-forked");
const box = libbyResults();
box.appendChild(noresultsElem);
// document.getElementById("libby-results-forked").appendChild(noresultsElem);
} else {
let resultsElement = document.createElement('div');
resultsElement.className=library.baseKey;
resultsElement.style.paddingBottom="5px";
resultsElement.style.display = "flex";
resultsElement.style.flexDirection = "row";
let resultsElementLink = document.createElement("div");
resultsElementLink.id = `libby-forked-${libraryKey}`;
// resultsElementLink.href = `https://${library.baseKey}.overdrive.com/search/title?query=${apiSearchString}`;
// resultsElementLink.href = `https://libbyapp.com/search/${library.baseKey}/search/query-${urlSearchString}/page-1`;
resultsElement.appendChild(resultsElementLink);
// document.getElementById("libby-results-forked").appendChild(resultsElement);
const libbyResults= () => document.getElementById("libby-results-forked");
const box = libbyResults();
box.appendChild(resultsElement);
let resultItems = result.items;
resultItems.forEach(item => {
console.log(item);
var itemFormat = "";
if (item.type.id === "audiobook"){
itemFormat = '\uD83C\uDFA7'
}
if (item.type.id === "ebook"){
itemFormat = '\uD83D\uDCDA'
}
var bookLinkText;
var linkColor;
if (item.ownedCopies != 0) {
if (item.availableCopies === 0) {
bookLinkText = `${item.holdsCount}/${item.ownedCopies} holds ${itemFormat}`;
linkColor = (document.querySelector("html[data-theme='light']")) ? "orange" : "#ffbe3d";
} else {
bookLinkText = `${item.availableCopies} available ${itemFormat}`;
linkColor = (document.querySelector("html[data-theme='light']")) ? "limegreen" : "#6dff6d";
}
} else {
bookLinkText = "request"
linkColor = (document.querySelector("html[data-theme='light']")) ? "orange" : "#ffbe3d";
}
let resultElem = document.createElement('a');
resultElem.className = "result";
resultElem.href = `https://libbyapp.com/search/${libraryKey}/search/query-${urlSearchString}/page-1/${item.id}`;
resultElem.style.display = "block";
resultElem.title = `${libraryKey}: ${item.title} by ${item.creators[0].name} ${itemFormat}`;
resultElem.style.color = linkColor;
resultElem.innerHTML = bookLinkText;
document.getElementById(
`libby-forked-${libraryKey}`
).appendChild(resultElem);
});
}
});
});
// put something here in case of no resulrs
};
if (unsafeWindow.location.host == "libbyapp.com") {
addLibbyButton();
} else if ((unsafeWindow.location.host == "www.goodreads.com") || (unsafeWindow.location.host == "www.amazon.com")) {
GM_addStyle(`#libby-results-forked > div::before {
content: attr(class) ': ';
flex-basis: 7.5em;
line-height: inherit;
}
#libby-results-forked > div,
#libby-results-forked > div > a,
#libby-results-forked > div > a > div{
line-height: inherit;
}
#libby-results-forked > div > a{
text-decoration: none;
}
#libby-results-forked > div > a:hover {
text-decoration: underline;
}`);
addGoodreadsResults();
}
})();