This repository has been archived by the owner on Aug 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
douban2readfree.user.js
91 lines (84 loc) · 3 KB
/
douban2readfree.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// ==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();