Skip to content

Commit

Permalink
修正许多书改版失效的问题
Browse files Browse the repository at this point in the history
  • Loading branch information
ywzhaiqi committed Sep 11, 2015
1 parent e61dec4 commit 0167b18
Showing 1 changed file with 47 additions and 30 deletions.
77 changes: 47 additions & 30 deletions kindle人自动下载.user.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@
// @name kindle人自动下载
// @namespace https://github.com/ywzhaiqi
// @author ywzhaiqi
// @version 0.1
// @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_addStyle
// @grant GM_download
// @grant GM_xmlhttpRequest
// @run-at document-end
// @noframes
// ==/UserScript==
Expand Down Expand Up @@ -38,6 +37,19 @@ function getParam(name, url) {
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)
Expand All @@ -56,16 +68,16 @@ var ns = {
if (btns.size() == 1)
return btns[0];

var buyAzw3 = btns.filter(function() {
return checkNodeIsAzw3.call(this, '.locked');
})[0];

// if (!buyAzw3)
// alert('不存在 azw3');

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];
Expand Down Expand Up @@ -93,7 +105,7 @@ var ns = {
addDownBtn: function() {
$('<button>')
.text('下载')
.css('cssText', 'position: fixed; right: 50%; top: 58%; margin-right: -365px; z-index: 200;')
.css('cssText', 'position: fixed; right: 50%; top: 52%; margin-right: -365px; z-index: 200;')
.insertAfter('#side_reply')
.click(function() {
ns.autoBuy();
Expand All @@ -110,7 +122,7 @@ var ns = {
if (lockedReply.size()) {
lockedReply.click();
waitForSelector('#postmessage', function() {
jQuery('#postmessage').val('感谢分享');
jQuery('#postmessage').val('感谢分享,谢谢!!!');
jQuery('#postsubmit').click();

waitFor(ns.getDownBtn, ns.autoDownload)
Expand All @@ -125,26 +137,31 @@ var ns = {
})
}
},
autoDownload: function () { // 自动点击许多书按钮下载
var xuduoshuViewBtn = ns.getDownBtn();
// xuduoshuViewBtn.removeAttribute('onclick');

var downUrl = xuduoshuViewBtn.getAttribute('url').replace('/view/', '/download/');
// location.href = downUrl.replace('http://', 'idm://');
// window.close();
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
// alert('已下载')

// xuduoshuViewBtn.click();

// // 找到 azw3 的条目
// .click();

// waitForSelector('iframe[src^="http://xuduoshu.com/ajax_view"]', function() {
// // 在 iframe 里面,跨域无权访问 document
// console.log('下载面板已经弹出');
// });
}
};

Expand Down

0 comments on commit 0167b18

Please sign in to comment.