Skip to content

Instantly share code, notes, and snippets.

@yurydelendik
Last active September 22, 2017 06:40
Show Gist options
  • Save yurydelendik/a9edeca755376e3b6336 to your computer and use it in GitHub Desktop.
Save yurydelendik/a9edeca755376e3b6336 to your computer and use it in GitHub Desktop.
PDF.js bookmarklet
var next = Promise.resolve();
var content = [];
for (var i = 1; i <= PDFView.pdfDocument.numPages; i++) {
next = next.then(function (pageNum) {
return PDFView.pdfDocument.getPage(pageNum).then(function (page) {
return page.getTextContent().then(function (tc) {
content.push((tc.items || tc).map(function (x) { return x.str; }).join(' '));
});
});
}.bind(null, i));
}
next.then(function () {
var allText = content.join('\n\n');
/* window.prompt('Text content', allText) */
/* new DownloadManager().downloadData(new Blob([allText]), 'export.txt', 'text/plain'); */
window.alert(allText);
}, function (reason) {
window.alert('Error: ' + reason);
});
function invertPage(canvas) {
var ctx = canvas.getContext('2d');
ctx.setTransform(1, 0, 0, 1, 0, 0);
var pixels = ctx.getImageData(0, 0, canvas.width, canvas.height);
var data = pixels.data;
for (var i = 0, ii = data.length; i < ii; i+=4) {
var r = data[i], g = data[i + 1], b = data[i + 2];
var d = 255 - (Math.max(r, g, b) + Math.min(r, g, b));
data[i] = r + d;
data[i + 1] = g + d;
data[i + 2] = b + d;
}
ctx.putImageData(pixels, 0, 0);
canvas.style.backgroundColor = 'black';
}
document.addEventListener('pagerender', function (e) {
var pageNumber = e.detail.pageNumber;
var page = PDFView.pages[pageNumber - 1];
invertPage(page.canvas);
});
(function reset() {
for (var i = 0, ii = PDFView.pages.length; i < ii; i++) {
if (PDFView.pages[i].renderingState === RenderingStates.FINISHED) {
invertPage(PDFView.pages[i].canvas);
}
}
})();
var waitInterval = 3000;
var lastPage = PDFView.page;
setTimeout(function next() {
if (lastPage != PDFView.page) {
return; /* user wants to control, cancelling*/
}
PDFView.page = ++lastPage;
setTimeout(next, waitInterval);
}, waitInterval);
@yurydelendik
Copy link
Author

Bookmarklet for Firefox PDF Viewer and PDF.js addon. Also works for Chromium PDF.js extension.

http://ted.mielczarek.org/code/mozilla/bookmarklet.html to minimize

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