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