|
1 | 1 | // ==UserScript== |
2 | 2 | // @name Goodreads Libby Results |
3 | 3 | // @namespace https://github.com/Dylancyclone/goodreads-libby-userscript |
4 | | -// @version 1.0.0 |
| 4 | +// @version 1.1.0 |
5 | 5 | // @description Searches for the book you are looking at on Goodreads across all your libby libraries |
6 | 6 | // @author Dylancyclone |
7 | 7 | // @match https://libbyapp.com/interview/menu |
|
11 | 11 | // @grant GM.getValue |
12 | 12 | // @license MIT |
13 | 13 | // ==/UserScript== |
| 14 | +window.addEventListener( |
| 15 | + "load", |
| 16 | + function () { |
| 17 | + ;(function () { |
| 18 | + "use strict" |
14 | 19 |
|
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 | + } |
17 | 31 |
|
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 = ` |
33 | 35 | <div class="menu-library-buttons"> |
34 | 36 | <button class="menu-library-buttons-add-library halo" role="button" type="button"> |
35 | 37 | <span role="text">Save Libraries (userscript)</span> |
36 | 38 | </button> |
37 | 39 | </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 | + } |
43 | 45 |
|
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") |
60 | 48 |
|
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=" |
72 | 59 | background-color: #ececec; |
73 | 60 | 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 | + } |
75 | 109 |
|
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 | + } |
81 | 126 |
|
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 | + } |
94 | 140 |
|
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 | +) |
0 commit comments