Skip to content

Commit caac112

Browse files
committed
Merge branch 'dev' into main
2 parents d33e543 + c23c375 commit caac112

4 files changed

Lines changed: 130 additions & 75 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

changelog.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
9/30/2022 1.1.0:
2+
+ Updated to support the new Goodreads UI
3+
+ Split eBook and Audiobook results
4+
5+
3/4/2022 1.0.0:
6+
+ Initial Release

goodreads-libby.user.js

Lines changed: 123 additions & 74 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,90 +11,139 @@
1111
// @grant GM.getValue
1212
// @license MIT
1313
// ==/UserScript==
14+
window.addEventListener(
15+
"load",
16+
function () {
17+
;(function () {
18+
"use strict"
1419

15-
(function () {
16-
"use strict";
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+
}
1731

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-
};
29-
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-
/**
45-
* Add the button
46-
* Might outrun the rest of the dom,
47-
* so keep retrying until the container is ready
48-
*/
49-
const addLibbyButton = () => {
50-
let container = document.getElementsByClassName("menu-library-buttons");
51-
if (container && container[0]) {
52-
container[0].parentNode.insertBefore(
53-
createLibbyButton(),
54-
container[0].nextSibling
55-
);
56-
} else {
57-
setTimeout(addLibbyButton, 10);
58-
}
59-
};
46+
const createGoodreadsResults = async () => {
47+
let builderDiv = document.createElement("div")
6048

61-
const addGoodreadsResults = async () => {
62-
let bookTitle = document.getElementById("bookTitle").innerHTML.trim();
63-
let bookAuthor = document
64-
.getElementsByClassName("authorName")[0]
65-
.firstChild.innerHTML.trim();
66-
let searchString = encodeURIComponent(`${bookTitle} ${bookAuthor}`);
67-
let libraries = JSON.parse(await GM.getValue("libraries", "[]"));
68-
document.body.innerHTML += `<div style="position: fixed;
69-
top: 100px;
70-
left: 1em;
71-
width: 400px;
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="
7259
background-color: #ececec;
7360
border: 1px solid black;
74-
padding: 1em;"><h3>Libby results</h3><div id="libby-results"></div></div>`;
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()
73+
74+
let goodreadsResults = builderDiv.firstChild
75+
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+
}
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+
})
106+
107+
return goodreadsResults
108+
}
75109

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-
}
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+
}
81126

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-
document.getElementById(
89-
"libby-results"
90-
).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>`;
91-
});
92-
});
93-
};
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+
}
94140

95-
if (unsafeWindow.location.host == "libbyapp.com") {
96-
addLibbyButton();
97-
} else if (unsafeWindow.location.host == "www.goodreads.com") {
98-
addGoodreadsResults();
99-
}
100-
})();
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)