Goodreads Plus

Add "Search MAM" button to Goodreads

スクリプトをインストールするには、Tampermonkey, GreasemonkeyViolentmonkey のような拡張機能のインストールが必要です。

You will need to install an extension such as Tampermonkey or Violentmonkey to install this script.

スクリプトをインストールするには、TampermonkeyViolentmonkey のような拡張機能のインストールが必要です。

スクリプトをインストールするには、TampermonkeyUserscripts のような拡張機能のインストールが必要です。

このスクリプトをインストールするには、Tampermonkeyなどの拡張機能をインストールする必要があります。

このスクリプトをインストールするには、ユーザースクリプト管理ツールの拡張機能をインストールする必要があります。

(ユーザースクリプト管理ツールは設定済みなのでインストール!)

このスタイルをインストールするには、Stylusなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus などの拡張機能をインストールする必要があります。

このスタイルをインストールするには、Stylus tなどの拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

このスタイルをインストールするには、ユーザースタイル管理用の拡張機能をインストールする必要があります。

(ユーザースタイル管理ツールは設定済みなのでインストール!)

このスクリプトの質問や評価の投稿はこちら通報はこちらへお寄せください
// ==UserScript==
// @name         Goodreads Plus
// @namespace    https://greasyfork.org/en/users/78880
// @version      0.3.14
// @description  Add "Search MAM" button to Goodreads
// @author       asdaa
// @match        https://www.goodreads.com/*
// @grant        none
// @license      MIT
// ==/UserScript==

window.addEventListener("load", Greasemonkey_main, false);
 
function Greasemonkey_main() {
 
    console.log("[G+] Tweaking Goodreads...");
 
    var page = (window.location.pathname.match(/(book|review)(?=\/show\/)/) || [])[0];
    var mamSearchUrl = "";
    var buttonBar = "";
    var mamButton = "";
    var buttonUl = "";
    var bookTitle = "";
 
    if(page === 'book'){
        console.log("[G+] We got a book URL");

        // Try different ways to grab book title
        try {
            bookTitle = document.getElementsByClassName("Text__title1")[0].innerHTML;
        }
        catch {
            bookTitle = document.getElementById("bookTitle").innerText;
        }
        finally {
            console.log("[G+] Book title: " + bookTitle);
            bookTitle = bookTitle.replace('&', '%26');
            bookTitle = bookTitle.replace('&', '%26');
        }
        
        mamSearchUrl = "https://www.myanonamouse.net/tor/browse.php?tor[text]=" + bookTitle;
 
        // Add 'Search MAM' button
        // Old or new layout?
        var old = document.getElementById("buyButtonContainer");
        if(old != null) {
            console.log("[G+] Old layout");
            buttonBar = document.getElementById("buyButtonContainer");
            if (buttonBar === null || buttonBar == "null") {
                buttonBar = document.getElementById("asyncBuyButtonContainer");
            }
            buttonUl = buttonBar.getElementsByTagName("ul");
            mamButton = document.createElement("li");
            mamButton.innerHTML = '<a id="mamLink" href="' + mamSearchUrl + '" target="_blank" class="buttonBar">Search MAM</a>';
            mamButton.className = "Button";
            buttonUl[0].appendChild(mamButton);
            console.log("[G+] 'Search MAM' button added!");
        }
        else {
            console.log("[G+] New layout");
            buttonBar = document.getElementsByClassName("BookActions")[0];
            mamButton = document.getElementsByClassName("BookActions__button")[0].cloneNode(true);
            mamButton.innerHTML = '<div class="BookActions__button"><a href="' + mamSearchUrl + '">' +
                '<div class="Button__container Button__container--block">' +
                '<button type="button" class="Button Button--secondary Button--medium Button--block">' +
                '<span class="Button__labelItem" style="text-decoration=none">Search MAM</span></button></div></a></div>';
     
            buttonBar.appendChild(mamButton)
            console.log("[G+] 'Search MAM' button added!");
        }
    } else if(page === 'review'){
        var bookList = document.querySelectorAll('#booksBody .title div a');
        // Loop over all the books
        for(var i=0; i<bookList.length; i++){
            bookTitle = getBookTitle(bookList[i]);
            bookTitle = bookTitle.replace('&amp;', '%26');
            bookTitle = bookTitle.replace('&', '%26');
            mamSearchUrl = "https://www.myanonamouse.net/tor/browse.php?tor[text]=" + bookTitle;
            // Add 'Search MAM' button
            var newLink = document.createElement('a');
            var linkText = document.createTextNode('[Search MAM]');
            newLink.appendChild(linkText);
            newLink.setAttribute('href',mamSearchUrl);
            newLink.setAttribute('style','color:#b3b3b3;font-style:italic');
            bookList[i].parentNode.parentNode.appendChild(newLink);
        }
        console.log("[G+] 'Search MAM' buttons added!");
    }
 
}
 
// Grab book title (and only title) from the element
function getBookTitle(el){
	var bookTitle = el.innerHTML.trim().split('<', 1)+'';
	console.log("Book title: " + bookTitle.trim());
	return bookTitle.trim();
}