【问题标题】:CefShap - How can I inject a double click into the webpage via C#?CefSharp - 如何通过 C# 将双击注入网页?
【发布时间】:2020-08-19 17:47:34
【问题描述】:

我有一个基于 CefSharp v83.4.20 的应用程序,并尝试从 C# 代码中注入双击。 Winform 和 WPF 都会发生这种情况。

这是用于测试的Html:

<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta charset="utf-8">
    <title>test</title>
  </head>
  <body>    
    <div style="position: absolute; width: 100px; height: 100px; z-index: 100; background: rgb(0, 0, 0); left: 100px; top: 100px;"></div>
  </body>
</html>

<script>
  var elem = document.getElementsByTagName('div')[0];
  elem.addEventListener('mousedown', function () {
    console.log('mousedown');
  });

  elem.addEventListener('mouseup', function () {
    console.log('mouseup');
  });

  elem.addEventListener('click', function () {
    console.log('click');
  });

  elem.addEventListener('dblclick', function () {
    // this never gets called when injecting two clicks, but works when manually double clicking
    console.log('doubleclicked');
   });
</script>

我可以像这样从 C# 代码轻松地向 Html 注入点击:

int x = 150;
int y = 150;
var host = chromeBrowserInstance.GetBrowser().GetHost();

host.SendMouseClickEvent(x, y, MouseButtonType.Left, false, 0, CefEventFlags.LeftMouseButton);
System.Threading.Thread.Sleep(50);
host.SendMouseClickEvent(x, y, MouseButtonType.Left, true, 0, CefEventFlags.LeftMouseButton);

但是我怎样才能注入双击呢? 如上图简单发送两次点击失败:

int x = 150;
int y = 150;
var host = chromeBrowserInstance.GetBrowser().GetHost();

host.SendMouseClickEvent(x, y, MouseButtonType.Left, false, 0, CefEventFlags.LeftMouseButton);
System.Threading.Thread.Sleep(50);
host.SendMouseClickEvent(x, y, MouseButtonType.Left, true, 0, CefEventFlags.LeftMouseButton);

System.Threading.Thread.Sleep(100);

host.SendMouseClickEvent(x, y, MouseButtonType.Left, false, 0, CefEventFlags.LeftMouseButton);
System.Threading.Thread.Sleep(50);
host.SendMouseClickEvent(x, y, MouseButtonType.Left, true, 0, CefEventFlags.LeftMouseButton);

这会给出以下输出(缺少预期的“双击”):

mousedown
mouseup
click
mousedown
mouseup
click

手动双击时会出现预期的“双击”:

mousedown
mouseup
click
mousedown
mouseup
click
doubleclicked

我试图调整时间,但没有任何帮助。任何想法如何注入有效的双击?


编辑:
感谢您的输入,我试过了:

MouseEvent me = new MouseEvent(150, 150, CefEventFlags.LeftMouseButton);
bool mouseup = true;
int clickCount = 2;

host.SendMouseClickEvent(
    me,
    MouseButtonType.Left,
    mouseup,
    clickCount
);

但是无论我为clickCount(甚至是0)设置什么,它只会产生一个mouseup / 鼠标按下事件。

在我发现的正文中添加随机文本后,使用上述方法 clickCount 是 2 或 3 和 mouseup = true 正文中的文本得到 就像手动双击/三次单击时一样突出显示。
所以它以某种方式工作,但不会产生预期的事件。

【问题讨论】:

  • 尝试使用cefsharp.github.io/api/83.4.x/html/… 并指定点击次数
  • github.com/cefsharp/CefSharp.MinimalExample 中包含的 WPF 示例是否按预期工作?它在内部使用鼠标功能。
  • 对不起,我忘了说我使用 Winforms 进行测试。但是使用 MinimalExample WPF 版本测试相同的代码会产生完全相同的结果。
  • 您是否在 WPF 示例中加载了 html 并使用了鼠标? WPF 版本在内部使用这些方法,如果它在 WPF 示例中有效,您可以添加一些日志记录来锻炼您需要的调用。

标签: javascript c# html cefsharp chromium-embedded


【解决方案1】:

这会双击并触发dblclick 事件:

public void double_click(int x, int y) {
    var host = browser.GetHost();

    // first click            
    host.SendMouseClickEvent(
        x, y, MouseButtonType.Left, false, 1, CefEventFlags.LeftMouseButton
    );

    host.SendMouseClickEvent(
        x, y, MouseButtonType.Left, true, 1, CefEventFlags.None
    );

    // second click
    host.SendMouseClickEvent( 
        x, y, MouseButtonType.Left, false, 2, CefEventFlags.LeftMouseButton
    );

    host.SendMouseClickEvent(
        x, y, MouseButtonType.Left, true, 1, CefEventFlags.None
    );            
}

在按照 amaitland 的建议登录 CefSharp.WPF.ChromiumWebBrowser.cs 之后,我发现了上述失败的原因: 我通过认为误解了count 参数,它将被执行的点击次数。 但实际上是一系列事件中当前事件的索引。
比如第三个SendMouseClickEventcount参数是2,这样cef就知道第二个和第三个事件是一起的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-28
    • 1970-01-01
    • 2018-11-21
    • 2011-05-29
    • 1970-01-01
    • 1970-01-01
    • 2013-11-28
    • 1970-01-01
    相关资源
    最近更新 更多