Skip to content

Commit

Permalink
Fix direct download by webextension download API
Browse files Browse the repository at this point in the history
The new arxiv.org behavior redirects PDF to browser.arxiv.org, which
causes the original direct download using <a> links to fail due to the
violation of same-origin URL requirement. See:

    https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#attributes

To prevent similar issues from happening in the future, a more robust
way is to simply use the webextension download API along with send
message, so as to achieve real direct downloads.
  • Loading branch information
j3soon committed Oct 8, 2023
1 parent 69aa42d commit 05185b1
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 11 deletions.
10 changes: 10 additions & 0 deletions chrome/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,14 @@ async function onButtonClickedAsync(tab) {
}
console.log(LOG_PREFIX, "Opened abstract / PDF page in existing / new tab.");
}
async function onMessage(message) {
await chrome.downloads.download({
url: message.url,
filename: message.filename,
saveAs: false,
});
console.log(LOG_PREFIX, `Downloaded file: ${message.filename} from ${message.url}.`)
}
function onContextClicked(info, tab) {
if (info.menuItemId === 'help')
chrome.tabs.create({
Expand Down Expand Up @@ -129,6 +137,8 @@ chrome.tabs.onUpdated.addListener(onTabUpdated);
chrome.action.onClicked.addListener(onButtonClickedAsync);
// Listen to extension button right-click.
chrome.contextMenus.onClicked.addListener(onContextClicked)
// Listen to download request
chrome.runtime.onMessage.addListener(onMessage);

// Listen to on extension install event.
chrome.runtime.onInstalled.addListener(onInstalled);
Expand Down
17 changes: 12 additions & 5 deletions chrome/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,14 +82,14 @@ async function addCustomLinksAsync(id, articleInfo) {
.replace('${publishedYear}', articleInfo.publishedYear)
.replace('${updatedYear}', articleInfo.updatedYear)
.replace('${version}', articleInfo.version)
.replace('${paperid}', id)
;
const directURL = `https://arxiv.org/pdf/${id}.pdf`;
const directDownloadId = "arxiv-utils-direct-download-li";
document.getElementById(directDownloadId)?.remove();
const directDownloadLiId = "arxiv-utils-direct-download-li";
const directDownloadAId = "arxiv-utils-direct-download-a";
document.getElementById(directDownloadLiId)?.remove();
const directDownloadHTML = ` \
<li id="${directDownloadId}"> \
<a href="${directURL}" download="${fileName}" type="application/pdf">Direct Download</a> \
<li id="${directDownloadLiId}"> \
<a id="${directDownloadAId}" href="#">Direct Download</a> \
</li>`;
const downloadUL = document.querySelector(".full-text > ul");
if (!downloadUL) {
Expand All @@ -98,6 +98,13 @@ async function addCustomLinksAsync(id, articleInfo) {
}
downloadUL.innerHTML += directDownloadHTML;
console.log(LOG_PREFIX, "Added direct download link.")
document.getElementById(directDownloadAId).addEventListener('click', function(e) {
chrome.runtime.sendMessage({
url: directURL,
filename: fileName,
});
e.preventDefault();
});
// Add extra services links.
const elExtraRefCite = document.querySelector(".extra-ref-cite");
if (!elExtraRefCite) {
Expand Down
3 changes: 2 additions & 1 deletion chrome/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"activeTab",
"storage",
"contextMenus",
"scripting"
"scripting",
"downloads"
],
"host_permissions": [
"*://arxiv.org/*",
Expand Down
10 changes: 10 additions & 0 deletions firefox/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,14 @@ async function onButtonClickedAsync(tab) {
}
console.log(LOG_PREFIX, "Opened abstract / PDF page in existing / new tab.");
}
async function onMessage(message) {
await chrome.downloads.download({
url: message.url,
filename: message.filename,
saveAs: false,
});
console.log(LOG_PREFIX, `Downloaded file: ${message.filename} from ${message.url}.`)
}
// Redirect to custom PDF page.
async function onBeforeWebRequestAsync(requestDetails) {
if (requestDetails.documentUrl !== undefined) {
Expand Down Expand Up @@ -162,6 +170,8 @@ browser.contextMenus.create({
});
}
});
// Listen to download request
chrome.runtime.onMessage.addListener(onMessage);

// Redirect the PDF page to custom PDF container page.
browser.webRequest.onBeforeRequest.addListener(
Expand Down
17 changes: 12 additions & 5 deletions firefox/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ async function addCustomLinksAsync(id, articleInfo) {
.replace('${publishedYear}', articleInfo.publishedYear)
.replace('${updatedYear}', articleInfo.updatedYear)
.replace('${version}', articleInfo.version)
.replace('${paperid}', id)
;
const directURL = `https://arxiv.org/pdf/${id}.pdf?download`;
const directDownloadId = "arxiv-utils-direct-download-li";
document.getElementById(directDownloadId)?.remove();
const directDownloadLiId = "arxiv-utils-direct-download-li";
const directDownloadAId = "arxiv-utils-direct-download-a";
document.getElementById(directDownloadLiId)?.remove();
const directDownloadHTML = ` \
<li id="${directDownloadId}"> \
<a href="${directURL}" download="${fileName}" type="application/pdf">Direct Download</a> \
<li id="${directDownloadLiId}"> \
<a id="${directDownloadAId}" href="#">Direct Download</a> \
</li>`;
const downloadUL = document.querySelector(".full-text > ul");
if (!downloadUL) {
Expand All @@ -99,6 +99,13 @@ async function addCustomLinksAsync(id, articleInfo) {
}
downloadUL.innerHTML += directDownloadHTML;
console.log(LOG_PREFIX, "Added direct download link.")
document.getElementById(directDownloadAId).addEventListener('click', function(e) {
chrome.runtime.sendMessage({
url: directURL,
filename: fileName,
});
e.preventDefault();
});
// Add extra services links.
const elExtraRefCite = document.querySelector(".extra-ref-cite");
if (!elExtraRefCite) {
Expand Down
1 change: 1 addition & 0 deletions firefox/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
"webRequest",
"webRequestBlocking",
"bookmarks",
"downloads",
"*://arxiv.org/*pdf*",
"*://export.arxiv.org/*pdf*",
"*://browse.arxiv.org/*pdf*"
Expand Down

0 comments on commit 05185b1

Please sign in to comment.