Greasy Fork 支持简体中文。

BachNgocSach Leecher

Tải truyện từ bachngocsach.com định dạng html. Sau đó, bạn có thể dùng Mobipocket Creator để tạo ebook prc

  1. // ==UserScript==
  2. // @name BachNgocSach Leecher
  3. // @namespace https://holy-donkey.github.io
  4. // @description Tải truyện từ bachngocsach.com định dạng html. Sau đó, bạn có thể dùng Mobipocket Creator để tạo ebook prc
  5. // @version 0.1.5
  6. // @icon http://i.imgur.com/3lomxTC.png
  7. // @author The Holy Donkey (Thánh Lư Đại Nhân)
  8. // @license WTFPL
  9. // @include /^https?:\/\/bachngocsach\.com\/reader\/[^\/]+$/
  10. // @require https://cdn.jsdelivr.net/jquery/2.2.4/jquery.min.js
  11. // @require https://cdn.jsdelivr.net/filesaver.js/1.3.3/FileSaver.min.js
  12. // @noframes
  13. // @connect self
  14. // @supportURL https://github.com/holy-donkey/UserScripts/issues
  15. // @run-at document-idle
  16. // @grant GM_xmlhttpRequest
  17. // ==/UserScript==
  18.  
  19. (function ($, window, document, undefined) {
  20. 'use strict';
  21.  
  22. function downloadFail() {
  23. $download.css({'background': '#e05d59', 'border-color': '#c83e35'});
  24. titleError.push(title);
  25.  
  26. if (debug) console.log('%cError: ' + url, 'color:red;');
  27. }
  28.  
  29. function getContent() {
  30. GM_xmlhttpRequest({
  31. method: 'GET',
  32. url: url,
  33. onload: function (response) {
  34. var $data = $(response.responseText),
  35. $chapter = $data.find('#noidung'),
  36. $next = $data.find('.page-next.chuong-button:has("span"):first');
  37.  
  38. title = $data.find('#chuong-title').text().trim();
  39. if (count === 0) begin = title;
  40. end = title;
  41.  
  42. $download.html(title);
  43.  
  44. if (!$chapter.length) {
  45. downloadFail();
  46. } else {
  47. $download.css('background', 'orange');
  48.  
  49. txt += '<h2 class="title">' + title + '</h2>' + $chapter.html();
  50. count++;
  51.  
  52. if (debug) console.log('%cComplete: ' + url, 'color:green;');
  53. }
  54.  
  55. document.title = '[' + count + '] ' + pageName;
  56.  
  57. if (!$next.text().length) {
  58. var fileName = location.pathname.slice(8) + '.html',
  59. blob;
  60.  
  61. if (titleError.length) {
  62. titleError = '<h4>Các chương lỗi: <font color="gray">' + titleError.join(', ') + '</font></h4>';
  63. if (debug) console.log('Các chương lỗi:', titleError);
  64. } else {
  65. titleError = '';
  66. }
  67.  
  68. txt = '<html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><h1><font color="red">' + $('h1').text().trim() + '</font></h1><h3>Tác giả: <font color="blue">' + $('div#tacgia').find('a').text().trim() + '</font></h3><h3>Thể loại: <font color="green">' + $('div#theloai').find('a').text().trim() + '</font></h3><br><h4>Từ <font color="gray">' + begin + '</font> đến <font color="gray">' + end + '</font></h4>' + titleError + '<br><br>' + credits + '<br><br><br>' + txt + '</body></html>';
  69.  
  70. blob = new Blob([txt], {
  71. type: 'text/html'
  72. });
  73.  
  74. $download.attr({
  75. href: window.URL.createObjectURL(blob),
  76. download: fileName
  77. }).html('✓ Tải xong').css({'background': '#d0ead1', 'border-color': '#abceb7'}).off('click');
  78.  
  79. saveAs(blob, fileName);
  80.  
  81. $(window).off('beforeunload');
  82. if (debug) console.log('%cDownload Finished!', 'color:blue;');
  83. document.title = '[⇓] ' + pageName;
  84.  
  85. return;
  86. }
  87.  
  88. url = $next.attr('href');
  89. getContent();
  90. },
  91. onerror: function (err) {
  92. downloadFail();
  93.  
  94. setTimeout(function () {
  95. getContent();
  96. }, 3000);
  97. }
  98. });
  99. }
  100.  
  101.  
  102. var pageName = document.title,
  103. $download = $('<a>', {
  104. class: 'truyen-button',
  105. href: '#download',
  106. css: {
  107. background: '#f4b759',
  108. color: '#ffffff !important',
  109. border: '1px solid #eb813d'
  110. },
  111. text: 'Tải xuống'
  112. }),
  113. disableClick = false,
  114.  
  115. count = 0,
  116. begin = '',
  117. end = '',
  118.  
  119. txt = '',
  120. url = $('.truyen-button:contains("Đọc từ đầu")').attr('href'),
  121.  
  122. title = '',
  123. titleError = [],
  124.  
  125. credits = '<p>Truyện được tải từ <a href="' + location.href + '">BachNgocSach</a></p><p>Userscript được viết bởi: Thánh Lư Đại Nhân - https://holy-donkey.github.io</p>',
  126.  
  127. debug = false;
  128.  
  129.  
  130. $('nav#truyen-nav:last').append($download);
  131.  
  132. $download.on('click', function (e) {
  133. e.preventDefault();
  134. if (disableClick) return;
  135. disableClick = true;
  136.  
  137. getContent();
  138.  
  139. $(window).on('beforeunload', function () {
  140. return 'Truyện đang được tải xuống...';
  141. });
  142. });
  143.  
  144. })(jQuery, window, document);