|
11 | 11 | // @grant GM.getValue |
12 | 12 | // @license MIT |
13 | 13 | // ==/UserScript== |
14 | | - |
| 14 | +window.addEventListener('load', function() { |
15 | 15 | (function () { |
16 | 16 | "use strict"; |
17 | 17 |
|
|
41 | 41 | return libbySyncButton; |
42 | 42 | }; |
43 | 43 |
|
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"); |
60 | 46 |
|
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(); |
66 | 49 | let searchString = encodeURIComponent(`${bookTitle} ${bookAuthor}`); |
67 | 50 | 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; |
75 | 64 |
|
76 | 65 | if (libraries.length === 0) { |
77 | 66 | document.getElementById( |
|
90 | 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>`; |
91 | 80 | }); |
92 | 81 | }); |
| 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 | + } |
93 | 115 | }; |
94 | 116 |
|
95 | 117 | if (unsafeWindow.location.host == "libbyapp.com") { |
|
98 | 120 | addGoodreadsResults(); |
99 | 121 | } |
100 | 122 | })(); |
| 123 | +}, false); |
0 commit comments