如何使用c#将脚本发送到chrome?

时间:2017-10-14 14:29:20

标签: c#

我想发送并执行hello脚本到当前标签激活,我该怎么做?这里我有代码检查当前标签活动铬

private Dictionary<IntPtr, string> chromeWindows = new Dictionary<IntPtr, string>();
public Form1()
{
    InitializeComponent();
}

private string ClassName(IntPtr hWnd)
{
    StringBuilder sbClassName = new StringBuilder(256);
    GetClassName(hWnd, sbClassName, 256);
    return sbClassName.ToString();
}

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern int GetWindowText(IntPtr hwnd, StringBuilder lpString, int cch);

[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
private static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
[DllImport("user32", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]

private static extern bool EnumWindows(EnumWindowsProcDelegate lpEnumFunc, IntPtr lParam);
private delegate bool EnumWindowsProcDelegate(IntPtr hWnd, IntPtr lParam);
private bool ClassesByName(IntPtr hWnd, IntPtr lParam)
{
    //Google Chrome main window class name.
    if (ClassName(hWnd) == "Chrome_WidgetWin_1")
    {
        StringBuilder capText = new StringBuilder(255);
        if (GetWindowText(hWnd, capText, capText.Capacity) > 0)
        {
            chromeWindows.Add(hWnd, capText.ToString());
        }
    }
    return true;
}
private void button1_Click(object sender, EventArgs e)
{
    chromeWindows.Clear();
    // clear dic.
    EnumWindows(ClassesByName, IntPtr.Zero);
    if (chromeWindows.Count == 0)
    {
        MessageBox.Show("None found, list is empty!");
    }
    else
    {
        foreach (var chrome_loopVariable in chromeWindows)
        {
            var chrome = chrome_loopVariable;
            Debug.WriteLine("hWnd={0}, Title={1}", chrome.Key, chrome.Value);
        }
    }

//how to send and run hello scrip?
}

当用户更改标签时如何重新发送hello脚本?

0 个答案:

没有答案