Goodreads to StoryGraph

User script to add a link from every book page on Goodreads to StoryGraph

  1. // ==UserScript==
  2. // @name Goodreads to StoryGraph
  3. // @namespace https://github.com/ar3h1d/Goodreads-to-StoryGraph
  4. // @version 0.1
  5. // @description User script to add a link from every book page on Goodreads to StoryGraph
  6. // @author ar3h1d
  7. // @match https://www.goodreads.com/book/*
  8. // @grant none
  9. // @icon https://raw.githubusercontent.com/ar3h1d/Goodreads-to-StoryGraph/icon.png
  10. // @license GPL3
  11. // ==/UserScript==
  12.  
  13. function getElementByXpath(path) {
  14. // Find an element in page with Xpath
  15. return document.evaluate(path, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
  16. }
  17.  
  18. function createButton(storygraphLink){
  19. // Create the button element
  20. const storygraphButton = document.createElement('button');
  21. storygraphButton.className = 'Button Button--secondary Button--medium';
  22. storygraphButton.href = storygraphLink;
  23. storygraphButton.textContent = 'StoryGraph';
  24. storygraphButton.target = '_blank';
  25. storygraphButton.style.marginLeft = '10px';
  26. storygraphButton.style.marginRight = '15px';
  27. //storygraphButton.style.border = '1px solid';
  28. //storygraphButton.style.color = '#d24040';
  29.  
  30. // Add an onclick attribute that calls a function to redirect the user
  31. storygraphButton.onclick = function () {
  32. window.open(storygraphLink, '_blank');
  33. };
  34.  
  35. // Find the TMDb button
  36. const shareButton = document.querySelector("#__next > div.PageFrame.PageFrame--siteHeaderBanner > main > div.BookPage__gridContainer > div.BookPage__rightColumn > div.BookPage__mainContent > div.BookPageTitleSection > div.BookPageTitleSection__share > div > button");
  37. if (shareButton) {
  38. // Insert the 30nama button after the All Topics button
  39. shareButton.parentNode.insertBefore(storygraphButton, shareButton.nextSibling);
  40. }
  41. }
  42.  
  43. (function () {
  44. 'use strict';
  45.  
  46. // Extract movie title from the URL
  47. //const ISBNNode = getElementByXpath("//*[@id=\"__next\"]/div[2]/main/div[1]/div[2]/div[2]/div[2]/div[6]/div/span[2]/div[1]/span/div/dl/div[3]/dd/div/div[1]");
  48. //const ISBN = ISBNNode ? ISBNNode.textContent : null;
  49. let scriptTag = document.querySelector('script[type="application/ld+json"]');
  50. let jsonContent = JSON.parse(scriptTag.textContent);
  51. let ISBN = jsonContent.isbn;
  52.  
  53. let bookURL = document.URL.split("/");
  54. let bookParts = bookURL[bookURL.length - 1].split("-");
  55. bookParts.shift();
  56. let bookTitle = bookParts.join(" ");
  57.  
  58. if (ISBN) {
  59. //try{
  60. // Split the text by space and take the first part, which is the ISBN13
  61. // ISBN = ISBN.split(' ')[0];
  62. //} catch (error) {console.error;}
  63. // Create the storygraph link with ISBN in the page
  64. const storygraphLink = `https://app.thestorygraph.com/browse?search_term=${ISBN}`;
  65. createButton(storygraphLink);
  66. }
  67. else {
  68. // Create the storygraph link with Book Title in URL
  69. const storygraphLink = `https://app.thestorygraph.com/browse?search_term=${bookTitle}`;
  70. createButton(storygraphLink);
  71. }
  72.  
  73. })();
  74.  
  75. //<button type="button" class="Button Button--transparent Button--small Button--rounded" aria-label="Share"><span class="Button__labelItem"><i class="Icon ShareIcon">