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.
This commit is contained in:
Kumi 2024-07-06 14:33:53 +02:00
commit ddd3005859
Signed by: kumi
GPG key ID: ECBCC9082395383F
3 changed files with 111 additions and 0 deletions

19
LICENSE Normal file
View file

@ -0,0 +1,19 @@
Copyright (c) 2024 Kumi <goodreads-morawa@kumi.email>
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

23
README.md Normal file
View file

@ -0,0 +1,23 @@
# goodreads-morawa
[![Support Private.coffee!](https://shields.private.coffee/badge/private.coffee-support%20us!-pink?logo=coffeescript)](https://private.coffee)
[![Latest Git Commit](https://shields.private.coffee/gitea/last-commit/kumi/goodreads-morawa?gitea_url=https://git.private.coffee)](https://git.private.coffee/kumi/goodreads-morawa)
This is a UserScript that adds a link to the Morawa.at website on the Goodreads book page.
## Installation
1. Install a UserScript manager for your browser. For example, [Tampermonkey](https://www.tampermonkey.net/) or [Greasemonkey](https://www.greasespot.net/).
2. Click on the `goodreads-morawa.user.js` file in this repository.
3. Click on the `Raw` button.
4. The UserScript manager should ask you to install the script.
## Usage
1. Go to a book page on Goodreads.
2. Click on the "Buy at Morawa" button where the Amazon button used to be.
3. Enjoy!
## License
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details.

69
goodreads-morawa.user.js Normal file
View file

@ -0,0 +1,69 @@
// ==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);
})();