Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Speed read PDFs with jetzt
#!/bin/bash
# Convert a pdf to html, add Javascript for speed reading
#READPDFLOC="./readPDF" # Use this if you want to use a local version
READPDFLOC="https://cdn.rawgit.com/mm--/b47c2700b6c63f1598e4/raw/a96388cb3cf89b7a041a63f06a91c2953ee5731c/readPDF.js"
FILE="$1"
HTML="$(basename "$FILE" .pdf).html"
SAVEDDIR=`pwd`
echo SAVEDDIR IS $SAVEDDIR
TMPDIR=`mktemp -d pdfconvert-XXXXXX`
function finish {
cd "$SAVEDDIR"
rm -rf "$TMPDIR"
}
trap finish EXIT
cp "$FILE" "$TMPDIR"
cd "$TMPDIR"
SCRIPTFILE="script.txt"
cat > "$SCRIPTFILE" <<EOF
<script data-main="$READPDFLOC" src="https://cdnjs.cloudflare.com/ajax/libs/require.js/2.1.22/require.min.js"></script>
EOF
pdf2htmlEX --zoom 1.5 --process-outline 0 "$FILE"
sed -i "/X-UA-Compatible/ r $SCRIPTFILE" "$HTML"
cp "$HTML" "$SAVEDDIR"
// Speed read PDF pages with pdf2htmlEX and jetzt
// Press "n" to go to next page
// Press "p" to go to previous page
// Press "r" to RSVP read the current page
require.config({
paths: {
jetzt: 'https://cdn.rawgit.com/ds300/jetzt/master/jetzt-solid.min'
, jquery: 'https://code.jquery.com/jquery-2.1.4.min'
},
shim: {
'jetzt': { deps: [ 'css!https://cdn.rawgit.com/ds300/jetzt/master/jetzt.css' ], exports : 'jetzt' }
},
map: {
'*': {
'css': 'https://cdnjs.cloudflare.com/ajax/libs/require-css/0.1.8/css.min.js'
}
}
});
define(['jquery', 'jetzt'], function($, jetzt) {
function quitJetzt() {
if(jetzt.isOpen())
jetzt.quit();
}
function highlightNode(node) {
var sel = window.getSelection();
var range = document.createRange();
range.setStartBefore(node);
range.setEndAfter(node);
sel.removeAllRanges();
sel.addRange(range);
}
function unhighlight() {
var sel = window.getSelection();
sel.removeAllRanges();
}
function scrollMe(selector, callback) {
var st = $("#page-container").scrollTop();
var newSt = st + $(selector).position().top;
$("#page-container").animate({ scrollTop: newSt }, 1000, callback);
};
var currentPage = function() {
return window.pdf2htmlEX.defaultViewer.cur_page_idx + 1;
};
function scrollTo(pageNum, callback) {
quitJetzt();
console.log(pageNum);
scrollMe("#pf" + pageNum.toString(16), callback);
}
var scrollNext = function(callback) {
scrollTo(currentPage() + 1, callback);
};
var scrollPrev = function(callback) {
scrollTo(Math.max(currentPage() - 1, 1), callback);
};
var readNext = function() {
var cur = currentPage() + 1;
read("#pf" + cur.toString(16));
};
function doRead(place) {
window.setTimeout(function() {
highlightNode($(place + " .opened")[0]);
window.setTimeout(function () {
jetzt.select();
}, 500);
}, 500);
}
var readCurrent = function() {
var cur = currentPage();
doRead("#pf" + cur.toString(16));
};
function getSelectionText() {
var text = "";
if (window.getSelection) {
text = window.getSelection().toString();
} else if (document.selection && document.selection.type != "Control") {
text = document.selection.createRange().text;
}
return text;
}
var read = function(place) {
if(!place)
place = "#pf1";
quitJetzt();
scrollMe($(place), function() {
doRead(place);
});
};
$( document ).on("keypress", function(e) {
console.log(e.which);
switch(e.which) {
case 110: // 'n'
case 78:
scrollNext();
break;
case 112: // 'p'
case 80:
scrollPrev();
break;
case 114: // 'r'
case 82:
readCurrent();
break;
default:
return;
}
e.preventDefault();
});
window.readPDF = {
read: read,
readNext: readNext,
readCurrent: readCurrent,
scrollPrev: scrollPrev,
scrollNext: scrollNext
};
});

paggers commented Sep 22, 2017

Hey Josh,
Nifty script you got here, I too have been trying to use jetzt to read scientific papers.
Unfortunately, I can't seem to be able to make this work.

Would you be kind enough as to mark the places I should modify in order to make it work on my local machine?

Cheers,
Eilam (a fellow bear)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment