diff --git a/chrome/background.js b/chrome/background.js index a00450b..44b4770 100644 --- a/chrome/background.js +++ b/chrome/background.js @@ -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({ @@ -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); diff --git a/chrome/content.js b/chrome/content.js index 8975594..0a16756 100644 --- a/chrome/content.js +++ b/chrome/content.js @@ -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 = ` \ -
  • \ - Direct Download \ +
  • \ + Direct Download \
  • `; const downloadUL = document.querySelector(".full-text > ul"); if (!downloadUL) { @@ -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) { diff --git a/chrome/manifest.json b/chrome/manifest.json index 6d066e3..c0809a4 100644 --- a/chrome/manifest.json +++ b/chrome/manifest.json @@ -37,7 +37,8 @@ "activeTab", "storage", "contextMenus", - "scripting" + "scripting", + "downloads" ], "host_permissions": [ "*://arxiv.org/*", diff --git a/firefox/background.js b/firefox/background.js index 114b4cf..5d934df 100644 --- a/firefox/background.js +++ b/firefox/background.js @@ -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) { @@ -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( diff --git a/firefox/content.js b/firefox/content.js index ea9eaa6..29bf651 100644 --- a/firefox/content.js +++ b/firefox/content.js @@ -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 = ` \ -
  • \ - Direct Download \ +
  • \ + Direct Download \
  • `; const downloadUL = document.querySelector(".full-text > ul"); if (!downloadUL) { @@ -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) { diff --git a/firefox/manifest.json b/firefox/manifest.json index 3b933bf..219ed1d 100644 --- a/firefox/manifest.json +++ b/firefox/manifest.json @@ -38,6 +38,7 @@ "webRequest", "webRequestBlocking", "bookmarks", + "downloads", "*://arxiv.org/*pdf*", "*://export.arxiv.org/*pdf*", "*://browse.arxiv.org/*pdf*"