是否可以检测 WP7 WebBrowser 控件中点击的 DOM 元素?

发布于 2024-12-18 10:42:08 字数 71 浏览 1 评论 0原文

是否可以使用 WP7 WebBrowser 控件检测用户在网站上点击的位置下的 DOM 元素?我想让用户识别渲染页面的某些部分。

Is it possible to detect the DOM elements under a spot the user taps on a website using the WP7 WebBrowser control? I would like to let the user identify certain sections of the rendered page.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

虫児飞 2024-12-25 10:42:08

是的。这段代码对我来说效果很好:

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        // first define a new function which serves as click handler
        webBrowser1.InvokeScript("eval", "this.newfunc_eventHandler = function(e) { window.external.notify(e.srcElement.tagName); }");
        // attach function to body
        webBrowser1.InvokeScript("eval", "document.body.addEventListener('click', newfunc_eventHandler, false);");
    }

    private void webBrowser1_ScriptNotify(object sender, NotifyEventArgs e)
    {
        // this should be called every time you tap an element; the value should be its tag name
        Debug.WriteLine(e.Value);
    }

它符合贾斯汀的答案(他在编码时打败了我)。它首先定义一个单击处理程序,然后将其附加到页面的主体(全部通过 InvokeScript)。每次点击某个元素时 ScriptNotify 应该在 WebBrowser 上调用,报告点击元素的标签名称。

确保您已将浏览器控件的 IsScriptEnabled 设置为 true。

Yes. This code works pretty well for me:

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        // first define a new function which serves as click handler
        webBrowser1.InvokeScript("eval", "this.newfunc_eventHandler = function(e) { window.external.notify(e.srcElement.tagName); }");
        // attach function to body
        webBrowser1.InvokeScript("eval", "document.body.addEventListener('click', newfunc_eventHandler, false);");
    }

    private void webBrowser1_ScriptNotify(object sender, NotifyEventArgs e)
    {
        // this should be called every time you tap an element; the value should be its tag name
        Debug.WriteLine(e.Value);
    }

It goes along Justin's answer (who beat me while coding). It first defines a click handler which then is attached to the page's body (all via InvokeScript). Every time you tap an element ScriptNotify should be called on the WebBrowser reporting the tapped element's tag name.

Make sure you have set IsScriptEnabled true for your browser control.

萌面超妹 2024-12-25 10:42:08

我猜你必须构建一些东西 ScriptNotify 事件

此时,您将照常注册一个 JavaScript 事件处理程序,但该事件处理程序将依次调用 window.external.notify("someMessageToHandle");。然后,您必须适当地路由呼叫。

如果您无法直接在页面上添加 JavaScript 事件处理程序,则可以使用 WebBrowserControl.InvokeScript

I'm guessing you'd have to build something off of the ScriptNotify Event.

At that point, you would register a JavaScript Event handler as usual but that event handler would, in turn, call window.external.notify("someMessageToHandle");. You would then have to route the calls appropriately.

If you don't have the ability to add the JavaScript event handlers directly on the page, you could instead add them using WebBrowserControl.InvokeScript.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文