69 lines
2.6 KiB
JavaScript
69 lines
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);
|
||
|
})();
|