Advertisement
Conquistadork

hnk

Sep 25th, 2016
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.75 KB | None | 0 0
  1. // ==UserScript==
  2. // @name ebjhooker
  3. // @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.
  4. // @namespace ebjhooker
  5. // @include https://br.ebookjapan.jp/br/reader/viewer/view.html*
  6. // @version 1
  7. // @grant none
  8. // @require https://ajax.googleapis.com/ajax/libs/jquery/1.12.3/jquery.min.js
  9. // ==/UserScript==
  10.  
  11. // If you're reading this, here's the usage tips
  12. // Currently it requires you to manually flip through the manga in the browser reader and it will pop up to save the pair of pages you've just clicked off
  13. // Be aware that it's the one you've just clicked away from, not the ones that just loaded.
  14. // IE if you flip to page 4-5, it'll pop up to save 2 and 3.
  15. // If you want to save the currently showing images (EG for the last pages), you can click in the middle - the way it usually brings up the reader menu.
  16. // That will also trigger the save.
  17. // This has to do with how the events are being handled, but it works fine as long as you don't forget that so whatever.
  18.  
  19.  
  20. // more comments in source if you want to see the nuts and bolts and how I'm fucking everything up because I suck
  21.  
  22.  
  23. this.$ = this.jQuery = jQuery.noConflict(true);
  24.  
  25. pad = function (n, width, z) {
  26. z = z || '0';
  27. n = n + '';
  28. return n.length >= width ? n : new Array(width - n.length + 1).join(z) + n;
  29. }
  30.  
  31. saveAs = function(uri, filename) {
  32. var link = document.createElement('a');
  33. if (typeof link.download === 'string') {
  34. link.href = uri;
  35. link.download = filename;
  36. document.body.appendChild(link);
  37. link.click();
  38. document.body.removeChild(link);
  39. } else {
  40. window.open(uri);
  41. }
  42. }
  43.  
  44. // wait for page to load
  45. $(document).ready(function(){
  46.  
  47. // Check to see if actually want to run the script
  48. if(!confirm("Try and dump pages?")) return false;
  49.  
  50. //set up an array for the page numbers we've dumped
  51. var pages = [];
  52. var pagenumber = -1; //cover has page number 0
  53. var pagedata = "";
  54. //set up an event to dump the current pages every time the mouse is clicked, because the canvases change without page reloads.
  55. //could possibly try and automate by triggering the mousedown/mouseup events through script (seems that they're not using click but mousedown/up)
  56. // Note that one of the divs changes when the mouse moves from left to right, so that probably determines the direction of travel <div class="leftCursor" id="viewArea" -- or it may just be the cursor graphic
  57. //but for now just do it this way.
  58.  
  59. // Put the event handler in the "mask" div - IE where the area where the manga pages are visible.
  60. // seems mask is masked (lol) so use controller which controls things.
  61.  
  62. // For some reason it only works when you click in the middle, but that's okay. Load up next two pages, click in middle, click for next 2, click in middle, etc
  63. // actually also works when you click next, but grabs the previous two pages (IE I think it triggers before the page change)
  64.  
  65. // triggering before page change maybe? seems to have odd effect when loading up previously read manga. It'll save a cover as the page number which was being
  66. // loaded in the background while you were choosing to browse from the start of the manga
  67. // trying mouseup for funnies maybe mousedown is better as the change happens on mouseup
  68. $("div#controller").on("mousedown", function(){
  69.  
  70.  
  71. //pick the current canvases ( do we have to wait for them to load?)
  72. //apart from covers when there's only one, there will usually be two 'current' canvases containing the currently visable pages.
  73. //There can be up to 8 pages loaded in masked-off divs which will dynamically shift around, load and unload as you move through the pages.
  74. // for simplicity, just deal with the currently displayed images
  75. $("canvas.current").each(function(index, element){
  76. // wait until loaded?
  77. $(element).ready(function(){
  78. //grab page number
  79. pagenumer = -1; // reset it first otherwise it can grab the wrong canvas for the page
  80.  
  81. pagenumber = $(element).attr("page");
  82.  
  83. // check that page number is valid and not already in pages array - Also check it's not "false" - IE a empty page placeholder
  84. if(pagenumber > -1 && pagenumber != "false" && pagenumber != pages.find(function(x){return x == pagenumber})){
  85.  
  86. pagecanvas = $(element);
  87.  
  88. //get dataurl - this is where the actual image data lies
  89. pagedata = pagecanvas[0].toDataURL();
  90. pagedata.replace("image/png", "image/octet-stream");
  91.  
  92. // save it through the browser
  93. saveAs(pagedata, pad(pagenumber,4)+'.png');
  94.  
  95. // comment this out for now Save manually
  96. // The original plan was to send the pages via ajax to a local server so they can be processed and saved without
  97. // having to confirm every file. But fuck it, too much work.
  98. /*
  99. //post them to our server which will turn them into images
  100. $.ajax(
  101. global: false, // don't want to trigger anything else on the page
  102. type : "POST",
  103. crossDomain : true, // because we're posting to localhost
  104. url : "http://127.0.0.1:8090/", // 8090 is the port the server will listen on
  105. data: { thispage : pagenumber, thisdata : pagedata }
  106. );
  107. // note we're not worried about responses. Fire and forget.
  108. */
  109.  
  110.  
  111. // store page numbers so we don't repeatedly download the same image
  112. pages.push(pagenumber);
  113.  
  114. //endif
  115. }
  116.  
  117. // end this.ready block
  118. })
  119. // end canvas.current iterating
  120. })
  121.  
  122.  
  123. // end click handler
  124. })
  125.  
  126. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement