Skip to content

Commit 4f0d71e

Browse files
committed
Split up eBook and Audiobook results and lint
With less time to read coupled with more time spent driving to commute, I find myself leaning towards audiobooks more. By splitting up the results, it is much easier to see if a particular library has audiobooks available, or just eBooks These were supposed to be separate commits whoops
1 parent 503889c commit 4f0d71e

3 files changed

Lines changed: 122 additions & 96 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ Go to the libby web app's main menu (https://libbyapp.com/interview/menu) and cl
2222

2323
![The Save Libraries button](./libby.png)
2424

25-
Then, search for a book on Goodreads. When you've found a book, the userscript will search across all your libby libraries and show the results on the left side of the screen.
25+
Then, search for a book on Goodreads. When you've found a book, the userscript will search across all your libby libraries and show the results under the book's description.
2626

2727
![The search results on Goodreads](./goodreads.png)
2828

goodreads-libby.user.js

Lines changed: 121 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// ==UserScript==
22
// @name Goodreads Libby Results
33
// @namespace https://github.com/Dylancyclone/goodreads-libby-userscript
4-
// @version 1.0.0
4+
// @version 1.1.0
55
// @description Searches for the book you are looking at on Goodreads across all your libby libraries
66
// @author Dylancyclone
77
// @match https://libbyapp.com/interview/menu
@@ -11,113 +11,139 @@
1111
// @grant GM.getValue
1212
// @license MIT
1313
// ==/UserScript==
14-
window.addEventListener('load', function() {
15-
(function () {
16-
"use strict";
14+
window.addEventListener(
15+
"load",
16+
function () {
17+
;(function () {
18+
"use strict"
1719

18-
const syncLibraries = () => {
19-
// Grab libraries from libby and remove circular references
20-
let libraries = unsafeWindow.APP.libraries.all.map((library) => {
21-
return {
22-
baseKey: library.baseKey,
23-
_: { activeKey: library._.activeKey, name: library._.name },
24-
};
25-
});
26-
libraries = JSON.stringify(libraries);
27-
GM.setValue("libraries", libraries);
28-
};
20+
const syncLibraries = () => {
21+
// Grab libraries from libby and remove circular references
22+
let libraries = unsafeWindow.APP.libraries.all.map((library) => {
23+
return {
24+
baseKey: library.baseKey,
25+
_: { activeKey: library._.activeKey, name: library._.name },
26+
}
27+
})
28+
libraries = JSON.stringify(libraries)
29+
GM.setValue("libraries", libraries)
30+
}
2931

30-
const createLibbyButton = () => {
31-
let builderDiv = document.createElement("div");
32-
builderDiv.innerHTML = `
32+
const createLibbyButton = () => {
33+
let builderDiv = document.createElement("div")
34+
builderDiv.innerHTML = `
3335
<div class="menu-library-buttons">
3436
<button class="menu-library-buttons-add-library halo" role="button" type="button">
3537
<span role="text">Save Libraries (userscript)</span>
3638
</button>
3739
</div>
38-
`.trim();
39-
let libbySyncButton = builderDiv.firstChild;
40-
libbySyncButton.onclick = syncLibraries;
41-
return libbySyncButton;
42-
};
40+
`.trim()
41+
let libbySyncButton = builderDiv.firstChild
42+
libbySyncButton.onclick = syncLibraries
43+
return libbySyncButton
44+
}
4345

44-
const createGoodreadsResults = async () => {
45-
let builderDiv = document.createElement("div");
46+
const createGoodreadsResults = async () => {
47+
let builderDiv = document.createElement("div")
4648

47-
let bookTitle = document.querySelector("[data-testid='bookTitle']").innerHTML.trim();
48-
let bookAuthor = document.querySelector("[data-testid='name']").innerHTML.trim();
49-
let searchString = encodeURIComponent(`${bookTitle} ${bookAuthor}`);
50-
let libraries = JSON.parse(await GM.getValue("libraries", "[]"));
51-
builderDiv.innerHTML = `
52-
<div style="
53-
background-color: #ececec;
54-
border: 1px solid black;
55-
margin-top: 25px;
56-
padding: 1em;"
57-
>
58-
<h3>Libby results</h3>
59-
<div id="libby-results"></div>
60-
</div>
61-
`.trim();
49+
let bookTitle = document
50+
.querySelector("[data-testid='bookTitle']")
51+
.innerHTML.trim()
52+
let bookAuthor = document
53+
.querySelector("[data-testid='name']")
54+
.innerHTML.trim()
55+
let searchString = encodeURIComponent(`${bookTitle} ${bookAuthor}`)
56+
let libraries = JSON.parse(await GM.getValue("libraries", "[]"))
57+
builderDiv.innerHTML = `
58+
<div style="
59+
background-color: #ececec;
60+
border: 1px solid black;
61+
margin-top: 25px;
62+
padding: 1em;"
63+
>
64+
<h3>Libby results</h3>
65+
<table id="libby-results">
66+
<tr>
67+
<th style="margin-right: 20px;">Library</th>
68+
<th>Results</th>
69+
</tr>
70+
</div>
71+
</div>
72+
`.trim()
6273

63-
let goodreadsResults = builderDiv.firstChild;
74+
let goodreadsResults = builderDiv.firstChild
6475

65-
if (libraries.length === 0) {
66-
document.getElementById(
67-
"libby-results"
68-
).innerHTML = `No libraries found, please visit <a href="https://libbyapp.com/interview/menu" target="_blank">here</a> to sync your libraries.`;
69-
}
76+
if (libraries.length === 0) {
77+
document.getElementById(
78+
"libby-results"
79+
).innerHTML = `No libraries found, please visit <a href="https://libbyapp.com/interview/menu" target="_blank">here</a> to sync your libraries.`
80+
}
7081

71-
libraries.map((library) => {
72-
let libraryKey = library._.activeKey || library.baseKey;
73-
let url = `https://thunder.api.overdrive.com/v2/libraries/${libraryKey}/media?query=${searchString}`;
74-
fetch(url)
75-
.then((response) => response.json())
76-
.then((result) => {
77-
document.getElementById(
78-
"libby-results"
79-
).innerHTML += `<div>${library._.name} <b><a href="https://libbyapp.com/search/${library.baseKey}/search/query-${searchString}/page-1" target="_blank">${result.totalItems} results</a></b></div>`;
80-
});
81-
});
82+
libraries.map((library) => {
83+
let libraryKey = library._.activeKey || library.baseKey
84+
let url = `https://thunder.api.overdrive.com/v2/libraries/${libraryKey}/media?query=${searchString}`
85+
fetch(url)
86+
.then((response) => response.json())
87+
.then((result) => {
88+
let ebookCount = result.items.filter(
89+
(item) => item.type.id === "ebook"
90+
).length
91+
let audiobookCount = result.items.filter(
92+
(item) => item.type.id === "audiobook"
93+
).length
94+
document.getElementById("libby-results").innerHTML += `
95+
<tr>
96+
<td style="padding-right: 20px;">${library._.name}</td>
97+
<td><a
98+
href="https://libbyapp.com/search/${
99+
library.baseKey
100+
}/search/query-${searchString}/page-1"
101+
target="_blank">
102+
${ebookCount || "-"} 📕 / ${audiobookCount || "-"} 🎧</a></td>
103+
</tr>`
104+
})
105+
})
82106

83-
return goodreadsResults
84-
};
107+
return goodreadsResults
108+
}
85109

86-
/**
87-
* Add the buttons
88-
* Might outrun the rest of the dom,
89-
* so keep retrying until the container is ready
90-
*/
91-
const addLibbyButton = () => {
92-
let container = document.getElementsByClassName("menu-library-buttons");
93-
if (container && container[0]) {
94-
container[0].parentNode.insertBefore(
95-
createLibbyButton(),
96-
container[0].nextSibling
97-
);
98-
} else {
99-
setTimeout(addLibbyButton, 10);
100-
}
101-
};
110+
/**
111+
* Add the buttons
112+
* Might outrun the rest of the dom,
113+
* so keep retrying until the container is ready
114+
*/
115+
const addLibbyButton = () => {
116+
let container = document.getElementsByClassName("menu-library-buttons")
117+
if (container && container[0]) {
118+
container[0].parentNode.insertBefore(
119+
createLibbyButton(),
120+
container[0].nextSibling
121+
)
122+
} else {
123+
setTimeout(addLibbyButton, 10)
124+
}
125+
}
102126

103-
const addGoodreadsResults = async () => {
104-
let container = document.getElementsByClassName("BookDetails");
105-
if (container && container[0]) {
106-
createGoodreadsResults().then(goodreadsResults => {
107-
let test = container[0].parentNode.insertBefore(
108-
goodreadsResults,
109-
container[0].nextSibling
110-
);
111-
})
112-
} else {
113-
setTimeout(addGoodreadsResults, 10);
114-
}
115-
};
127+
const addGoodreadsResults = async () => {
128+
let container = document.getElementsByClassName("BookDetails")
129+
if (container && container[0]) {
130+
createGoodreadsResults().then((goodreadsResults) => {
131+
let test = container[0].parentNode.insertBefore(
132+
goodreadsResults,
133+
container[0].nextSibling
134+
)
135+
})
136+
} else {
137+
setTimeout(addGoodreadsResults, 10)
138+
}
139+
}
116140

117-
if (unsafeWindow.location.host == "libbyapp.com") {
118-
addLibbyButton();
119-
} else if (unsafeWindow.location.host == "www.goodreads.com") {
120-
addGoodreadsResults();
121-
}
122-
})();
123-
}, false);
141+
if (unsafeWindow.location.host == "libbyapp.com") {
142+
addLibbyButton()
143+
} else if (unsafeWindow.location.host == "www.goodreads.com") {
144+
addGoodreadsResults()
145+
}
146+
})()
147+
},
148+
false
149+
)

goodreads.png

19 KB
Loading

0 commit comments

Comments
 (0)