Skip to content

Commit

Permalink
v0.7.3
Browse files Browse the repository at this point in the history
  • Loading branch information
elementdavv committed Jul 3, 2024
1 parent 8f73d06 commit 5af474d
Show file tree
Hide file tree
Showing 7 changed files with 364 additions and 323 deletions.
17 changes: 4 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Internet Archive Downloader

[Internet Archive](https://archive.org) holds more than 34 millions books available for public. Some books can be read online by borrowing for a limit period. This extension can download these books for offline reading.
[Internet Archive](https://archive.org) holds more than 34 millions books. Some books are permitted to read online by borrowing for a limit period. With the extension, the borrowed books can be downloaded for offline reading.

[HathiTrust Digital Library](https://hathitrust.org) is also supported. All books with full view permission can be downloaded.

Expand All @@ -12,7 +12,6 @@ The extension works by fetching every leaf of a book, constructing a PDF stream
* Optional leaf range
* Optional leaf quality
* Download multiple books in parallel
* Clean codes with small size

## Install
For Chrome, Edge, Brave, etc:
Expand All @@ -27,7 +26,7 @@ For Firefox:
Alternatively, install automatically from their repositories:
- [Chrome WebStore](https://chrome.google.com/webstore/detail/internet-archive-download/keimonnoakgkpnifppoomfdlkadghkjb)
- [Edge Addons](https://microsoftedge.microsoft.com/addons/detail/internet-archive-download/cnpoedgimjaecinmgfnfhfmcpcngeeje)
- [Mozilla Addons (Firefox)](https://addons.mozilla.org/en-US/firefox/addon/internet_archive_downloader/)(All Optional permissions must be granted as well)
- [Mozilla Addons (Firefox)](https://addons.mozilla.org/en-US/firefox/addon/internet_archive_downloader/) (All Optional permissions must be granted as well)

## Usage
### Internet Archive:
Expand All @@ -45,12 +44,9 @@ After borrowing a book, two new buttons, "Quality" and "Download", appear under
* To get each leaf as a JPEG file, press the "Download" button while holding Ctrl key (Command key on Mac).
* To get only a range of leafs, press the "Download" button while holding Alt key (Option key on Mac), then input a range.

During the download process, the button transforms into a progress bar.
<image src="resources/capture/download1.png">

There are multiple leaf qualities for each book with up to four levels available. To select preferred quality, press the stars on the "Quality" button. The default option is the best quality, which preserves the original image without scaling it down.

After successful downloads, the book will be returned automatically for availability to other users. In [Internet Archive](https://archive.org), books are always allowed to loan to limited users, others have to wait.
After successful downloads, the book will be returned automatically for availability to other users.

### HathiTrust:
No login, no borrows required. In [hathitrust.org](https://hathitrust.org), for books with full view permission, a new section, "Ayesha", appears above the "Download" section on the left-hand side of the page. The section contains three buttons, "Quality", "Tasks" and "Download".
Expand All @@ -60,14 +56,9 @@ No login, no borrows required. In [hathitrust.org](https://hathitrust.org), for
* To get each leaf as a JPEG/PNG file, press the "Download" button while holding Ctrl key (Command key on Mac).
* To get only a range of leafs, press the "Download" button while holding Alt key (Option key on Mac), then input a range.

During the download process, the button transforms into a progress bar.
<image src="resources/capture/download2.png">

There are multiple leaf qualities for each book with four levels available. To select preferred quality, press the "Quality" button. The first option, "full size", will download every leaf in its maximum size, but the size may be much different between each leafs. Other options will download all leafs in almost the same size.

The third button is "Tasks", which indicates the task number of a download process running synchronously. HathiTrust server does not allowed frequent access. So the the download process will take some breaks. Choose a suitable task number to get the best experience.

Even for some books which HathiTrust provides download link, the produced PDF files are very ugly. So it is suggested to bring in the extension as well.
The download process may take some breaks due to server constraints.

## Availability
* Chromium family(Chrome, Edge, Brave, Vivaldi, Opera, Yandex, Kiwi, etc) version 90+ supported
Expand Down
166 changes: 89 additions & 77 deletions moz/js/content1.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,80 +61,13 @@ export default function(){
alt = data.alt;
}

if (getSelPages()) {
begin();
}

begin();
break;
default:
break;
}
};

var pagecount = 0; // page count
var startp; // start page
var endp; // end page
var realcount; // real page count

function getSelPages() {
if (alt) {
const inputpages = prompt(getMessage("promptpage"));
if (inputpages == null) {
return false;
}

var pagea = inputpages.split('-');

if (pagea.length == 1) {
pagea = inputpages.split(',');
}

if (pagea.length != 2) {
alert(getMessage("invalidinput"));
return false;
}

if (pagea[0].trim().length == 0) {
startp = 1;
}
else {
startp = Number(pagea[0]);

if (!Number.isInteger(startp)) {
alert(getMessage("invalidinput"));
return false;
}
}

if (pagea[1].trim().length == 0) {
endp = pagecount;
}
else {
endp = Number(pagea[1]);

if (!Number.isInteger(endp)) {
alert(getMessage("invalidinput"));
return false;
}
}

if (startp < 1) startp = 1;
if (endp > pagecount) endp = pagecount;

if (startp > endp) {
alert(getMessage("invalidinput"));
return false;
}
}
else {
startp = 1;
endp = pagecount;
}

realcount = endp - startp + 1;
return true;
}

function loadScript(href) {
console.log(`load script: ${href}`);
var script = document.createElement('script');
Expand Down Expand Up @@ -189,6 +122,7 @@ export default function(){

var fileid = ""; // book basename
var data = []; // page urls
var pagecount = 0; // page count

function getBookInfo() {
console.log('get book info');
Expand Down Expand Up @@ -296,15 +230,80 @@ export default function(){
readynotify();
}
else {
await clearwaitswfile();
getDownloadInfo();
console.log(`download ${filename} at ${new Date().toJSON().slice(0,19)}`);
download();
if (getSelPages()) {
await clearwaitswfile();
getDownloadInfo();
console.log(`download ${filename} at ${new Date().toJSON().slice(0,19)}`);
download();
}
}
};

var filename = ""; // filename to save
var startp = 1; // start page
var endp = 100; // end page
var realcount; // real page count

function getSelPages() {
if (alt) {
const inputpages = prompt(getMessage("promptpage"), `${startp}-${endp}`);
if (inputpages == null) {
return false;
}

var pagea = inputpages.split('-');

if (pagea.length == 1) {
pagea = inputpages.split(',');
}

if (pagea.length != 2) {
alert(getMessage("invalidinput"));
return false;
}

if (pagea[0].trim().length == 0) {
startp = 1;
}
else {
startp = Number(pagea[0]);

if (!Number.isInteger(startp)) {
alert(getMessage("invalidinput"));
return false;
}
}

if (pagea[1].trim().length == 0) {
endp = pagecount;
}
else {
endp = Number(pagea[1]);

if (!Number.isInteger(endp)) {
alert(getMessage("invalidinput"));
return false;
}
}

if (startp < 1) startp = 1;
if (endp > pagecount) endp = pagecount;

if (startp > endp) {
alert(getMessage("invalidinput"));
return false;
}
}
else {
startp = 1;
endp = pagecount;
}

realcount = endp - startp + 1;
return true;
}

var scale = 1; // page scale
var filename = ""; // filename to save

function getDownloadInfo() {
scale = fromId('iadscaleid').value;
Expand Down Expand Up @@ -397,12 +396,18 @@ export default function(){
await createDoc();
}
} catch(e) {
// error from showSaveFilePicker
const message = e.toString();
console.log(message);

// user cancel in showSaveFilePicker
// SecurityError: Failed to execute 'showSaveFilePicker' on 'Window': Must be handling a user gesture to show a file picker.
if (e.code == 18) {
writer = null;
filehandle = null;
}
// DOMException: The user aborted a request.
// streamSaver will always create temporary file
if (e.name != 'AbortError') {
if (e.code != 20) {
abortdoc({sync: sw, message});
}
};
Expand Down Expand Up @@ -468,6 +473,11 @@ export default function(){
if (endp == pagecount) {
returnBook();
}
else {
startp += realcount;
endp += realcount;
if (endp > pagecount) endp = pagecount;
}
}
else {
updatenotify();
Expand Down Expand Up @@ -595,11 +605,13 @@ export default function(){
}

function createZIPPage(view, pageindex) {
pageindex++;
const name = fileid + '_' + pageindex.toString().padStart(4, '0');
doc.image({view, name});
}

function createPDFPage(view, pageindex, width, height) {
pageindex -= startp - 1;
doc.addPage({
pageindex
, margin: 0
Expand Down Expand Up @@ -676,7 +688,7 @@ export default function(){
writer = await writable.getWriter();

doc = new PDFDocument(writer, {
realcount
pagecount: realcount
, info
});
}
Expand All @@ -687,7 +699,7 @@ export default function(){

function createPDFDocSW() {
doc = new PDFDocument(writer, {
realcount
pagecount: realcount
, info
});
}
Expand Down
Loading

0 comments on commit 5af474d

Please sign in to comment.