From 88e230c71bcfbb1165e38c1ae791ce59d7b0363a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andrzej=20Pa=C5=84kowski?= Date: Sun, 29 Oct 2023 16:53:01 +0100 Subject: [PATCH] Minified bookmark code, building script and instructions --- README.md | 10 ++++++++++ bookmark.min.js | 1 + build-bookmark.sh | 10 ++++++++++ 3 files changed, 21 insertions(+) create mode 100644 bookmark.min.js create mode 100755 build-bookmark.sh diff --git a/README.md b/README.md index 1a240e7..ca8b410 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,12 @@ Possible options: 4. `quality` – quality of images; applicable when `format` is `'jpg'`; number between `0` and `1`; default is `0.9` 5. `imageNamePrefix` – prefix for names of downloaded images; string; default is `'page'` (resulting in downloaded file names e.g.: `page001.jpg`, `page002.jpg`, etc. assuming `format` is `'jpg'`) +### Bookmark + +Create a browser bookmark pasting content of [this file](bookmark.min.js) (exactly as it is) in the URL field. + +From now on, clicking it on a document page will download all pages as JPEGs. + ## Converting downloaded images back to a PDF Under Linux you can easily convert downloaded images back to a PDF. @@ -51,3 +57,7 @@ Under Linux you can easily convert downloaded images back to a PDF. ### Troubleshooting If you see errors from ImageMagick with the message "attempt to perform an operation not allowed by the security policy 'PDF'", see [this StackOverflow question](https://stackoverflow.com/q/52998331/1820695) and answers for a likely quick fix. + +## Developing + +1. Run `build-bookmark.sh` to update minified bookmark code in `bookmark.min.js`. diff --git a/bookmark.min.js b/bookmark.min.js new file mode 100644 index 0000000..fb844f5 --- /dev/null +++ b/bookmark.min.js @@ -0,0 +1 @@ +javascript:function getPageCanvas(pageNo){return document.getElementById("page_"+pageNo)}function getPageCount(){const pageNumInput=document.getElementById("pageNumInput");if(!pageNumInput){throw new Error("Couldn't find element containing total page count")}return parseInt(pageNumInput.parentNode.innerText.replaceAll(" ","").replaceAll("/",""))}function revealAllPagePlaceholders(){let continueButton;while((continueButton=document.getElementById("continueButton"))!=null){continueButton.click()}const pageCount=getPageCount();for(let pageNo=1;pageNo<=pageCount;pageNo++){if(!getPageCanvas(pageNo))throw new Error("Couldn't find page canvas for page #"+pageNo)}console.log("Revealed all page placeholders")}function waitUntilPageIsLoaded(pageNo,pageCanvas,resolve){const isLoaded=pageCanvas.getAttribute("lz")==="1";if(isLoaded){console.log("Loaded page #"+pageNo);resolve()}else{setTimeout(()=>waitUntilPageIsLoaded(pageNo,pageCanvas,resolve),100)}}async function preloadPage(pageNo,pageCanvas){console.log("Preloading page #"+pageNo);pageCanvas.scrollIntoView();return new Promise(resolve=>waitUntilPageIsLoaded(pageNo,pageCanvas,resolve))}async function preloadAllPages(){revealAllPagePlaceholders();const pageCount=getPageCount();for(let pageNo=1;pageNo<=pageCount;pageNo++){const pageCanvas=getPageCanvas(pageNo);await preloadPage(pageNo,pageCanvas)}console.log("Finished preloading pages")}function imageNameFor(pageNo,{imageNamePrefix="page"}){return imageNamePrefix+pageNo.toString().padStart(3,"0")}function imageFormatFor({format="jpg",quality=.9}){switch(format.toLowerCase()){case"jpg":case"jpeg":return{mimeType:"image/jpeg",extension:".jpg",quality:quality};case"png":return{mimeType:"image/png",extension:".png"};default:throw new Error("Unknown image format "+format)}}function downloadCanvasAsImage(canvas,imageName,imageFormat){const{mimeType,extension,quality}=imageFormat;canvas.toBlob(blob=>{const anchor=document.createElement("a");anchor.download=imageName+extension;anchor.href=URL.createObjectURL(blob);anchor.click();URL.revokeObjectURL(anchor.href)},mimeType,quality)}async function downloadPages(options={}){revealAllPagePlaceholders();const imageFormat=imageFormatFor(options);const{fromPage=1,toPage=getPageCount()}=options;for(let pageNo=fromPage;pageNo<=toPage;pageNo++){const pageCanvas=getPageCanvas(pageNo);if(!pageCanvas)break;const imageName=imageNameFor(pageNo,options);await preloadPage(pageNo,pageCanvas).then(()=>{downloadCanvasAsImage(pageCanvas,imageName,imageFormat);console.log("Downloaded page #"+pageNo)})}console.log("Finished downloading pages "+fromPage+"-"+toPage)}downloadPages(); diff --git a/build-bookmark.sh b/build-bookmark.sh new file mode 100755 index 0000000..2227223 --- /dev/null +++ b/build-bookmark.sh @@ -0,0 +1,10 @@ +#!/bin/bash + +set -euE +set -o pipefail + +input=downloadPages.js +output=bookmark.min.js + +echo -n "javascript:" > $output +uglifyjs <<< "$(cat "$input"; echo "downloadPages()")" >> $output