Skip to content

Commit

Permalink
Make autodownload more user friendly
Browse files Browse the repository at this point in the history
  • Loading branch information
aokellermann committed Aug 15, 2023
1 parent 3aee121 commit 0eaf72e
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
nexus-now.zip
web-ext-artifacts
build
build
.idea
45 changes: 35 additions & 10 deletions background.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
const doiRegex = /\b(10[.][0-9]{4,}(?:[.][0-9]+)*\/(?:(?!["&'<>])\S)+)\b/;
const stcUrl = "https://standard--template--construct-org.ipns.dweb.link/#/nexus_science/doi:";
const carUrl = "https://bafyb4iee27p2wdqsorvj7gquitwuti3sfeepdvx2p3feao2dqri37fm3yy.ipfs.dweb.link";
const trueRed = "#BC243C";

INTERNET_IPFS_GATEWAY_URL = new URL('https://dweb.link')
HUB_DOMAIN = 'hub-standard--template--construct-org'

async function onInstalled(details) {
// add nexus option to context menu (right click)
await browser.contextMenus.create({
Expand All @@ -11,35 +13,58 @@ async function onInstalled(details) {
contexts: ["selection", "link"],
});

// open options page to choose autodownload if they want
if (details.reason === "install") {
await browser.runtime.openOptionsPage();
}
}

function getUrl(doi) {
const host = INTERNET_IPFS_GATEWAY_URL;
const baseUrl = new URL(`${host.protocol}//${HUB_DOMAIN}.ipns.${host.host}`);
return new URL(`${encodeURIComponent(encodeURIComponent(doi))}.pdf`, baseUrl);
}

async function handleDoi(doi) {
const optionsData = await browser.storage.sync.get("options");
const autodownload = optionsData?.options?.autodownload;

if (autodownload) {
const url = `${carUrl}/${encodeURIComponent(encodeURIComponent(doi))}.pdf`;
const url = getUrl(doi);
console.log(`Attempting to download pdf from ${url}`)
const downloadId = await browser.downloads.download({
url: url,

let downloadId;
browser.downloads.download({
url: url.href,
filename: `${doi}.pdf`
}).then(id => {
downloadId = id;
});
console.log(`Started download`);

const timeout = 1000;
const timeout = 5000;
await new Promise(r => setTimeout(r, timeout));

const downloads = await browser.downloads.search({id: downloadId});
const download = downloads[0];
const bytesReceived = download.bytesReceived;
console.log(`Received ${bytesReceived} after ${timeout} ms`);
let downloadStarted = false;
if (downloadId) {
const downloads = await browser.downloads.search({id: downloadId});
const download = downloads[0];
const bytesReceived = download.bytesReceived;
console.log(`Received ${bytesReceived} after ${timeout} ms`);
downloadStarted = bytesReceived !== 0;

if (!downloadStarted) {
// clean up if download failed to start
browser.downloads.erase({id: downloadId })
}
}

if (bytesReceived) return;
if (downloadStarted) {
return;
}
}

// if download didn't start, open in new tab
const url = stcUrl + doi;
console.log(`Opening STC in new tab: ${url}`)
await openTab(url);
Expand Down
2 changes: 1 addition & 1 deletion options.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<form id="optionsForm">
<label for="autodownload">
<input type="checkbox" name="autodownload" id="autodownload">
Auto-download PDF
Auto-download PDF if available
</label>
</form>
</body>
Expand Down

0 comments on commit 0eaf72e

Please sign in to comment.