Scripting System Overview (2.0)

HDoujin Downloader supports the use of custom scripts, so that you can you can add any site you want, or edit functionality for sites that are already supported by default. Of course, you can always ask me to do that for you, too.

This page will help you get started with writing your own scripts. I recommend using the HDD Script Editor, as it will allow you to test your scripts before using them. It also comes with a couple of example scripts to give you an idea of how they work.

The new scripting system functions a lot more like a legitimate scripting language than the previous iteration, and allows you to set your own variables, use if statements and loops, and modify results with a bunch of built-in functions.

Getting Started

Scripts are divided into three sections: info, where you fetch information about a manga and add it to the list; pages, where you fetch a list of pages to download; and chapters, where you fetch a list of chapter URLs/titles. These sections are specified using #info, #pages, and #chapters headings. Any code after these lines will be executed at the applicable time, up until the next heading (or the end of the script).

Functions

The main functions to be aware of are list_add, page_add, and chapter_add. They are used for adding an item to the download queue, adding a page to the page list, and adding a chapter to the chapter list, respectively. They are generally used at the end of a section (or repeatedly through loops in the case of the last two). They are used like so, where each parameter is a variable declared/assigned to before calling the function:

list_add(faviconURL, title, pages, chapters, site, tags, desc, author, artist)
page_add(pageURL)
chapter_add(chapterTitle, chapterURL)

A complete list of available functions is below:

between
(a, b, c) // returns substring in ‘a’ between substrings ‘b’ and ‘c’
after(a, b) // returns substring in ‘a’ after ‘b’
before(a, b) // returns substring in ‘a’ before ‘b’
count(a, b) // returns number of substring ‘b’ present in ‘a’
contains(a, b) // returns true if ‘a’ contains substring ‘b’; false otherwise
replace(a, b, c) // returns ‘a’ with all instances of substring ‘b’ replaced with substring ‘c’
mtags(a, b, c) // creates a comma-delimited list of tags present in ‘a’, between bounds ‘b’ and ‘c’
trim(a) // removes excess white-space surrounding ‘a’
repair(a) // returns ‘a’ with HTML character encoding repaired
substring(a, b, c) // returns a substring of ‘a’ from index ‘b’ to length ‘c’
last_index_of(a, b) // returns the index of the last instance of substring ‘b’ in ‘a’
index_of(a, b) // returns the first index of substring ‘b’ in ‘a’
length(a) // returns the length of string ‘a’

pad(a, b) // pads number ‘a’ with zeroes until it is length ‘b’
ceil(a) // returns ceiling of number ‘a’
floor(a) // returns floor of number ‘a’
rnd(a) // returns rounded of number ‘a’

simplify(a) // simplifies a URL (i.e., eliminates “http” and directories)
filename(a) // returns the file name of URL
extension(a) // returns the extension of file name
get(a) // returns source code of webpage at address ‘a’

page_reverse() // reverses page order
chapter_reverse() // reverses chapter order

log(a) // (editor-only) writes value ‘a’ to the script processing log

Variables

You can declare variables using the var keyword, followed by the names of the variables you would like to use, separated by commas. For example:

var favicon, title, pages, chapters, site = “FAKKU!”, tags, desc, author, artist

You can also assign values to variables when declaring them, as seen above with the ‘site’ variable. Note that you don’t necessarily need to declare variables in this way (they’ll be created automatically if you assign a value to a non-existent variable), but it’s safer to do it this way.

The following “constants” also exist: url, src, true, false, newline

I put “constants” in quotation marks because you can change the values of url and src, which refer to the URL of the manga being downloaded and the source code retrieved from the page it’s found on, respectively. In a lot of cases, you’ll need to modify these values. Of course, you could also assign them to a new variable if you want to.

If, While, and Loop

if statements will allow you to test a condition before running code that is encapsulated in curly braces. If the condition is false, the code won’t be run.

while loops will continue to run as long as the condition is true, and loop will run a specified number of iterations.

Note that all statements require that the condition be in parentheses directly following the keyword, along with an opening curly brace either on the next line, or following the condition.

Operators

The following operators are supported:
For numeric values: +, -, /, , *, <, >, %, mod, ^
For Boolean values: &&, ||, ==, !=, !, <>, not, or, and
For strings: &
Assignment: = (can be combined with some other operators)

Be sure to check out the example scripts that come with the editor! Some of them have comments to help you understand each line (if you need it).

8 thoughts on “Scripting System Overview (2.0)

      1. DoujinDownloader Post author

        I know you mentioned you already figured things out, but I’ll provide an example loop anyway. It will extract all “pages” in between the sets of braces, adding each one to the page list (and also outputting it to the output log if you’re using the script editor). “src” is not a constant, so you’re free to modify it as needed:

        src = "[Page 1][Page 2][Page 3]";
        var page_url;
        while(contains(src, "]")) {
        page_url = between(src, "[", "]");
        src = after(src, "]");
        page_add(page_url);
        log(page_url);
        }

        If you knew you were going to have exactly 3 pages to extract, you could use the loop keyword instead, replacing while(contains(src, "]")) with loop(3) to run the loop 3 times.

        Reply
    1. AnoPem

      I found out how to grab images from same page, and would like to make a request for the program and script editor,

      In the script editor.
      add some sort of exception so the program wont crash upon faulty code.
      and please specify a way to make script download to subfolder.

      Sorry for spamming.
      I just love the opportunities this program brings with custom scripts.

      Reply
      1. DoujinDownloader Post author

        No worries, feedback is always welcome, and I’m glad you find it useful!

        The editor is indeed a big buggy. I do have plans to make the scripting language more powerful as well, and I’ll make sure to allow users to have more control over directories when I get around to doing that. The language itself isn’t perfect, and I want to take the time at some point to rewrite the interpreter and improve the syntax. I’ve got a few other major updates to deal with first, but it’s on my to-do list!

        Reply
  1. Ano

    I seems to have problems with pad()

    when used with interval like pad(i, 2) it returns blank value

    Reply
    1. DoujinDownloader Post author

      Thanks for letting me know! It should be working now in the latest release.

      I haven’t updated the script editor, so if you’re using that, it’s still going to show as blank. However, it will work in HDoujin (for scripts or custom tokens) as expected.

      Reply
  2. Pingback: Updated Scripting System & Other News | HDoujin Downloader

Leave a Reply

Your email address will not be published.