|
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('load', function() { |
15 | | -(function () { |
16 | | - "use strict"; |
| 14 | +window.addEventListener( |
| 15 | + "load", |
| 16 | + function () { |
| 17 | + ;(function () { |
| 18 | + "use strict" |
17 | 19 |
|
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 | + } |
29 | 31 |
|
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 | | - const createGoodreadsResults = async () => { |
45 | | - let builderDiv = document.createElement("div"); |
| 46 | + const createGoodreadsResults = async () => { |
| 47 | + let builderDiv = document.createElement("div") |
46 | 48 |
|
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() |
62 | 73 |
|
63 | | - let goodreadsResults = builderDiv.firstChild; |
| 74 | + let goodreadsResults = builderDiv.firstChild |
64 | 75 |
|
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 | + } |
70 | 81 |
|
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 | + }) |
82 | 106 |
|
83 | | - return goodreadsResults |
84 | | - }; |
| 107 | + return goodreadsResults |
| 108 | + } |
85 | 109 |
|
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 | + } |
102 | 126 |
|
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 | + } |
116 | 140 |
|
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 | +) |
0 commit comments