jc3213 / download_with_aria2

The browser extenson for aria2 download utility via JSON-RPC
https://jc3213.github.io/download_with_aria2/
GNU Lesser General Public License v2.1
65 stars 9 forks source link

Use tab title as filename in Browser Chromium based and Firefox #39

Closed candrapersada closed 2 years ago

candrapersada commented 2 years ago

will there be an option for how to download use tab title as filename?

jc3213 commented 2 years ago

I just wonder why? Will it be always or some rule based? It is hard to do so since my extension don't work as Aria2-Integration which prompt a new window and ask user to confirm the downloads

jc3213 commented 2 years ago

For temporary work around, you can

  1. Copy the codes below, change info into yours
    {"url": "YourDownloadURL", "options": {"out": "YourFileNameWithExtension","referer": "CurrentTabURL"}}
  2. Open New Download Window, and set Download Urls to JSON then paste the info into the textarea box
  3. Click Submit to start the download
candrapersada commented 2 years ago

how to use tab title as filename in Capture browser downloads like *page_title*.*ext*?

jc3213 commented 2 years ago

In Firefox downloads API https://github.com/jc3213/download_with_aria2/blob/9bbbcc82773896a22c6adda59148f8e59feb7904/firefox/background.js#L97 webRequest API https://github.com/jc3213/download_with_aria2/blob/9bbbcc82773896a22c6adda59148f8e59feb7904/firefox/background.js#L84 In Chromium https://github.com/jc3213/download_with_aria2/blob/9bbbcc82773896a22c6adda59148f8e59feb7904/chromium/background.js#L49 In Chromium Manifest V3 https://github.com/jc3213/download_with_aria2/blob/9bbbcc82773896a22c6adda59148f8e59feb7904/chromium_mv3/background.js#L39

Aria2 option out defines download filename. so I used variable out to do so.

To query tab title, you'd need tabs api as https://github.com/jc3213/download_with_aria2/blob/9bbbcc82773896a22c6adda59148f8e59feb7904/firefox/background.js#L59 or https://github.com/jc3213/download_with_aria2/blob/9bbbcc82773896a22c6adda59148f8e59feb7904/chromium_mv3/background.js#L35

jc3213 commented 2 years ago

There will be an issue that if downloads will be start in some time (like sourceforge, in 5 seconds), and you are visiting another tab, the result tab title will be messed up as current tab title.

EDIT: I've tested that, it's easy to add an option, but you may need to turn it on/off manually for usage. It's not recommended to do so.

jc3213 commented 2 years ago

Take chromium branch for example, here's the changes. Firefox one needs some trick so.

You need to these steps for the codes to take effect

  1. Open Extentions Manager
  2. Turn on debug mode and enter debug console.
  3. Type aria2Store['use_titlename'] = '1'; and press Enter

Reference based on https://github.com/jc3213/download_with_aria2/tree/76923b5e6f95249f8595aebc8c8796e0c448a7bb

chrome.contextMenus.onClicked.addListener((info, tab) => {
    var {linkUrl, pageUrl} = info;
    var out = aria2FileName(null, tab.title);
    aria2Download(linkUrl, getHostname(pageUrl), {referer: pageUrl, out});
});
    chrome.tabs.query({active: true, currentWindow: true}, ([tab]) => {
        var referer = 'about:blank'.includes(referrer) ? tab.url : referrer;
        var hostname = getHostname(referer);
        if (getCaptureFilter(hostname, getFileExtension(filename), fileSize)) {
            chrome.downloads.erase({id});
            var out = aria2FileName(filename, tab.title);
            aria2Download(finalUrl, hostname, {referer, out: filename});
        }
    });
function aria2FileName(filename, title) {
    if (aria2Store['use_titlename'] === '1') {
        if (filename) {
            var ext = getFileExtension(filename);
            return title + '.' + ext;
        }
        return title;
    }
    return filename;
}
candrapersada commented 2 years ago

You need to these steps for the codes to take effect

is there any option or button to use it right away without needing these steps?

jc3213 commented 2 years ago

I won't implement this into the main branch, since it's kind of conflict with my code. It's better that you switch to Aria2-Integration and ask the developer to implement this feature.