Skip to content

Commit

Permalink
v0.6.3
Browse files Browse the repository at this point in the history
  • Loading branch information
elementdavv committed Sep 6, 2023
1 parent 306ca3b commit fc0fc70
Show file tree
Hide file tree
Showing 11 changed files with 243 additions and 107 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,11 @@ After borrowing a book, two new buttons read "Quality" and "Download" will appea
With download begins, the button will turn into a progress bar.
<image src="resources/download.png">

There are variant leaf qualities for each book which the extension keeps up to four levels. Click the stars on the "Quality" button to choose one. Default is the best quality(the original image, without scaled down).

When download completes, the book will be returned automatically to make it available to other users.

There are variant leaf qualities for each book which the extension keeps up to four levels. Click the stars on the "Quality" button to choose one. Default is the best quality(the original image, without scaled down).
In [Internet Archive](https://archive.org), if a book has 14 days loan period, only one user can borrow it for 14 days, others must wait. And multiple users can borrow it for 1 hour in the meantime. If a book has only 1 hour loan period, only one user can borrow it, others must wait.

## Availability
* Chromium family(Chrome, Edge, Brave, Vivaldi, Opera, Yandex, Kiwi, etc) version 90+ supported
Expand Down
109 changes: 81 additions & 28 deletions moz/js/bg.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,15 @@
(() => {
'use strict';

const host = 'archive.org';
const origin = 'https://archive.org';
const detail = 'https://archive.org/details';
var dnr = false;
chrome.action.onClicked.addListener(async tab => {
const origin = 'https://archive.org';
const detail = 'https://archive.org/details';
const dnr = await loadDnr();

chrome.action.onClicked.addListener(tab => {
if (!dnr) {
if (dnr == 0) {
console.log('browser unsupported');
}
// on old kiwi, tabs updated complete event not triggered, to work from here
// on old kiwi, tabs update complete event not triggered, to work from here
else if (tab.url.indexOf(detail) > -1) {
injectjs(tab.id);
}
Expand All @@ -26,9 +25,15 @@
}
});

chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => {
if (dnr && changeInfo.status == 'complete' && tab.url.indexOf(detail) > -1) {
injectjs(tabId);
chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => {
const detail = 'https://archive.org/details';

if (changeInfo.status == 'complete' && tab.url.indexOf(detail) > -1) {
const dnr = await loadDnr();

if (dnr == 1) {
injectjs(tabId);
}
}
});

Expand All @@ -39,21 +44,20 @@
});
}

var fileidtab = new Map(); // fileid to tabid map
var downloadidtab = new Map(); // downloadid to tabid map

// when new/abort download from extension
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
chrome.runtime.onMessage.addListener(async (message, sender, sendResponse) => {
if (sender.id != chrome.runtime.id) return;

console.log('message received:');
console.log(message);

var fileidtab = await loadFileidtab();

switch(message.cmd) {
case 'new':
const fileid = message.fileid;
const tabid = sender.tab.id;
fileidtab.set(fileid, tabid);
saveFileidtab(fileidtab);
console.log(`fileid added: ${fileid}`);
console.log(fileidtab);
break;
Expand All @@ -62,30 +66,33 @@

// if download hasn't begin
fileidtab.forEach((tabid, fileid) => {
if (tabid == sender.tab.id) {
if (parseInt(tabid) == sender.tab.id) {
found = fileid;
}
});

if (found != 0) {
fileidtab.delete(found);
saveFileidtab(fileidtab);
console.log(`fileid removed: ${found}`);
console.log(fileidtab);
}

// if user interrupt during download
found = 0;
var downloadidtab = await loadDownloadidtab();

downloadidtab.forEach((tabid, downloadid) => {
if (tabid == sender.tab.id) {
if (parseInt(tabid) == sender.tab.id) {
found = downloadid;
chrome.downloads.cancel(downloadid);
chrome.downloads.cancel(parseInt(downloadid));
console.log(`download aborted: ${downloadid}`);
}
});

if (found != 0) {
downloadidtab.delete(found);
saveDownloadidtab(downloadidtab);
console.log(`downloadid removed: ${found}`);
console.log(downloadidtab);
}
Expand All @@ -98,20 +105,24 @@
sendResponse({});
});

// when user confirm in save as dialog
chrome.downloads.onCreated.addListener(downloadItem => {
// when user confirm in save as dialog/automatic confirm
chrome.downloads.onCreated.addListener(async downloadItem => {
var fileidtab = await loadFileidtab();

if (fileidtab.size == 0) return;

console.log('download created:')
console.log(downloadItem);
const downloadid = downloadItem.id;
const fileurl = downloadItem.url;
var downloadidtab = await loadDownloadidtab();
var found = 0;

fileidtab.forEach((tabid, fileid) => {
if (fileurl.indexOf(fileid) > -1) {
found = fileid;
downloadidtab.set(downloadid, tabid);
saveDownloadidtab(downloadidtab);
console.log(`downloadid added: ${downloadid}`);
console.log(downloadidtab);

Expand All @@ -123,19 +134,22 @@

if (found != 0) {
fileidtab.delete(found);
saveFileidtab(fileidtab);
console.log(`fileid removed: ${found}`);
console.log(fileidtab);
}
});

// when download paused/resume/canceled/error/complete from browser
chrome.downloads.onChanged.addListener(downloadDelta => {
if (!downloadidtab.has(downloadDelta.id)) return;
chrome.downloads.onChanged.addListener(async downloadDelta => {
var downloadidtab = await loadDownloadidtab();

if (!downloadidtab.has('' + downloadDelta.id)) return;

console.log('download change:')
console.log(downloadDelta);
const downloadid = downloadDelta.id;
const tabid = downloadidtab.get(downloadid);
const downloadid = '' + downloadDelta.id;
const tabid = parseInt(downloadidtab.get(downloadid));

if (downloadDelta.paused != undefined
&& downloadDelta.paused.current == true) {
Expand All @@ -151,6 +165,7 @@
}
else if (downloadDelta.error != undefined) {
downloadidtab.delete(downloadid);
saveDownloadidtab(downloadidtab);
console.log(`downloadid removed: ${downloadid}`);
console.log(downloadidtab);

Expand All @@ -160,18 +175,22 @@
});
}
else {
chrome.downloads.cancel(downloadid);
chrome.downloads.cancel(parseInt(downloadid));
}
}
else if (downloadDelta.state !== undefined
&& downloadDelta.state.current == 'complete') {
downloadidtab.delete(downloadid);
saveDownloadidtab(downloadidtab);
console.log(`downloadid removed: ${downloadid}`);
console.log(downloadidtab);
}
});

const getOption = () => {
const host = 'archive.org';
const origin = 'https://archive.org';

return {
removeRuleIds: [1]
, addRules: [
Expand Down Expand Up @@ -204,15 +223,49 @@

if (chrome.declarativeNetRequest) {
chrome.declarativeNetRequest.updateSessionRules(getOption())
.then(()=>dnr=true)
.catch(e=>console.error(e));
.then(()=>saveDnr())
.catch(e=>console.error(e));
}

var dnr = 0;

// for Brave
setTimeout(() => {
if (!dnr) {
if (dnr == 0) {
chrome.runtime.reload();
}
}, 2e3);

// storage
// dnr
function saveDnr() {
dnr = 1;
chrome.storage.session.set({ 'dnr': dnr });
}

async function loadDnr() {
const r = await chrome.storage.session.get({ 'dnr': 0 });
return parseInt(r.dnr);
}

// fileid to tabid map
function saveFileidtab(fileidtab) {
chrome.storage.session.set({'fileidtab': Object.fromEntries(fileidtab)});
}

async function loadFileidtab() {
const r = await chrome.storage.session.get({'fileidtab': {}});
return new Map(Object.entries(r.fileidtab));
}

// downloadid to tabid map
function saveDownloadidtab(downloadidtab) {
chrome.storage.session.set({'downloadidtab': Object.fromEntries(downloadidtab)});
}

async function loadDownloadidtab() {
const r = await chrome.storage.session.get({'downloadidtab': {}});
return new Map(Object.entries(r.downloadidtab));
}

})();
41 changes: 21 additions & 20 deletions moz/js/content1.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,7 @@ export default function(){
});
}

function begin() {
async function begin() {
if (status == 1) {
pause();
// before confirm dislog returns, onmessage waits.
Expand All @@ -222,7 +222,7 @@ export default function(){
readynotify();
}
else {
clearwaitswfile();
await clearwaitswfile();
getDownloadInfo();
console.log(`download ${filename} at ${new Date().toJSON().slice(0,19)}`);
download();
Expand All @@ -243,7 +243,14 @@ export default function(){
if (doc) {
if (sw) {
waitswfile();
waitsw();

if (streamSaver.swready) {
const message = 'sw ready';
console.log(message);
}
else {
waitsw();
}
}
else {
getLeafs();
Expand All @@ -257,12 +264,12 @@ export default function(){
}

// before begin, clear unclean state
function clearwaitswfile() {
async function clearwaitswfile() {
if (swaitcreate) {
swaitcreate = false;
const message = 'clear last waitswfile';
console.log(message);
abortdoc({sync: sw});
await abortdoc({sync: sw});
}
}

Expand Down Expand Up @@ -307,7 +314,6 @@ export default function(){
if (sw) {
console.log('notify browser: new');
await chrome.runtime.sendMessage({cmd: 'new', fileid: filename});
streamSaver.swready = false;
createDocSW();
}
else {
Expand Down Expand Up @@ -377,9 +383,9 @@ export default function(){
}
}

function nextLeaf() {
async function nextLeaf() {
if (++complete >= jobcount) {
clear();
await clear();
completenotify();
returnBook();
}
Expand All @@ -395,9 +401,11 @@ export default function(){
}
}

function clear() {
async function clear() {
ac = null;
doc.end();
await writer.ready;
await writer.close();
}

function dispatch() {
Expand Down Expand Up @@ -458,10 +466,10 @@ export default function(){
}
}

function abort(extra) {
async function abort(extra) {
failnotify();
abortLeaf();
abortdoc(extra);
await abortdoc(extra);
}

function abortLeaf() {
Expand All @@ -477,14 +485,14 @@ export default function(){
doc = null;

if (writer?.abort) {
writer.abort().catch(e => {
await writer.abort().catch(e => {
console.log(`${e} when abort writer`);
}).finally(writer = null);
}

if (filehandle?.remove) {
// Brave may throw error
filehandle.remove().catch(e => {
await filehandle.remove().catch(e => {
console.log(`${e} when remove filehandle`);
}).finally(filehandle = null);
}
Expand Down Expand Up @@ -742,13 +750,6 @@ export default function(){
if (globalThis.WebStreamsPolyfill) {
streamSaver.WritableStream = globalThis.WebStreamsPolyfill.WritableStream;
}

// keep bg alive
setInterval(() => {
if (chrome.runtime?.id) {
chrome.runtime.sendMessage({cmd: 'keepalive'});
}
}, 25e3);
}

// start
Expand Down
3 changes: 1 addition & 2 deletions moz/js/pdf/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,7 @@ class PDFDocument {
this._write(`${xRefOffset}`);
this._write('%%EOF');

// end the stream
this.writer.close();
// this.writer.close();
return false;
}

Expand Down
Loading

0 comments on commit fc0fc70

Please sign in to comment.