Advertisement
Guest User

Untitled

a guest
Apr 1st, 2020
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.72 KB | None | 0 0
  1. // ==UserScript==
  2. // @name GBookDown - Download Google Books
  3. // @description Saves all available Preview pages from a Google Book as PNGs
  4. // @author nikisby
  5. // @namespace nikisby
  6. // @include https://books.google.*/books*
  7. // @version 1.2
  8. // @require https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.0/jquery.min.js
  9. // @require https://cdnjs.cloudflare.com/ajax/libs/jszip/2.5.0/jszip.min.js
  10. // @require https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.3.8/FileSaver.min.js
  11. // @grant GM_xmlhttpRequest
  12. // ==/UserScript==
  13.  
  14. this.$ = this.jQuery = jQuery.noConflict(true);
  15.  
  16. (function () {
  17. var css = [
  18. '#savePNG {',
  19. ' position: fixed;',
  20. ' padding: 7px;',
  21. ' background-color: #F8EEB1;',
  22. ' border: 2px solid #333;',
  23. ' border-radius: 6px;',
  24. ' z-index: 9999;',
  25. ' font-size: 18px;',
  26. ' right: 30px;',
  27. ' bottom: 20px;',
  28. ' color: #000;',
  29. ' width: 650px;',
  30. ' text-align: center;',
  31. ' box-shadow: 0 1px 4px rgba(0, 0, 0, 0.5), 0 0 15px rgba(0, 0, 0, 0.3);',
  32. '}',
  33. '#savePNG button {',
  34. ' font-size: 18px;',
  35. ' width: 100px;',
  36. ' cursor: pointer;',
  37. '}',
  38. '#savePNG input {',
  39. ' width: 60px;',
  40. ' font-size: 18px;',
  41. ' text-align: center;',
  42. '}'
  43. ].join('\n');
  44. if (typeof GM_addStyle != 'undefined') {
  45. GM_addStyle(css);
  46. } else if (typeof PRO_addStyle != 'undefined') {
  47. PRO_addStyle(css);
  48. } else if (typeof addStyle != 'undefined') {
  49. addStyle(css);
  50. } else {
  51. var node = document.createElement('style');
  52. node.type = 'text/css';
  53. node.appendChild(document.createTextNode(css));
  54. var heads = document.getElementsByTagName('head');
  55. if (heads.length > 0) {
  56. heads[0].appendChild(node);
  57. } else {
  58. // no head yet, stick it whereever
  59. document.documentElement.appendChild(node);
  60. }
  61. }
  62. }) ();
  63.  
  64. var html = '<div id="savePNG">' +
  65. '<span>There are max </span> ' +
  66. '<input type="text" id="total" value="---" readonly> ' +
  67. '<span>pages available. Input the page range to download </span> ' +
  68. '<input type="text" id="count" value="0"> ' +
  69. '<input type="text" id="max" value="1"> ' +
  70. '<span>.</span> ' +
  71. '<button>Start</button>' +
  72. '</div>';
  73.  
  74. $('body').prepend(html);
  75.  
  76. $('#count').select();
  77.  
  78. var title, id, zipfile, zipname;
  79. var zipflag = false;
  80. var next = true;
  81. var zip = new JSZip();
  82. var pids = [];
  83.  
  84. $('#savePNG button').click(function(){
  85. doTheMagic();
  86. })
  87.  
  88. $("#count").keyup(function(event){
  89. if(event.keyCode == 13){
  90. $("#savePNG button").click();
  91. }
  92. });
  93.  
  94. function addIMG(url, name, count) {
  95. var filename = name + '.png';
  96. GM_xmlhttpRequest({
  97. method: 'GET',
  98. synchronous: true,
  99. url: url,
  100. overrideMimeType: 'image/png; charset=x-user-defined',
  101. onload: function (response) {
  102. $('#savePNG button').prop('disabled', true).text('Working…');
  103. $('#count').val(count);
  104. $('#count').css('font-weight', 'normal').css('text-decoration', 'none');
  105. zip.file(filename, response.responseText, {
  106. binary: true
  107. });
  108. }
  109. });
  110. }
  111.  
  112. function saveZip(count, number) {
  113. zipname = title + '_' + id + '_' +
  114. pad(count, number.toString().length) +
  115. '_' + number + '.zip';
  116. zipfile = zip.generate({
  117. type: 'blob'
  118. });
  119. $('#savePNG button').prop('disabled', false).text('Start');
  120. saveAs(zipfile, zipname);
  121. }
  122.  
  123. function pad(num, size) {
  124. var s = "000000000" + num;
  125. return s.substr(s.length-size);
  126. }
  127.  
  128. function doTheMagic() {
  129. $('#savePNG button').prop('disabled', true);
  130. var href = window.location.href;
  131. var domain = href.split('/') [2];
  132.  
  133. id = href.match(/id=([^&]+)/) [1];
  134. title = $('title').text().split(' - ')[0].replace(/\s+/g,'_');
  135.  
  136. var pid = 'PP1';
  137. var count = parseInt($('#count').val());
  138. var max = parseInt($('#max').val());
  139.  
  140. var url = 'https://' + domain + '/books?id=' + id + '&lpg=' + pid + '&pg=' + pid + '&jscmd=click3';
  141.  
  142. $.ajax({
  143. dataType: 'json',
  144. async: false,
  145. url: url,
  146. }).done(function (data) {
  147. pids = $.map(data.page, function (val, i) {
  148. if (i > 3) {
  149. return val.pid;
  150. }
  151. });
  152. });
  153.  
  154. var number = pids.length;
  155. $('#total').val(number);
  156.  
  157. if (count >= number) {
  158. alert('Wrong number of pages. There are only ' + number + ' available.');
  159. $('#savePNG button').prop('disabled', false);
  160. } else {
  161. var i = count;
  162. var timer = setInterval(function () {
  163.  
  164. if (i == max || i == number) {
  165. clearInterval(timer);
  166. alert('Downloaded every available page out of '+ number + ' total. Saving…');
  167. saveZip(i, number);
  168. return false;
  169. }
  170.  
  171. var url = 'https://' + domain + '/books?id=' + id + '&lpg=' + pids[i] + '&pg=' + pids[i] + '&jscmd=click3';
  172.  
  173. $.ajax({
  174. dataType: 'json',
  175. async: false,
  176. url: url,
  177. }).done(function (data) {
  178. if (data.page[0].hasOwnProperty('src') || data.page[0].flags == 8) {
  179. var url = data.page[0].src + '&w=1600';
  180. var name = pad((i+1), number.toString().length) + '_' + pids[i];
  181.  
  182. if (data.page[0].flags == 8) {
  183. $('#count').val(i + 1);
  184. $('#count').css('font-weight', 'bold').css('text-decoration', 'line-through');
  185. } else {
  186. addIMG(url, name, (i + 1));
  187. }
  188. i++;
  189.  
  190. } else {
  191. clearInterval(timer);
  192. alert('Can\'t download more.\n' +
  193. 'Saving ' + i + ' out of ' + number + ' pages…\n' +
  194. 'Change your IP and continue!');
  195. saveZip(i, number);
  196. }
  197. });
  198. }, 400);
  199. }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement