Skip to content

Commit 503889c

Browse files
committed
Fix for Goodreads UI update and move results
Goodreads has changed around their interface so the old results box would be covered by other information, so the results box has been moved to below the book details
1 parent d33e543 commit 503889c

1 file changed

Lines changed: 52 additions & 29 deletions

File tree

goodreads-libby.user.js

Lines changed: 52 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// @grant GM.getValue
1212
// @license MIT
1313
// ==/UserScript==
14-
14+
window.addEventListener('load', function() {
1515
(function () {
1616
"use strict";
1717

@@ -41,37 +41,26 @@
4141
return libbySyncButton;
4242
};
4343

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-
};
44+
const createGoodreadsResults = async () => {
45+
let builderDiv = document.createElement("div");
6046

61-
const addGoodreadsResults = async () => {
62-
let bookTitle = document.getElementById("bookTitle").innerHTML.trim();
63-
let bookAuthor = document
64-
.getElementsByClassName("authorName")[0]
65-
.firstChild.innerHTML.trim();
47+
let bookTitle = document.querySelector("[data-testid='bookTitle']").innerHTML.trim();
48+
let bookAuthor = document.querySelector("[data-testid='name']").innerHTML.trim();
6649
let searchString = encodeURIComponent(`${bookTitle} ${bookAuthor}`);
6750
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;
72-
background-color: #ececec;
73-
border: 1px solid black;
74-
padding: 1em;"><h3>Libby results</h3><div id="libby-results"></div></div>`;
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();
62+
63+
let goodreadsResults = builderDiv.firstChild;
7564

7665
if (libraries.length === 0) {
7766
document.getElementById(
@@ -90,6 +79,39 @@
9079
).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>`;
9180
});
9281
});
82+
83+
return goodreadsResults
84+
};
85+
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+
};
102+
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+
}
93115
};
94116

95117
if (unsafeWindow.location.host == "libbyapp.com") {
@@ -98,3 +120,4 @@
98120
addGoodreadsResults();
99121
}
100122
})();
123+
}, false);

0 commit comments

Comments
 (0)