Greasy Fork

来自缓存

Greasy Fork is available in English.

豆瓣 ReadFree 传送门

在“豆瓣读书”页面增加到ReadFree电子书的传送门

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name        豆瓣 ReadFree 传送门
// @namespace   https://github.com/JiajunW/douban2readfree
// @description 在“豆瓣读书”页面增加到ReadFree电子书的传送门
// @icon        https://raw.githubusercontent.com/JiajunW/douban2readfree/master/res/icon.png
// @include     https://book.douban.com/*
// @version     2.0.2
// @resource    custom_css https://raw.githubusercontent.com/JiajunW/douban2readfree/master/style/style.css
// @grant       GM_xmlhttpRequest
// @grant       GM_addStyle
// @grant       GM_getResourceText
// @connect     readfree.me
// ==/UserScript==

function get_book_id(url) {
    let re = /\/subject\/(\d+)(\/?$|\/.icn=(index-topchart-subject|index-book250-subject|index-editionrecommend)$)/g;
    let matches = re.exec(url);
    if (matches && matches.length > 1) {
        return parseInt(matches[1]);
    }
}

function add_readfree_style() {
    GM_addStyle(GM_getResourceText("custom_css"));
}

function dom(tag, attr, inner) {
    let el = document.createElement(tag);
    for (let key in attr) {
        if (attr.hasOwnProperty(key)) {
            el.setAttribute(key,attr[key]);
        }
    }
    if (inner) {
        el.innerHTML = inner;
    }
    return el;
}

function add_link_to_book_info () {
    let path = document.location.pathname;
    let id = get_book_id(path);
    if (id) {
        let rf_url = 'http://readfree.me/book/' + id;
        GM_xmlhttpRequest({
            method: "HEAD",
            url: rf_url,
            onload: function (response) {
                if (response.status === 200) {
                    let panel = dom('div', {id: 'readfree-link'});
                    let ahref = dom('a', { class: 'readfree-link book_info-link', href: rf_url, target: '_blank'}, 'ReadFree!');
                    panel.appendChild(ahref);
                    document.body.appendChild(panel);
                }
            }
        })
    }
}

function add_links_to_all_books() {
    let my_a = document.querySelectorAll("a");
    for (let i = 0; i < my_a.length; i++) {
        let this_a = my_a[i];
        if (this_a.children[0] === undefined || this_a.children[0].nodeName !== "IMG"){
            if (this_a.href.indexOf("subject")) {
                let id = get_book_id(this_a.href);
                if (id) {
                    let rf_url = 'http://readfree.me/book/' + id;
                    GM_xmlhttpRequest({
                        method: "HEAD",
                        url: rf_url,
                        onload: function (response) {
                            if (response.status === 200) {
                                let ahref = dom('a', {class: 'readfree-link all_books_link', href: rf_url, target: '_blank'}, 'ReadFree!');
                                this_a.parentNode.insertBefore(ahref, this_a.nextSibling);
                            }
                        }
                    })
                }
            }
        }
    }
}

function main(){
    add_readfree_style();
    add_link_to_book_info();
    add_links_to_all_books();
}

main();