Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

下载最新版7z脚本 #2

Open
bajins opened this issue Nov 4, 2019 · 2 comments
Open

下载最新版7z脚本 #2

bajins opened this issue Nov 4, 2019 · 2 comments

Comments

@bajins
Copy link

bajins commented Nov 4, 2019

1>1/* ::
:: by bajins https://www.bajins.com

@ECHO OFF
color 0a
Title FRPC启动工具 by:bajins.com
:: 窗口高宽40*120
REG ADD "HKEY_CURRENT_USER\Console" /t REG_DWORD /v WindowSize /d 0x00280078 /f >nul
:: 屏幕缓冲区高宽1000*120
REG ADD "HKEY_CURRENT_USER\Console" /t REG_DWORD /v ScreenBufferSize /d 0x03e80078 /f >nul

:: 开启延迟环境变量扩展
:: 解决for或if中操作变量时提示ECHO OFF问题,用!!取变量
:: 解决调用jscript提示命令错误问题
setlocal enabledelayedexpansion

cscript -nologo -e:jscript "%~f0"


goto :EXIT

:EXIT
:: 结束延迟环境变量扩展和命令执行
endlocal&exit /b %errorlevel%

*/

// ****************************  JavaScript  *******************************


var Argv = WScript.Arguments;
for (i = 0; i < Argv.length; i++) {
    info("参数:" + Argv(i));
}
var ArgvName = Argv.Named;

download7z()


/**
 * 下载7-Zip
 */
function download7z() {
    var srcUrl = "https://sourceforge.net/projects/sevenzip/rss?path=/7-Zip";
    var txt = request("get", srcUrl, "text", "", "");
    var dlUrl = "https://www.7-zip.org/a/";
    var filename;
    try {
        var xmlDoc = XMLParser(txt);
        var links = xmlDoc.getElementsByTagName("link");
        if (links.length <= 0) {
            txt = request("get", srcUrl, "text", "", "");
        }
        var link = links[5].firstChild.nodeValue.split("/");
        filename = link[link.length - 2];
    } catch (e) {
        WScript.StdOut.WriteLine(e.message);
        srcUrl = "https://www.7-zip.org/download.html";
        txt = request("get", srcUrl, "text", "", "");
        var html = HtmlParsing(txt);
        var tbody = html.getElementsByTagName("tbody")(4);
        var href = tbody.children(3).firstChild.firstChild.getAttribute("href");
        href = href.split("/");
        filename = href[href - 1];
    }
    dlUrl += filename;
    // 当前文件所在目录
    var currentDirectory = fso.GetFile(WScript.ScriptFullName).ParentFolder;
    try {
        download(dlUrl, currentDirectory, filename);
    } catch (e) {
        throw new Error("下载7z错误:" + e.message);
    }
}


/**
 * HTTP请求
 *
 * @param method        GET,POST
 * @param url           请求地址
 * @param dataType      "",text,stream,xml,json
 * @param data          数据,{key:value}格式
 * @param contentType   发送的数据类型:multipart/form-data、
 * application/x-www-form-urlencoded(默认)、text/plain
 * @returns {string|Document|any}
 */
function request(method, url, dataType, data, contentType) {
    if (url == "" || url == null || url.length <= 0) {
        throw new Error("请求url不能为空!");
    }
    if (method == "" || method == null || method.length <= 0) {
        method = "GET";
    } else {
        // 把字符串转换为大写
        method = method.toUpperCase();
    }
    if (contentType == "" || contentType == null || contentType.length <= 0) {
        contentType = "application/x-www-form-unlenconded;charset=utf-8";
    }
    var XMLHTTPVersions = [
        'WinHttp.WinHttpRequest.5.1',
        'WinHttp.WinHttpRequest.5.0',
        'Msxml2.ServerXMLHTTP.6.0',
        'Msxml2.ServerXMLHTTP.5.0',
        'Msxml2.ServerXMLHTTP.4.0',
        'Msxml2.ServerXMLHTTP.3.0',
        'Msxml2.ServerXMLHTTP',
        'MSXML2.XMLHTTP.6.0',
        'MSXML2.XMLHTTP.5.0',
        'MSXML2.XMLHTTP.4.0',
        'MSXML2.XMLHTTP.3.0',
        'MSXML2.XMLHTTP',
        'Microsoft.XMLHTTP'
    ];
    var XMLHTTP;
    for (var i = 0; i < XMLHTTPVersions.length; i++) {
        try {
            XMLHTTP = new ActiveXObject(XMLHTTPVersions[i]);
            break;
        } catch (e) {
            WScript.StdOut.Write(XMLHTTPVersions[i]);
            WScript.StdOut.WriteLine("" + e.message);
        }
    }

    //将对象转化成为querystring形式
    var paramarray = [];
    for (key in data) {
        paramarray.push(key + "=" + data[key]);
    }
    var params = paramarray.join("&");

    switch (method) {
        case "POST":
            // 0异步、1同步
            XMLHTTP.Open(method, url, 0);
            XMLHTTP.SetRequestHeader("CONTENT-TYPE", contentType);
            XMLHTTP.Send(params);
            break;
        default:
            // 默认GET请求
            if (params == "" || params.length == 0 || params == null) {
                // 0异步、1同步
                XMLHTTP.Open(method, url, 0);
            } else {
                XMLHTTP.Open(method, url + "?" + params, 0);
            }
            XMLHTTP.SetRequestHeader("CONTENT-TYPE", contentType);
            XMLHTTP.Send();
    }

    // 把字符串转换为小写
    dataType = dataType.toLowerCase();
    switch (dataType) {
        case "text":
            return XMLHTTP.responseText;
            break;
        case "stream":
            return XMLHTTP.responseStream;
            break;
        case "xml":
            return XMLHTTP.responseXML;
            break;
        case "json":
            return eval("(" + XMLHTTP.responseText + ")");
            break;
        default:
            return XMLHTTP.responseBody;
    }
}


/**
 * 下载文件
 *
 * @param url
 * @param directory 文件存储目录
 * @param filename  文件名,为空默认截取url中的文件名
 * @returns {string}
 */
function download(url, directory, filename) {
    if (url == "" || url == null || url.length <= 0) {
        throw new Error("请求url不能为空!");
    }
    if (directory == "" || directory == null || directory.length <= 0) {
        throw new Error("文件存储目录不能为空!");
    }

    var fso = new ActiveXObject("Scripting.FileSystemObject");
    // 如果目录不存在
    if (!fso.FolderExists(directory)) {
        // 创建目录
        var strFolderName = fso.CreateFolder(directory);
    }

    if (filename == "" || filename == null || filename.length <= 0) {
        filename = url.substring(url.lastIndexOf("/") + 1);
        // 去掉文件名的特殊符号(包括之前的)字符
        filename = filename.replace(/^.*(\&|\=|\?|\/)/ig, "");
    }
    var path = directory + "\\" + filename;

    var ADO = new ActiveXObject("ADODB.Stream");
    ADO.Mode = 3;
    ADO.Type = 1;
    ADO.Open();
    ADO.Write(request("GET", url, ""));
    ADO.SaveToFile(path, 2);
    ADO.Close();

    // 如果文件不存在
    if (!fso.FileExists(path)) {
        return "";
    }
    return path;
}

/**
 * 解析XML
 *
 * @param xml xml字符串或者文件路径
 * @returns {*}
 * @constructor
 */
function XMLParser(xml) {
    if (xml == "" || xml == null || xml.length <= 0) {
        throw new Error("xml字符串或者文件路径不能为空!");
    }
    var xmlDomVersions = [
        'Msxml2.DOMDocument.6.0',
        'MSXML2.DOMDocument.3.0',
        'Microsoft.XMLDOM'
    ];
    var xmlDoc;
    for (var i = 0; i < xmlDomVersions.length; i++) {
        try {
            xmlDoc = new ActiveXObject(xmlDomVersions[i]);
            break;
        } catch (e) {
            WScript.StdOut.Write(xmlDomVersions[i]);
            WScript.StdOut.WriteLine("" + e.message);
        }
    }
    xmlDoc.async = false;
    var fso = new ActiveXObject("Scripting.FileSystemObject");
    if (!fso.FileExists(xml)) {
        // 载入xml字符串
        xmlDoc.loadXML(xml);
        return xmlDoc;
    }
    // 载入xml文件
    xmlDoc.load(xml);
    return xmlDoc;
}

/**
 * 解析HTML
 *
 * @param html html字符串或者文件路径
 * @returns {any}
 * @constructor
 */
function HtmlParsing(html) {
    if (html == "" || html == null || html.length <= 0) {
        throw new Error("html字符串或者文件路径不能为空!");
    }
    var fso = new ActiveXObject("Scripting.FileSystemObject");
    if (fso.FileExists(html)) {
        var htmlFile = fso.OpenTextFile(html, ForReading);
        html = htmlFile.ReadAll;
        htmlFile.Close();
    }
    // mhtmlfile
    var htmlParser = new ActiveXObject("htmlfile");
    htmlParser.designMode = "on";
    htmlParser.write(html);
    return htmlParser;
}
@Mapaler
Copy link
Owner

Mapaler commented Nov 4, 2019

这个也搞得太复杂了吧
而且我也看不懂,对Winwods层面的我不了解。

但是能看出您是JScript高手,需要指出的是 JScript != JavaScript 。

@bajins
Copy link
Author

bajins commented Nov 5, 2019

这个也搞得太复杂了吧
而且我也看不懂,对Winwods层面的我不了解。

但是能看出您是JScript高手,需要指出的是 JScript != JavaScript 。

哈,其实一点都不复杂,只不过是为了高可用性用函数封装了而已,对于batch执行JScript你可以看这里 https://www.bajins.com/VPS/Batch%E6%89%A7%E8%A1%8CWScript.html

VBScript和JScript都是属于WScript,在同一个宿主下执行

https://www.bajins.com/VPS/WindowsScript.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants