有没有办法以编程方式登录 Google Chrome?

有没有办法以编程方式登录 Google Chrome?

我需要将 Google Chrome 登录到大量计算机上的 Google 应用域。每台计算机都可以访问其用户名和临时密码。有没有办法自动登录 Chrome?最好不要安装更多软件。

到目前为止,我尝试使用 Visual Basic 脚本SendKeys方法模拟按键来登录。虽然这种方法确实有效,但当 Chrome 更新、计算机运行缓慢或任何其他不可预见的情况发生时,它经常会中断。

答案1

这更像是一种权宜之计,而不是解决方案,所以我不会将其标记为解决方案。

目前,我正在通过让程序生成并运行 vbs 文件来输入用户名和密码来解决此问题。然后,我让另一个程序检查 Chrome 的首选项文件以查看它是否已成功配置。如果没有,则重新运行脚本。

chromeConf.vbs(我编写了另一个程序自动生成此文件并在每台计算机上运行它):

Set oShell = CreateObject("WScript.Shell")
oShell.SendKeys "^{ESCAPE}" 'start menue
WScript.Sleep 1000 'wait for it to load
oShell.SendKeys "chrome.exe" 'chrome
WScript.Sleep 1000
oShell.SendKeys "{ENTER}"
return = oShell.Run("waitForFocus.exe Chrome", 0, true)'wait for Chrome to open
oShell.SendKeys "%{F4}" 'go! be gone!
'Chrome has to be started twice to ensure the same start state on all computers
oShell.SendKeys "^{ESCAPE}" 'start menue
WScript.Sleep 1000 'wait for it to load
oShell.SendKeys "chrome.exe" 'chrome
WScript.Sleep 1000
oShell.SendKeys "{ENTER}"
return = oShell.Run("waitForFocus.exe Chrome", 0, true)
oShell.SendKeys "chrome://settings" 'settings
oShell.SendKeys "{ENTER}"
WScript.Sleep 5000
oShell.SendKeys "{TAB}{TAB}" 'select log in
oShell.SendKeys "{ENTER}"
WScript.Sleep 8000
oShell.SendKeys "{TAB}{TAB}" 'log in
oShell.SendKeys "[email protected]"
oShell.SendKeys "{TAB}"
oShell.SendKeys "{ENTER}"
WScript.Sleep 4000
oShell.SendKeys "password"
oShell.SendKeys "{TAB}"
oShell.SendKeys "{ENTER}"
WScript.Sleep 4000
oShell.SendKeys "{TAB}" 'link data
oShell.SendKeys "{TAB}"
oShell.SendKeys "{TAB}"
oShell.SendKeys "{TAB}"
oShell.SendKeys "{ENTER}"
WScript.Sleep 4000
oShell.SendKeys "{ESCAPE}" 'get rid of pesky user dialog
oShell.SendKeys "%{F4}" 'go! be gone!

waitForFocus.exe 来源:

// waitForFocus.cpp : This program waits for a window of a specified name to load
//

#include "stdafx.h"
#include <string.h>
#include <atlstr.h>
#include <iostream>

using namespace std;

LPWSTR pszMem;

BOOL CALLBACK FindWindowBySubstr(HWND hwnd, LPARAM substring)
{
    const DWORD TITLE_SIZE = 1024;
    TCHAR windowTitle[TITLE_SIZE];

    if (GetWindowText(hwnd, windowTitle, TITLE_SIZE))
    {
        string fstr = CW2A(windowTitle);//convert window title to string
        if (fstr.find(LPCSTR(substring)) != string::npos && !(fstr.find("waitForFocus.exe") != string::npos)) { //is it what we want
            cout << "Found window!" << endl;
            _tprintf(TEXT("%s\n"), windowTitle);
            SwitchToThisWindow(hwnd, true);//The true enables alt tab emulation which prevents the transparent window bug
            return false;
        }
    }
    return true;
}

int main(int argc, char* argv[])
{
    if (argc > 2) {
        cout << "This program takes 1 argument" << endl;
        cout << "The argument should be part of the name of the window you want to wait for" << endl;
    }
    else if (argc == 2) {
        if (string(argv[1]) == "/h" || string(argv[1]) == "-h" || string(argv[1]) == "/?" || string(argv[1]) == "-?"){
            cout << "This program takes one input, part of the window name, and waits for that window to load" << endl;
        }
        else {
            bool nfound = true;
            while (nfound) {
                HWND windowHandle = FindWindowA(0, argv[1]); //check if there is a window exactly matching what we want
                if (windowHandle == NULL) {//no
                    HWND WINAPI GetForegroundWindow(void);//is the window already up (If so I dont have to look for it)
                    pszMem = (LPWSTR)VirtualAlloc((LPVOID)NULL,
                        (DWORD)(80), MEM_COMMIT,
                        PAGE_READWRITE);
                    GetWindowText(GetForegroundWindow(), pszMem,
                        80);
                    cout << GetForegroundWindow() << ", ";
                    string resstr = CW2A(pszMem);
                    wcout << pszMem << endl;
                    if (resstr.find(string(argv[1])) != string::npos && !(resstr.find(string("waitForFocus.exe")) != string::npos)) {
                        cout << "found!" << endl;//It was already up
                        nfound = false;
                    }
                    else {//it wasn't
                        if (!EnumWindows(FindWindowBySubstr, (LPARAM)argv[1])) {//loop through every single window
                            nfound = false;
                        }
                        else {
                            Sleep(1000);
                        }
                    }
                }
                else {
                    cout << "found absolute result" << endl;
                    SwitchToThisWindow(windowHandle, true);//switch to the located window
                    nfound = false;
                }
            }

        }
    }
    else if (argc == 1) {
        cout << "This program takes one input, part of the window name, and waits for that window to load" << endl;
    }
    else {
        cout << "How did you manage to pass a negative number of flags?" << endl;
    }
    return 0;
}

validateGoogleChrome.exe 源代码(该程序检查 Chrome 是否已配置。它假定计算机用户名与 Google Apps 帐户用户名相同。它还必须在 中运行%LOCALAPPDATA%\Google\Chrome\User Data\Default\Preferences):

// validateGoogleChrome.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <string.h>
#include <sstream>
#include <iostream>
#include <fstream>
#include <windows.h>
#include <lm.h>

using namespace std;

string gUname() {
    char username[UNLEN + 1];
    DWORD size = UNLEN + 1;
    GetUserNameA(username, &size);
    stringstream conv;
    string uname = "";
    conv << username;
    conv >> uname;
    cout << "username:" << uname << endl;
    return uname;
}

int main()
{
    string line;
    string query =  gUname();
    ifstream file("Preferences");
    if (file.is_open()) {
        while (getline(file, line, '\n')) {
            if (line.find(query) != string::npos) {
                cout << "passed" << endl;
                return 0;
            }
            else {
                cout << "fail" << endl;
                return 1;
            }
        }
    }
    else {
        cout << "ERROR: Chrome preferences file not found" << endl;
        return 2;
    }
}

希望这段代码能让您感到恐惧,从而想出更好的解决方案。

相关内容