Skip to content

Variables In Download Path

Haryfw edited this page Aug 28, 2018 · 3 revisions

下载路径中的变量

Variables In Download Path

Path containing variables

Use a variable name in the form of ${XXX} (note that they are not template strings in es6, just look similar)

For example ${today} is replaced with 2018-01-01; ${host} is replaced with example.com

Variable list

Variable Description
year Year, format: YYYY
month Month, format: MM
date Number of days in a month, format:DD
today Combination of the above three values, format: YYYY-MM-DD
host Website domain,like: example.com
pagetitle Page title

Note: The file name is automatically appended at the end of the path

Example:

Input After conversion
${today} Download path/2018-01-01fillename
picture/${host}/ Download path/picture/example.com/filename
data/${today}@ Download path/data/2018-01-01@fillename

JS function

The last entry in the custom directory is to determine the final download path based on the return value of the JS function.

You can only use this feature when downloading images.

The input should be a piece of JS code, and it is a normal anonymous function, no parameters are passed in.

(This code will be inserted directly into the page, so you can use any of these global variables like window or location.)

function(){
  return something;
}

In addition to the variables in the list mentioned above, you can also use the following additional variables

Variable Description
index The number of downloads on the current page, format: DDDD. Cleared after the page is refreshed
url Image link
filename File name

When the type returned by this function is string, its value is the value of options.filename. This means you need to specify the file name manually, but the extension will handle the other parameters of the option.

When the returned type is object, it is passed directly to browser.downloads.download() as a parameter.

You can refer to the introduction of the browser.downloads.download parameter in this MDN documentation

For example, I want the download path to be "domain name/file name". Then the code can be written like this (of course you can also use the es6 template string syntax)

function(){
  return host + '/' + filename;
}

Or save the image to a different directory based on the website.

function(){
  if(host=='i.imgur.com') return 'imgur' + '/' + filename;
  if(host.endsWith('pixiv.net')) return 'acg' + '/' + filename;
  return 'interesting'+ '/'+ filename;
}