goodreads-morawa/goodreads-morawa.user.js
Kumi ddd3005859
feat: add script to replace Amazon button with Morawa
Introduced a UserScript that replaces the "Buy on Amazon" button with a "Buy at Morawa" button on Goodreads book pages. This script fetches the ISBN of the book and links to Morawa.at using the ISBN.

Added LICENSE file with MIT license and created a README that includes installation and usage instructions.
2024-07-06 14:33:53 +02:00

69 lines
No EOL
2.6 KiB
JavaScript

// ==UserScript==
// @name Replace Amazon Button with Morawa Button on Goodreads
// @namespace http://tampermonkey.net/
// @version 0.3
// @author Kumi
// @description Replace the "Buy on Amazon" button with a "Buy at Morawa" button on Goodreads book pages.
// @match https://www.goodreads.com/book/show/*
// @grant none
// ==/UserScript==
(function () {
'use strict';
// Function to get the ISBN from the Goodreads page
function getISBN() {
const scriptTags = document.querySelectorAll('script[type="application/ld+json"]');
for (let scriptTag of scriptTags) {
try {
const jsonData = JSON.parse(scriptTag.textContent);
if (jsonData['@type'] === 'Book' && jsonData.isbn) {
return jsonData.isbn;
}
} catch (e) {
console.error('Error parsing JSON from script tag:', e);
}
}
return null;
}
// Function to create the Morawa button
function createMorawaButton(isbn) {
const morawaButton = document.createElement('a');
morawaButton.textContent = 'Buy at Morawa';
morawaButton.href = `https://www.morawa.at/detail/ISBN-${isbn}`;
morawaButton.className = 'Button Button--buy Button--medium Button--block';
morawaButton.setAttribute('role', 'link');
morawaButton.setAttribute('aria-label', 'Buy at Morawa, link, opens in new tab');
morawaButton.style.textDecoration = 'none';
morawaButton.style.color = '#fff';
morawaButton.style.backgroundColor = '#ff9900';
morawaButton.style.padding = '10px';
morawaButton.style.borderRadius = '5px';
return morawaButton;
}
// Main function to replace the Amazon button
function replaceAmazonButton() {
const isbn = getISBN();
if (!isbn) {
console.log('ISBN not found.');
return;
}
const amazonButtonContainer = Array.from(document.querySelectorAll('.Button__container')).find(container => {
const button = container.querySelector('button');
return button && button.textContent.includes('Buy on Amazon');
});
if (amazonButtonContainer) {
const morawaButton = createMorawaButton(isbn);
amazonButtonContainer.parentNode.replaceChild(morawaButton, amazonButtonContainer);
} else {
console.log('Amazon button not found.');
}
}
// Wait for the DOM to load before running the script
window.addEventListener('load', replaceAmazonButton);
})();