Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

SmartDownloadManager

0.00/5 (No votes)
15 May 2007 1  
Create download managers using VBScript

Screenshot - SmartDownloadManager1.gif

Screenshot - SmartDownloadManager2.gif

Introduction

This article gives some basic information about how to create your own download managers.

Procedure

There are four different steps that must be followed for the application to work. They are:

  1. Adding your own item to the Right Click menu of Internet Explorer.
  2. Gathering required information from the Page using VBScript files, Javascript files or HTML files when the user clicks the added item.
  3. Launching the main application that does the actual downloading.
  4. Programming the main application to download the target file.

Step 1: Adding our own item to the Right Click menu of the Internet Explorer

Internet Explorer offers a built-in registry key to enable the addition of new menu items to the right click menu.

HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\MenuExt

Adding a new subkey to the above key will show a new item in the right click menu of Internet Explorer. Let us add a new key called "Download Now." We have to add the path of a script file to the default value of the key. Internet Explorer launches the script file when the item is clicked.

Step 2: Gathering required information from the Page

The script file should gather information such as the URL, the element which the user right clicked, and so on. This file is launched when the user selects the added item.

    var path = document.location.pathname;
    var Exepath = path.replace("SmartDownloadManager.htm",
        "SmartDownloadManager.exe");
    var Doc   = external.menuArguments.document;
    var Event = external.menuArguments.event;
    var mainURL   = external.menuArguments.document.URL;
    var Element = Doc.elementFromPoint(Event.clientX, Event.clientY);
    var Anchor  = Element;
    var cookie = Doc.cookie;
    while( Anchor.tagName!="A" && Anchor.tagName!="HTML" ) 
    {
        Anchor = Anchor.parentElement;
    }

    if(Anchor.tagName=="A") 
    {
        lnkURL  = Anchor.href; 
        var oShell = new ActiveXObject("Shell.Application");
        var commandtoRun = Exepath;
        oShell.ShellExecute(commandtoRun, 
            "\""+mainURL+"\"" + " \"" + cookie+"\" \"" + lnkURL + "\"","",
            "open", "1");
    }

The above code retrieves the element on which the user has made the right click and stores the reference in the variable Element. A simple while loop is used to check whether the anchor tag has any nested tags, like images, or any other formatting tags. Finally, a simple if condition checks whether the user has chosen the anchor tag or the blank page. It launches the application and passes the required information to the application as command line arguments.

Step 3: Launching the main application

    var oShell = new ActiveXObject("Shell.Application");
    oShell.ShellExecute(commandtoRun, "\""+mainURL+"\"" 
        + " \"" + cookie+"\" \"" + lnkURL + "\"","", "open", "1");

The above code launches the main application which does the actual downloading and passes such information as: main page URL, the URL of the link, etc.

Step 4: Programming tha main application to download the target file

    static void Main(string[] args)

The arguments that are supplied in Step 3 are stored in the array args of the Main function. Using these arguments, we can easily create a program that downloads the target file. A lot of articles can be found on this topic on the CodeProject.com website itself.

Conclusion

Copy the executable folder into any location. There is a file called SetUp.exe in the executable folder. This file has to be executed for the first time and will create proper registry values. Once the file is executed, the location of the files should not be changed, as it will not work properly. If the location of the folder gets changed, the registry of Download Now has to be manually deleted. After deleting the registry, you can safely execute SetUp.exe again to make the new registry values.

History

  • 15 May, 2007 - Original version posted

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here