forked from ywzhaiqi/userscript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kindle人自动下载.user.js
168 lines (152 loc) · 5.17 KB
/
kindle人自动下载.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// ==UserScript==
// @name kindle人自动下载
// @namespace https://github.com/ywzhaiqi
// @author ywzhaiqi
// @version 0.2
// @description kindleren 帖子增加 "下载" 按钮,点击后自动回复或购买,然后下载
// @include http://kindleren.com/forum.php?mod=viewthread&tid=*
// @include http://kandouren.com/forum.php?mod=viewthread&tid=*
// @include http://kandouren.com/thread-*-1-1.html
// @grant GM_xmlhttpRequest
// @run-at document-end
// @noframes
// ==/UserScript==
if (typeof unsafeWindow !== 'undefined') {
var $ = jQuery = unsafeWindow.jQuery;
}
function waitFor(testFx, callback) {
var timeId = setInterval(function() {
if (testFx()) {
clearInterval(timeId);
callback();
}
}, 500);
}
function waitForSelector(selector, then) {
waitFor(function _check() {
return document.querySelector(selector);
}, then);
}
function getParam(name, url) {
var regexp = new RegExp("(?:^|\\?|#|&)" + name + "=([^&#]*)(?:$|&|#)", "i"),
matches = regexp.exec(url || location.href);
return matches ? decodeURIComponent(matches[1]) : ""
}
function parseHTML(html) {
var doc;
try {
// firefox and chrome 30+,Opera 12 会报错
doc = new DOMParser().parseFromString(html, 'text/html');
} catch (ex) {}
if (!doc) {
doc = document.implementation.createHTMLDocument("");
doc.querySelector('html').innerHTML = html;
}
return doc;
}
function checkNodeIsAzw3(selector) {
var div = jQuery(this).closest(selector);
if (div.prev().text().indexOf('.azw3') != -1)
return true;
if (div[0] && div[0].previousSibling.textContent.indexOf('.azw3') != -1)
return true;
var node = div.prev()[0] && div.prev()[0].previousSibling;
if (node)
return node.textContent.indexOf('.azw3') > 0;
}
var ns = {
get buyBtn() {
var btns = jQuery('.viewpay');
if (btns.size() == 1)
return btns[0];
var buyAzw3 = btns.filter(function() { return checkNodeIsAzw3.call(this, '.locked'); })[0];
return buyAzw3;
},
getDownBtn: function () { // 点击查看按钮,点击后弹出许多盘下载界面
var link = jQuery('.showhide a').filter(function() { return jQuery(this).text().match(/\.azw3$/) });
if (link.size()) {
ns.autoDownload(link.href);
return;
}
var btns = jQuery('.xuduoshu_ajaxview');
if (btns.size() == 1)
return btns[0];
return btns.filter(function() {
return checkNodeIsAzw3.call(this, '.showhide');
})[0];
},
getXuDownBtn: function() { // 许多书下载界面按钮
return jQuery('a[href^="/download/"]')[0];
},
init: function() {
if (location.href.indexOf('autosave') > 0) {
switch(location.host) {
case 'kindleren.com':
case 'kandouren.com':
ns.autoBuy();
break;
}
} else {
this.addDownBtn();
}
},
addDownBtn: function() {
$('<button>')
.text('下载')
.css('cssText', 'position: fixed; right: 50%; top: 52%; margin-right: -365px; z-index: 200;')
.insertAfter('#side_reply')
.click(function() {
ns.autoBuy();
});
},
autoBuy: function () {
if (ns.getDownBtn()) {
ns.autoDownload();
return;
}
// 检查是否有回复
var lockedReply = jQuery('.locked a:contains(回复)');
if (lockedReply.size()) {
lockedReply.click();
waitForSelector('#postmessage', function() {
jQuery('#postmessage').val('感谢分享,谢谢!!!');
jQuery('#postsubmit').click();
waitFor(ns.getDownBtn, ns.autoDownload)
});
} else {
ns.buyBtn.click();
var paySubmit = '#payform #paysubmit';
waitForSelector(paySubmit, function() {
jQuery(paySubmit).click();
waitFor(ns.getDownBtn, ns.autoDownload);
})
}
},
autoDownload: function (downUrl) { // 自动点击许多书按钮下载
if (downUrl) {
ns.download(downUrl);
} else {
var xuduoshuViewBtn = ns.getDownBtn();
var viewUrl = xuduoshuViewBtn.getAttribute('url').replace("view","ajax_view");
ns.downFromXuDuoShu(viewUrl);
}
},
downFromXuDuoShu: function(viewUrl) {
console.log('GM_xmlhttpRequest ', viewUrl)
GM_xmlhttpRequest({
method: "GET",
url: viewUrl,
onload: function(response) {
var doc = parseHTML(response.responseText);
var link = doc.getElementById('download_file_link');
var dUrl = 'http://xuduoshu.com' + link.getAttribute('href');
ns.download(dUrl);
}
});
},
download: function(downUrl) {
location.href = downUrl;
document.title = '【已下载】' + document.title
}
};
ns.init();