Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save cemerson/043d3b455317d762bb1378aeac3679f3 to your computer and use it in GitHub Desktop.
Save cemerson/043d3b455317d762bb1378aeac3679f3 to your computer and use it in GitHub Desktop.
Archive.org Scanned Book Downloader Bookmarklet

Archive.org Scanned Book Downloader Bookmarklet

A simple "1-click" javascript approach to downloading a scanned book from archive.org to read at your leisure on the device of your choosing w/out having to manually screenshot every pages of the book by hand. In short it's a glorified "Save Image As..." approach but consolidated down to "1 click". BTW there may be a much better option than this out there - I just built this as an autistic project to see if it would work.

Demo Video

Archive.org SBDL Demo

Obligatory Legal/Disclaimer:

By using this script you agree to delete all book files/images after your 1 hour or 14 days is up! I don't support using this script for any other use cases. After all, none of us have ever kept a library book past it's return date, right?

NOTES:

  • Scanned Books Only: This only works on "scanned" books where each page is an image file. This means A) you won't be able search the text of the book and B) the book file size will be tens of megabytes not kilobytes like an EPUB/etc. Given the above always try to find the book in text format first (epub, etc) before using this method.
  • Compatibility: As of 11/2021 I've tested this on a few books w/no problems so it seems pretty stable but if someone finds a book that doesn't work w/it LMK in comments. It's very possible (likely?) at some point archive.org will change something that either requires some adjustments to this script and/or makes this approach no longer possible. Feel free to recommend tweaks or fixes if anyone has any suggestions btw.
  • Borrowed and?: I've only tested this for "Borrowed" books but I suppose you could use on Free books too - although normally those already offer a PDF download so not really a reason to do that.
  • Support: This is just a basic javascript thing so there's no real danger here but I can't/don't provide any support if this doesn't work for you and/or your browser crashes while trying it.

Instructions

  1. Create a bookmarklet in your browser using the code below via https://mrcoles.com/bookmarklet/
  2. Go to archive.org and "Borrow" the book for 1 hour or 14 days (only tested with the 1 hour)
  3. Once the borrowed book page reloads click zoom icon to zoom into the 1st page of book at least 2 times (otherwise you'll get low-res version of book images)
  4. Write down or make a mental note of how many pages the book has
  5. Use browser's "Inspect Element" on first page of book to find the page image URL and right-click to "open link" in a new tab.
  6. Once on the new tab looking at the book's 1st page image, click the bookmarklet button made in step 1 and type in the number of pages the book has that you noted in step 4. Tip: Add 5-10 more pages than the book has just in case the covers/final pages of the book actually add up to a higher number.
  7. As soon as you click 'OK' after entering the page count watch for the browser's "Allow Multiple Downloads from this Site" type message in your browser and click 'Accept' or whatever. Otherwise the process will fail. Some browsers may not do this - so disregard if this isn't an issue w/your browser.
  8. Wait for the process to finish - a 300 page book takes around 3-5 minutes. Note: You can minimize the browser tab/window while the pages are downloading.
  9. Once all pages have been downloaded an "alert" message will popup when the pages have all been downloaded.
  10. At this point you'll have a bunch of book page images in your Downloads folder like mybookwhatever_000.jpg, mybookwhatever_001.jpg etc.
  11. If you want to make a PDF of the pages go to https://tools.pdf24.org/en/images-to-pdf and drag all these images into the upload area. When the images are uploading click the "A-Z sort" button at the bottom of the page to make sure the pages sort by filename.
  12. Click the "Create PDF" button when it's ready and download the PDF when it's done.
  13. Now you can enjoy reading the book at your leisure, wherever you want without having to wait for the annoying page load times of archive.org, etc!
function downloadFile(filePath){
    var link=document.createElement('a');
    link.href = filePath;
    link.download = filePath.substr(filePath.lastIndexOf('/') + 1);
    link.click();
}

function getNewURL(pageCount){
	if(pageCount == null) pageCount = 1;
	var url = document.location.href; 	
	var urlParts = url.split(".jp2");	
	var urlPrefixParts = urlParts[0].split("_");	
	var urlPageNumber = urlPrefixParts[urlPrefixParts.length-1];	
	var nextPageNumberString = String(parseInt(urlPageNumber)+pageCount).padStart(4,'0');  	
	var newURLPrefix = ''; 
	for(var p=0;p<urlPrefixParts.length-1;p++) newURLPrefix += urlPrefixParts[p] + '_';	
	var newURL = newURLPrefix + nextPageNumberString + '.jp2' + urlParts[1];	
	return newURL;
}

var confirm1 = confirm('Archive.org Scanned Book Downloader:\n\nReady Check: Are you on a window/tab viewing *just* the IMAGE of the 1st page of the book? If not cancel and run this when you are.');
if(!confirm1) return false;
var pageCount = prompt('Archive.org Scanned Book Downloader:\n\nHow many pages are in this book?');
var pageCounter = 0;
var pageInterval = null;
if(pageCount == null || pageCount == undefined || parseInt(pageCount) == NaN){
   console.log('no page count provided.. giving up.');
}else{
	pageInterval = window.setInterval(function(){	
		if(pageCounter > parseInt(pageCount)){
			window.clearInterval(pageInterval);
			pageInterval = null;			
			console.log('downloading done!..');			
			var pdfTime = confirm('All pages downloaded! (some files may still be downloading though)\n\nWould you like to go to a site to create a PDF with them now?');
			if(pdfTime){
				window.open('https://tools.pdf24.org/en/images-to-pdf','_blank');			
			}
		}else{
			var nextFile = getNewURL(pageCounter);
			downloadFile(nextFile);
			console.log('downloading next page! (' + nextFile + ')');
		}
		pageCounter += 1;
	},900);
}
@Alchemytr
Copy link

Can you provide some more detail e.g. book tested on, element copied etc. I successfully used this this morning for an out of print book without issue.

-- **************************************This email is from SimonIves.com http://www.simonives.com based in Australia. Please delete this email if you are not the intended recipient. **************************************

Interesting. I was going to go test it out now myself.
If anyone's able to ID a title that doesn't work, I've found some alternative scripting tools that use a similar method but might work.

@Krando
Copy link

Krando commented Apr 21, 2024

Can you provide some more detail e.g. book tested on, element copied etc. I successfully used this this morning for an out of print book without issue.

-- **************************************This email is from SimonIves.com http://www.simonives.com based in Australia. Please delete this email if you are not the intended recipient. **************************************

Interesting. I was going to go test it out now myself. If anyone's able to ID a title that doesn't work, I've found some alternative scripting tools that use a similar method but might work.

This doesn't work for me, can you elaborate more on the alternative scripting tools that can assist me in getting a book from 1913? I would be very much indebted to you for the favor. Indeed, everybody here would be indebted and be very much thankful.

@DarkJester89
Copy link

Hi, I was trying to get this book, https://archive.org/details/motionsduringtri00spel/mode/2up, but its image page was generated failed downloads after I clicked "allowed".

Any guidance? Thank you in advance.
Image page
Failed

@complexmoth
Copy link

Tested and working @amochkin's code on Firefox v125.
Also thanks to @Alchemytr & @mikkovedru for the extra tips

@Alchemytr
Copy link

Alchemytr commented Apr 30, 2024 via email

@defarm
Copy link

defarm commented May 23, 2024

Thank you!

@etushar89
Copy link

How does one run Bookmarklet while also being on the same tab as first page of the book?

@IMANWriter
Copy link

Worked Like a charm. Saved me a week's work. Can't thank you enough!

@IMANWriter
Copy link

How does one run Bookmarklet while also being on the same tab as first page of the book?

After creating the bookmarklet, just drag to the bookmarks bar the blue button that appears after

You did it! You can run your bookmarklet by clicking: [the name you chose for your bookmarklet]

Open the page with the image and run the bookmark!

Hope it helps

@cemerson
Copy link
Author

Thank you, @IMANWriter. Glad it worked and helped! :)

@myfoundation
Copy link

Archive.org Scanned Book Auto Name Generating Bookmarklet.
Script generating name as

book name - year - ISBN

and copy it to clipboard.

`var LEN = function(x) { return x.length; };
var get_field = function(field) { return document.querySelectorAll(field); };
var get_value = function(x)
{
var _r = '';
if(LEN(x) == 1) _r = x[0].innerText;
if(LEN(x) == 2)
{
_r = x[0].innerText;
if(LEN(x[1].innerText) > LEN(_r)) _r = x[1].innerText;
}
if(LEN(x) > 2) _r = x[0].innerText;
return _r;
};
var GV = function(x) { return get_value(get_field(x)).replace(/[/:?!\n\]/g, ''); };

var split_date = function(x) { var t = x.split(' '); return t[LEN(t)-1]; };

var file_name =
GV('[itemprop="name"]') +
' - ' +
split_date(GV('[itemprop="datePublished"]')) +
' - ' +
'ISBN ' + GV('[itemprop="isbn"]');

console.log(file_name);

const copyToClipboard = str => {
if (navigator && navigator.clipboard && navigator.clipboard.writeText)
return navigator.clipboard.writeText(str);
return Promise.reject('The Clipboard API is not available.');
};

copyToClipboard(file_name);

/*
16:59 21.07.2024

THIS SCRIPT IS HELPER FOR DOWNLOAD BOOKS FROM https://archive.org
IT GENERATE STRING AS
book name - year - ISBN
AND COPY IT TO CLIPBOARD

DO bookmarklet IN https://mrcoles.com/bookmarklet/ FROM SCRIPT ABOVE OR JUST PASTE THIS TO BROWSER BOOKMARK BUTTON:

javascript:(function()%7Bvar%20LEN%20%3D%20function(x)%20%7B%20return%20x.length%3B%20%7D%3Bvar%20get_field%20%3D%20function(field)%20%7B%20return%20document.querySelectorAll(field)%3B%20%7D%3Bvar%20get_value%20%3D%20function(x)%7Bvar%20_r%20%3D%20''%3Bif(LEN(x)%20%3D%3D%201)%20_r%20%3D%20x%5B0%5D.innerText%3Bif(LEN(x)%20%3D%3D%202)%7B_r%20%3D%20x%5B0%5D.innerText%3Bif(LEN(x%5B1%5D.innerText)%20%3E%20LEN(_r))%20_r%20%3D%20x%5B1%5D.innerText%3B%7Dif(LEN(x)%20%3E%202)%20_r%20%3D%20x%5B0%5D.innerText%3Breturn%20_r%3B%7D%3Bvar%20GV%20%3D%20function(x)%20%7B%20return%20get_value(get_field(x)).replace(%2F%5B%2F%3A%3F!%5Cn%5C%5C%5D%2Fg%2C%20'')%3B%20%7D%3Bvar%20split_date%20%3D%20function(x)%20%7B%20var%20t%20%3D%20x.split('%20')%3B%20return%20t%5BLEN(t)-1%5D%3B%20%7D%3Bvar%20file_name%20%3DGV('%5Bitemprop%3D%22name%22%5D')%20%2B'%20-%20'%20%2Bsplit_date(GV('%5Bitemprop%3D%22datePublished%22%5D'))%20%2B'%20-%20'%20%2B'ISBN%20'%20%2B%20GV('%5Bitemprop%3D%22isbn%22%5D')%3Bconsole.log(file_name)%3Bconst%20copyToClipboard%20%3D%20str%20%3D%3E%20%7Bif%20(navigator%20%26%26%20navigator.clipboard%20%26%26%20navigator.clipboard.writeText)return%20navigator.clipboard.writeText(str)%3Breturn%20Promise.reject('The%20Clipboard%20API%20is%20not%20available.')%3B%7D%3BcopyToClipboard(file_name)%3B%2F16%3A59%2021.07.2024DO%20bookmarklet%20AT%20OR%20JUST%20PASTE%20THIS%20TO%20BROWSER%20BOOKMARK%20BUTTONhttps%3A%2F%2Fmrcoles.com%2Fbookmarklet%2F%2F%7D)()

*/
`

@Connal2
Copy link

Connal2 commented Aug 26, 2024

Hi there,
thanks a bunch for this site here and the script.
Unfortunately, and even though I did everything as described, after some pages ending with ".jpg". all other following pictures are only 3.2 kb and ending with ".jp2....". And all are useless ie cannot be viewed. I am desperate.

All those years, I used "Chrome Cache View" and could just copy and paste all jpgs and merge them to one pdf, but now all I find there too are these useless ".jp2..." files.
Here is an example from an Agatha Christie book:

450frompaddingto0000agat_y2n8_0007.jpg (first few perfect)

then it changes to:

450frompaddingto0000agat_y2n8_0021.jp2&id=450frompaddingto0000agat_y2n8&scale=1&rotate=0

DarkJester89 [on Apr 24] seemed to experience the same problem. With pictures see above,

THANKS!

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