Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name EBJ ripper
- // @namespace http://tampermonkey.net/
- // @version 0.2
- // @description Rips from EBJ kind of automatically, only runs with FF w/ GM don't try it on anything else god fucking damnit
- // @author (You)
- // @run-at document-start
- // @include https://br.ebookjapan.jp/br/reader/viewer/view.html*
- // @require https://raw.githubusercontent.com/eligrey/FileSaver.js/master/FileSaver.js
- // @require https://ajax.googleapis.com/ajax/libs/jquery/1.12.3/jquery.min.js
- // @grant none
- // ==/UserScript==
- (function() {
- 'use strict';
- //snatch toBlob() before the sneaky japs remove it
- var f = HTMLCanvasElement.prototype.toBlob;
- //Keep track of which page we're at at so we don't save dupes
- var page=0;
- $(document).ready(function(){
- var observer = new MutationObserver(function(mutations) {
- mutations.forEach(function(mutation) {
- if (mutation.attributeName === "class"){
- setTimeout(500);
- // Check to see if actually want to run the script
- if(!confirm("Try and dump pages?")) return false;
- observer.disconnect();
- main();
- }
- });
- });
- observer.observe(document.body,{
- attributes: true
- });
- });
- function main(){
- //Extract the cover page before inserting the observer
- var allCanvas = document.getElementsByTagName("canvas");
- extract(5, allCanvas);
- //Create observer
- var observer = new MutationObserver(function(mutations) {
- //Gather all the canvas that fired the observer and send to extract
- var i;
- var canvasE = [];
- for(i = 0;i<mutations.length;i++){
- canvasE[i] = mutations[i].target;
- }
- extract(3, canvasE);
- });
- var observerConfig = {
- attributes: true,
- subtree: true,
- Filter:['page']
- };
- var targetNode = document.getElementById("inner");
- observer.observe(targetNode, observerConfig);
- }
- function extract(t, canvas){
- //Check if canvas is 'current', if yes save to disk. If canvas were extracted flip pages after t seconds
- var i;
- var extracted;
- for(i = 0;i<canvas.length;i++){
- if(canvas[i].getAttribute("class")=="current"&&canvas[i].getAttribute("page")==page){
- save(page+1, canvas[i]);
- extracted = true;
- page++;
- }
- }
- if(extracted===true){
- window.setTimeout(flipPages, t*1000);
- }
- }
- function flipPages(){
- //Simulate a click to the controller element, this function is called on a timed delay to ensure the next pages are preloaded
- var elem = document.getElementById("controller");
- triggerMouseEvent(elem, "mousedown");
- triggerMouseEvent(elem, "mouseup");
- }
- function save(n, c){
- //Saves with a filename consisting of the book's title and the page + 1 (it starts from 0)
- var e = document.createElement('canvas');
- e=c;
- e.toBlob=f;
- e.toBlob(function(blob) {
- var filename = getFilename(n);
- saveAs(blob, filename);
- console.log("saved as "+filename);
- });
- }
- function getFilename(n){
- //Returns a filename in the Book title_000.png format
- var title = document.title;
- return title + "_" + prefix(3, n.toString()) + ".png";
- }
- function prefix(n, s){
- //Returns the page number with 3 digits for sorting purposes.
- var i = 0;
- var strPrefix="";
- for(i=0;i<n-s.length;i++){
- strPrefix+="0";
- }
- s = strPrefix+s;
- return s;
- }
- function triggerMouseEvent(node, eventType){
- //This is an internal function for mouse click events, fuck using jQuery for this.
- var clickEvent = document.createEvent ('MouseEvents');
- clickEvent.initEvent (eventType, true, true);
- node.dispatchEvent (clickEvent);
- }
- })();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement