Advertisement
Guest User

Untitled

a guest
Jul 28th, 2019
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.29 KB | None | 0 0
  1. // ==UserScript==
  2. // @name ebjhooker new
  3. // @namespace ebjhooker
  4. // @version 1.2
  5. // @icon https://www.ebookjapan.jp/favicon.ico
  6. // @description Grabs image data from the browser viewer to save locally. Still a bit buggy. Currently works when you click off one of the pages. Source has further comments. Updated by Cure (08/13/2017)
  7. // @author m3ch_mania, anonymous
  8. // @updateURL https://gitlab.com/m3ch_mania/ebjhooker/raw/master/ebjhooker.user.js
  9. // @downloadURL https://gitlab.com/m3ch_mania/ebjhooker/raw/master/ebjhooker.user.js
  10. // @run-at document-start
  11. // @include https://ebookjapan.yahoo.co.jp/viewer?sessionid=*
  12. // @grant GM_xmlhttpRequest
  13. // ==/UserScript==
  14.  
  15.  
  16. // Here's some usage tips
  17. // 1. Get the reader to go in single page mode, it'll make your crop job easier later.
  18. // 1.1. Create a transparency mask in PS, set its treshold to 255, trim transparent pixels, easy cropping done in 5 seconds or less.
  19. // 2. If you pressed cancel at the prompt or if it didn't show at all, press the 'up' key on your keyboard to turn on autoripping
  20. // 3. Autoripping won't stop until it gets to the end or if you make it save the same page twice.
  21. // 4. You can just manually rip using the left arrow key to scroll
  22. // 5. If you want to start ripping at a certain spot, just use any method to get through the book that isn't the left arrow key and it won't rip anything.
  23. (function() {
  24.  
  25. let f = HTMLCanvasElement.prototype.toBlob;
  26. let globPress;
  27. let pad;
  28.  
  29. pad = function (n, width, z) {
  30. z = z || '0';
  31. n = n + '';
  32. return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
  33. }
  34.  
  35.  
  36. function triggerKey(){
  37. try{
  38. var eventObj = document.createEvent("Events");
  39. eventObj.initEvent("keydown", true, true);
  40. eventObj.which = 37;
  41. eventObj.keyCode = 37;
  42. eventObj.key = "ArrowLeft";
  43. document.dispatchEvent(eventObj);
  44. }
  45. catch(e){
  46. console.log(e);
  47. }
  48. }
  49.  
  50. // wait for page to load
  51. function domReady(){
  52. let pages = [];
  53. let pagenumber = 0; //cover has page number 0
  54. let pagedata = "";
  55. let interval;
  56. let pagecanvas;
  57. if(confirm("Dump automatically? (Press the up arrow key to turn it on later, or use the left arrow key to rip manually)")){
  58. interval = setInterval(triggerKey,4000);
  59. }
  60. document.addEventListener('keydown', function(e) {
  61. if(e.keyCode == 37){
  62. pagecanvas = document.getElementsByTagName("canvas")[0];
  63. pagenumber = document.querySelector(".footer__page-output > output").innerText
  64. if(pages.includes(pagenumber)){
  65. console.log("Saving done");
  66. clearInterval(interval);
  67. }
  68. else{
  69. var tempCanvas = pagecanvas[0];
  70. tempCanvas.toBlob = f;
  71. tempCanvas.toBlob(function(blob) {
  72. let bookName = document.querySelector(".header__title > h2").innerText
  73. // var bookName = $('.header__title > h2').text().replace(' ????�???�_', ' ')
  74. let filename = bookName + pad(parseInt(pagenumber) + 1,4)+'.png';
  75. saveAs(blob, filename);
  76. //console.log("saved as "+filename);
  77. });
  78. pages.push(pagenumber);
  79. }
  80. //super();
  81. }else if(e.keyCode == 38){
  82. console.log("Manually turning on timer");
  83. interval = setInterval(triggerKey,2200);
  84. }
  85. });
  86. }
  87.  
  88. // Mozilla, Opera, Webkit
  89. if ( document.addEventListener ) {
  90. document.addEventListener( "DOMContentLoaded", function(){
  91. document.removeEventListener( "DOMContentLoaded", arguments.callee, false);
  92. domReady();
  93. }, false );
  94. }
  95.  
  96. var saveAs = saveAs || (function(view) {
  97. "use strict";
  98. // IE <10 is explicitly unsupported
  99. if (typeof view === "undefined" || typeof navigator !== "undefined" && /MSIE [1-9]\./.test(navigator.userAgent)) {
  100. return;
  101. }
  102. var
  103. doc = view.document
  104. // only get URL when necessary in case Blob.js hasn't overridden it yet
  105. , get_URL = function() {
  106. return view.URL || view.webkitURL || view;
  107. }
  108. , save_link = doc.createElementNS("http://www.w3.org/1999/xhtml", "a")
  109. , can_use_save_link = "download" in save_link
  110. , click = function(node) {
  111. var event = new MouseEvent("click");
  112. node.dispatchEvent(event);
  113. }
  114. , is_safari = /constructor/i.test(view.HTMLElement) || view.safari
  115. , is_chrome_ios =/CriOS\/[\d]+/.test(navigator.userAgent)
  116. , setImmediate = view.setImmediate || view.setTimeout
  117. , throw_outside = function(ex) {
  118. setImmediate(function() {
  119. throw ex;
  120. }, 0);
  121. }
  122. , force_saveable_type = "application/octet-stream"
  123. // the Blob API is fundamentally broken as there is no "downloadfinished" event to subscribe to
  124. , arbitrary_revoke_timeout = 1000 * 40 // in ms
  125. , revoke = function(file) {
  126. var revoker = function() {
  127. if (typeof file === "string") { // file is an object URL
  128. get_URL().revokeObjectURL(file);
  129. } else { // file is a File
  130. file.remove();
  131. }
  132. };
  133. setTimeout(revoker, arbitrary_revoke_timeout);
  134. }
  135. , dispatch = function(filesaver, event_types, event) {
  136. event_types = [].concat(event_types);
  137. var i = event_types.length;
  138. while (i--) {
  139. var listener = filesaver["on" + event_types[i]];
  140. if (typeof listener === "function") {
  141. try {
  142. listener.call(filesaver, event || filesaver);
  143. } catch (ex) {
  144. throw_outside(ex);
  145. }
  146. }
  147. }
  148. }
  149. , auto_bom = function(blob) {
  150. // prepend BOM for UTF-8 XML and text/* types (including HTML)
  151. // note: your browser will automatically convert UTF-16 U+FEFF to EF BB BF
  152. if (/^\s*(?:text\/\S*|application\/xml|\S*\/\S*\+xml)\s*;.*charset\s*=\s*utf-8/i.test(blob.type)) {
  153. return new Blob([String.fromCharCode(0xFEFF), blob], {type: blob.type});
  154. }
  155. return blob;
  156. }
  157. , FileSaver = function(blob, name, no_auto_bom) {
  158. if (!no_auto_bom) {
  159. blob = auto_bom(blob);
  160. }
  161. // First try a.download, then web filesystem, then object URLs
  162. var
  163. filesaver = this
  164. , type = blob.type
  165. , force = type === force_saveable_type
  166. , object_url
  167. , dispatch_all = function() {
  168. dispatch(filesaver, "writestart progress write writeend".split(" "));
  169. }
  170. // on any filesys errors revert to saving with object URLs
  171. , fs_error = function() {
  172. if ((is_chrome_ios || (force && is_safari)) && view.FileReader) {
  173. // Safari doesn't allow downloading of blob urls
  174. var reader = new FileReader();
  175. reader.onloadend = function() {
  176. var url = is_chrome_ios ? reader.result : reader.result.replace(/^data:[^;]*;/, 'data:attachment/file;');
  177. var popup = view.open(url, '_blank');
  178. if(!popup) view.location.href = url;
  179. url=undefined; // release reference before dispatching
  180. filesaver.readyState = filesaver.DONE;
  181. dispatch_all();
  182. };
  183. reader.readAsDataURL(blob);
  184. filesaver.readyState = filesaver.INIT;
  185. return;
  186. }
  187. // don't create more object URLs than needed
  188. if (!object_url) {
  189. object_url = get_URL().createObjectURL(blob);
  190. }
  191. if (force) {
  192. view.location.href = object_url;
  193. } else {
  194. var opened = view.open(object_url, "_blank");
  195. if (!opened) {
  196. // Apple does not allow window.open, see https://developer.apple.com/library/safari/documentation/Tools/Conceptual/SafariExtensionGuide/WorkingwithWindowsandTabs/WorkingwithWindowsandTabs.html
  197. view.location.href = object_url;
  198. }
  199. }
  200. filesaver.readyState = filesaver.DONE;
  201. dispatch_all();
  202. revoke(object_url);
  203. }
  204. ;
  205. filesaver.readyState = filesaver.INIT;
  206.  
  207. if (can_use_save_link) {
  208. object_url = get_URL().createObjectURL(blob);
  209. setImmediate(function() {
  210. save_link.href = object_url;
  211. save_link.download = name;
  212. click(save_link);
  213. dispatch_all();
  214. revoke(object_url);
  215. filesaver.readyState = filesaver.DONE;
  216. }, 0);
  217. return;
  218. }
  219.  
  220. fs_error();
  221. }
  222. , FS_proto = FileSaver.prototype
  223. , saveAs = function(blob, name, no_auto_bom) {
  224. return new FileSaver(blob, name || blob.name || "download", no_auto_bom);
  225. }
  226. ;
  227.  
  228. // IE 10+ (native saveAs)
  229. if (typeof navigator !== "undefined" && navigator.msSaveOrOpenBlob) {
  230. return function(blob, name, no_auto_bom) {
  231. name = name || blob.name || "download";
  232.  
  233. if (!no_auto_bom) {
  234. blob = auto_bom(blob);
  235. }
  236. return navigator.msSaveOrOpenBlob(blob, name);
  237. };
  238. }
  239.  
  240. // todo: detect chrome extensions & packaged apps
  241. //save_link.target = "_blank";
  242.  
  243. FS_proto.abort = function(){};
  244. FS_proto.readyState = FS_proto.INIT = 0;
  245. FS_proto.WRITING = 1;
  246. FS_proto.DONE = 2;
  247.  
  248. FS_proto.error =
  249. FS_proto.onwritestart =
  250. FS_proto.onprogress =
  251. FS_proto.onwrite =
  252. FS_proto.onabort =
  253. FS_proto.onerror =
  254. FS_proto.onwriteend =
  255. null;
  256.  
  257. return saveAs;
  258. }(
  259. typeof self !== "undefined" && self
  260. || typeof window !== "undefined" && window
  261. || this
  262. ));
  263. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement