如何使用 VBS 脚本自动登录 [英] How to Auto Login using VBS script

查看:61
本文介绍了如何使用 VBS 脚本自动登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是新手.我目前正在我们办公室的一个项目中工作,我已经有了如何在一个窗口中打开多选项卡 IE 的脚本.这是我使用的脚本.

I'm just new to this. I am currently working for a project here in our office and I already have the script on how to open multi-tab IE in one window. Here is the script that I use for it.

Const navOpenInBackgroundTab = &H1000

site1 = "http://site1.com/"
site2 = "https://site2.com"



Set oIE = CreateObject("InternetExplorer.Application")
oIE.Visible = True
oIE.Navigate2 site1
oIE.Navigate2 site2,navOpenInBackgroundTab



Set oIE = Nothing 

但是其中两个网站需要登录信息.我想知道允许我自动登录这些网站的脚本.我想知道我需要在上面的脚本中添加什么,以便每次使用该脚本时它都会自动登录.

But two of these websites needs log in information. I want to know the script that will allow me to auto log in to these websites. I was wondering what I need to add on the script above so that it will auto-log in every time I use the script.

这是其中一个网站:https://myapps.uhc.com/Citrix/AccessPlatform/auth/login.aspx

希望有人能帮我解答一下,谢谢

I hope someone can help me figure it out, thanks

推荐答案

长话短说,使用 Firefox + Greasemonkey 或 Chrome + Tampermonkey 会更轻松.

Long story short, you'd have an easier time using Firefox + Greasemonkey or Chrome + Tampermonkey.

枚举后台选项卡需要使用Shell.Application 对象.Windows() 方法.模棱两可的窗口对象似乎破坏了 DOMelement.parentNode 和类似的东西,因此每次导航 DOM 时,都必须重新建立从 tabCollection.item(i).document 开始的完整层次结构.用户名字段是否命名为用户、用户名、登录名、登录 ID、电子邮件或其他名称?您可以尝试使用正则表达式进行预测,但如果您打算扩展此脚本以登录其他站点,则可能会涉及一些试验和错误.

Enumerating background tabs requires the use of Shell.Application object .Windows() method. The ambiguous window object seems to break DOMelement.parentNode and similar, so that each time you navigate the DOM you have to re-establish the complete hierarchy starting with tabCollection.item(i).document. Is the username field named user, username, login name, login ID, email, or something else? You can try to predict it with a regex, but there'll probably be some trial and error involved if you plan to extend this script for logging into other sites.

不过,不可能的部分是表单提交.表单提交是否调用回发以在发布之前设置隐藏的输入值?如果是这样,那么 form.submit() 可能不会在提交之前触发必要的事件,从而导致身份验证失败.我测试的两个网站就是这种情况.

The impossible part, though, is form submission. Does the form submission call a postback to set hidden input values prior to post? If so, then form.submit() might not fire the necessary events before submission, resulting in an auth failure. This was the case for two sites I tested.

如果您可以触发提交按钮的 click() 事件,那可能会有所帮助.但是提交按钮是一个按钮吗?输入类型=按钮?一个图像?程式化的超链接?一个div?好的,所以也许在密码字段中发送 Enter 会更好.我已经用 initKeyboardEventpasswordField.dispatchEvent(evt) 试验了几个小时,似乎又是模棱两可的 windowShell.Application 产生的 code> 对象正在阻止成功.

If you could fire the click() event of the submit button, that might help. But is the submit button a button? An input type=button? An image? A stylized hyperlink? A div? OK, so maybe sending Enter in the password field would be better. I've been experimenting with initKeyboardEvent and passwordField.dispatchEvent(evt) for a couple of hours now, and it seems that here again, the ambiguous window object resulting from Shell.Application is preventing success.

这是我的几乎-可行的解决方案.它是批处理 + Jscript 的混合体.使用 .bat 扩展名保存它,然后随心所欲地使用它.*耸耸肩*也许 form.submit() 将适用于您正在使用的网站?

Here's my almost-working solution. It's a batch + Jscript hybrid. Save it with a .bat extension, and do with it what you will. *shrug* Maybe form.submit() will work for the sites you are using?

@if (@CodeSection == @Batch) @then
@echo off & setlocal

cscript /nologo /e:JScript "%~f0"
goto :EOf

@end // end Batch / begin JScript hybrid code

var navOpenInBackgroundTab = 4096,
    site = [
        "http://www.site1.com/",
        "http://www.site2.com/"
    ],
    IE = WSH.CreateObject("InternetExplorer.Application"),
    oSH = WSH.CreateObject("Shell.Application"),
    username = 'username',
    password = 'password';

IE.Visible = true;

for (var i=0; i<site.length; i++) {
    IE.Navigate(site[i], i ? 4096 : null);
}

var tabs = oSH.Windows();

for (var x = 0; x < tabs.Count; x++) {
    if (!!(tabs.item(x))) {
        WSH.Echo('tab ' + x);
        var entered = { 'username': 0, 'password': 0 };
        while (tabs.item(x).Busy || tabs.item(x).ReadyState != 4) WSH.Sleep(50);
        // sleep 1 second regardless, just in case there's some jquery crap going on
        WSH.Sleep(1000);
        for (var i in tabs.item(x).document.forms) {
            if (i == 'length') break;
            var inputs = tabs.item(x).document.forms[i].getElementsByTagName('input');
            for (var j = inputs.length; j--;) {
                if (/(login|user(name|id)?|e-?mail)/i.test(inputs[j].name)
                && inputs[j].type.toLowerCase() == "text") {
                    entered.username = inputs[j];
                    inputs[j].value = username;
                } else if (inputs[j].type.toLowerCase() == 'password') {
                    entered.password = inputs[j];
                    inputs[j].value = password;
                }
            }
            if (entered.username && entered.password) {
                tabs.item(x).document.forms[i].submit();

                /* === NOT QUITE WORKING ===
                try {
                    var evt = tabs.item(x).document.createEvent('KeyboardEvent');
                    // What is "window"? tabs.item(x) doesn't work, nor does IE.
                    evt.initKeyboardEvent('keypress', true, true, tabs.item(x),
                        false, false, false, false, 13, 13);
                    entered.password.dispatchEvent(evt);
                }
                catch(e) { WSH.Echo(e.message) }
                */

                i = tabs.item(x).document.forms.length;
            }
        }
    }
}

var IE = null;

另一种想法是执行 passwordElement.focus() 并使用 WshShell.SendKeys() 发送 Enter,但是您将无法加载后台标签.我还没有找到以编程方式激活选项卡的方法;但如果它们被加载到前台,它们就已经处于活动状态.虽然这不会在后台加载选项卡,但它比第一种方法效果更好.

Another idea would be to do passwordElement.focus() and use WshShell.SendKeys() to send Enter, but then you wouldn't be able to load the tabs in the background. I haven't found a way to activate tabs programmatically yet; but if they're loaded in the foreground, they're already active. Although this doesn't load tabs in the background, it works better than the first method.

@if (@CodeSection == @Batch) @then
@echo off & setlocal

cscript /nologo /e:JScript "%~f0"
goto :EOf

@end // end Batch / begin JScript hybrid code

var sites = {
        "http://www.site1.com/" : {
            'username': 'site1user',
            'password': 'site1pass'
        },
        "http://www.site2.net/" : {
            'username': 'site2user',
            'password': 'site2pass'
        }
    },
    IE = WSH.CreateObject("InternetExplorer.Application"),
    oSH = WSH.CreateObject("Shell.Application"),
    WshShell = WSH.CreateObject("WScript.Shell"),
    proc = GetObject("winmgmts:").ExecQuery("SELECT Handle FROM Win32_Process "
        + "WHERE Name='iexplore.exe'"),
    handle = new Enumerator(proc).item().Handle;

awesomeness:
for (var url in sites) {

    // if not a new window, open a new tab
    IE.Navigate(url, IE.Visible ? 2048 : null);
    IE.Visible = true;

    // give the tab a chance to load
    WSH.Sleep(1000);

    var tabs = oSH.Windows(),
        tab = tabs.item(tabs.Count - 1),
        entered = { 'username': 0, 'password': 0 };

    while (tab.Busy || tab.ReadyState != 4) WSH.Sleep(50);

    for (var i in tab.document.forms) {
        if (i == 'length') break;
        var inputs = tab.document.forms[i].getElementsByTagName('input');
        for (var j = inputs.length; j--;) {
            if (/(login|user(name|id)?|e-?mail)/i.test(inputs[j].name)
            && inputs[j].type.toLowerCase() == "text") {
                entered.username = inputs[j].value = sites[url].username;
            } else if (inputs[j].type.toLowerCase() == 'password') {
                entered.password = inputs[j];
                inputs[j].value = sites[url].password;
            }
            if (entered.username && entered.password) {
                // force IE window to have focus
                while (!(WshShell.AppActivate(handle))) WSH.Sleep(50);
                entered.password.focus();
                WshShell.SendKeys('{END}{ENTER}');
                continue awesomeness;
            }
        }
    }
}

var IE = null;

这篇关于如何使用 VBS 脚本自动登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆