commit ddd30058597f9a94b100f9c3b3881d62bf9fe1be Author: Kumi Date: Sat Jul 6 14:33:53 2024 +0200 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. diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..86e5988 --- /dev/null +++ b/LICENSE @@ -0,0 +1,19 @@ +Copyright (c) 2024 Kumi + +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. diff --git a/README.md b/README.md new file mode 100644 index 0000000..65415a4 --- /dev/null +++ b/README.md @@ -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. diff --git a/goodreads-morawa.user.js b/goodreads-morawa.user.js new file mode 100644 index 0000000..22ec5f7 --- /dev/null +++ b/goodreads-morawa.user.js @@ -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); +})(); \ No newline at end of file