Advertisement
Guest User

Untitled

a guest
Feb 16th, 2017
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.07 KB | None | 0 0
  1. // ==UserScript==
  2. // @name EBJ ripper
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.2
  5. // @description Rips from EBJ kind of automatically, only runs with FF w/ GM don't try it on anything else god fucking damnit
  6. // @author (You)
  7. // @run-at document-start
  8. // @include https://br.ebookjapan.jp/br/reader/viewer/view.html*
  9. // @require https://raw.githubusercontent.com/eligrey/FileSaver.js/master/FileSaver.js
  10. // @require https://ajax.googleapis.com/ajax/libs/jquery/1.12.3/jquery.min.js
  11. // @grant none
  12. // ==/UserScript==
  13.  
  14. (function() {
  15. 'use strict';
  16. //snatch toBlob() before the sneaky japs remove it
  17. var f = HTMLCanvasElement.prototype.toBlob;
  18. //Keep track of which page we're at at so we don't save dupes
  19. var page=0;
  20. $(document).ready(function(){
  21. var observer = new MutationObserver(function(mutations) {
  22. mutations.forEach(function(mutation) {
  23. if (mutation.attributeName === "class"){
  24. setTimeout(500);
  25. // Check to see if actually want to run the script
  26. if(!confirm("Try and dump pages?")) return false;
  27. observer.disconnect();
  28. main();
  29. }
  30. });
  31. });
  32. observer.observe(document.body,{
  33. attributes: true
  34. });
  35. });
  36. function main(){
  37. //Extract the cover page before inserting the observer
  38. var allCanvas = document.getElementsByTagName("canvas");
  39. extract(5, allCanvas);
  40. //Create observer
  41. var observer = new MutationObserver(function(mutations) {
  42. //Gather all the canvas that fired the observer and send to extract
  43. var i;
  44. var canvasE = [];
  45. for(i = 0;i<mutations.length;i++){
  46. canvasE[i] = mutations[i].target;
  47. }
  48. extract(3, canvasE);
  49. });
  50. var observerConfig = {
  51. attributes: true,
  52. subtree: true,
  53. Filter:['page']
  54. };
  55. var targetNode = document.getElementById("inner");
  56. observer.observe(targetNode, observerConfig);
  57. }
  58.  
  59. function extract(t, canvas){
  60. //Check if canvas is 'current', if yes save to disk. If canvas were extracted flip pages after t seconds
  61. var i;
  62. var extracted;
  63. for(i = 0;i<canvas.length;i++){
  64. if(canvas[i].getAttribute("class")=="current"&&canvas[i].getAttribute("page")==page){
  65. save(page+1, canvas[i]);
  66. extracted = true;
  67. page++;
  68. }
  69. }
  70. if(extracted===true){
  71. window.setTimeout(flipPages, t*1000);
  72. }
  73. }
  74.  
  75. function flipPages(){
  76. //Simulate a click to the controller element, this function is called on a timed delay to ensure the next pages are preloaded
  77. var elem = document.getElementById("controller");
  78. triggerMouseEvent(elem, "mousedown");
  79. triggerMouseEvent(elem, "mouseup");
  80. }
  81.  
  82. function save(n, c){
  83. //Saves with a filename consisting of the book's title and the page + 1 (it starts from 0)
  84. var e = document.createElement('canvas');
  85. e=c;
  86. e.toBlob=f;
  87. e.toBlob(function(blob) {
  88. var filename = getFilename(n);
  89. saveAs(blob, filename);
  90. console.log("saved as "+filename);
  91. });
  92. }
  93.  
  94. function getFilename(n){
  95. //Returns a filename in the Book title_000.png format
  96. var title = document.title;
  97. return title + "_" + prefix(3, n.toString()) + ".png";
  98. }
  99.  
  100. function prefix(n, s){
  101. //Returns the page number with 3 digits for sorting purposes.
  102. var i = 0;
  103. var strPrefix="";
  104. for(i=0;i<n-s.length;i++){
  105. strPrefix+="0";
  106. }
  107. s = strPrefix+s;
  108. return s;
  109. }
  110.  
  111. function triggerMouseEvent(node, eventType){
  112. //This is an internal function for mouse click events, fuck using jQuery for this.
  113. var clickEvent = document.createEvent ('MouseEvents');
  114. clickEvent.initEvent (eventType, true, true);
  115. node.dispatchEvent (clickEvent);
  116. }
  117.  
  118. })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement