Ace*_*t10 5 javascript c# webview xamarin
我试图禁用任何双击功能,在这种情况下,要禁用双击instagram图片以使其喜欢的功能。我已经测试了一些JavaScript作为chrome扩展,并为我提供了所需的结果,但是当我将其带入Xamarin WebView时,它不起作用。奇怪的是,注入的JavaScript正在运行,因为如果其中有警报,则会显示该警报。它仍然喜欢Instagram上的图像。
这是Chrome扩展程序的JavaScript
document.addEventListener("dblclick",handler,true);function handler(e) {
e.stopPropagation();
e.preventDefault(); }
Run Code Online (Sandbox Code Playgroud)
这是Xamarin的代码
string function = "document.addEventListener(\"dblclick\",handler,true);function handler(e){e.stopPropagation();e.preventDefault();}";
Xamarin.Forms.Device.BeginInvokeOnMainThread(() => { webBrowser.Eval(function); });
Run Code Online (Sandbox Code Playgroud)
更新:经过一些测试,我只能在运行网站的移动版本时确认这是一个问题。我不确定这是否与事件的触发方式或与此相关的事情有关,但这仅与移动设备有关。
我创建了一个仅包含网络视图的示例页面来测试您的案例:
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="TestApp.Page1" Appearing="Page1_OnAppearing">
<WebView x:Name="TestWebView" />
</ContentPage>
Run Code Online (Sandbox Code Playgroud)
在后面的代码中,我设置了带有按钮的静态 html,该按钮在双击后会显示警报:
public Page1()
{
InitializeComponent();
// Set static html
TestWebView.Source = new HtmlWebViewSource
{
Html = @"<html>
<body>
<button id='testbutton' style='font-size: 30px;'>Click me</button>
<script type='text/javascript'>
document.getElementById('testbutton').addEventListener('dblclick', function (e) {
alert('double clicked');
});
</script>
</body>
</html>"
};
}
Run Code Online (Sandbox Code Playgroud)
在页面的 OnAppearing 事件中,我执行 javascript 以防止双击事件处理程序:
private void Page1_OnAppearing(object sender, EventArgs e)
{
var script = "document.addEventListener('dblclick',handler,true);function handler(e) { e.stopPropagation(); e.preventDefault(); }";
TestWebView.Eval(script);
}
Run Code Online (Sandbox Code Playgroud)
运行此代码后,双击我的测试按钮后仍然出现双击警报。这是因为 javascript-eval 运行得太早。或者换句话说:在执行 javascript 之前,您必须给 webview 多一点时间来加载内容:
private async void Page1_OnAppearing(object sender, EventArgs e)
{
// Just wait a little bit before calling eval
await Task.Delay(1000);
var script = "document.addEventListener('dblclick',handler,true);function handler(e) { e.stopPropagation(); e.preventDefault(); }";
TestWebView.Eval(script);
}
Run Code Online (Sandbox Code Playgroud)
现在,警报不再出现。
让我知道这是否适用于您的情况。
编辑:另一种方法 也许问题是由 Instagram 页面的实现引起的。禁用双击的另一种方法是禁用 instagram 帖子上的整个事件。
您可以设置以下css-style来禁用此功能:
style="pointer-events:none;"
Run Code Online (Sandbox Code Playgroud)
因此,您必须确定 div 元素的 xpath 并使用 javascript 设置样式,如下所示:
htmlElement.setAttribute("style", "pointer-events:none;");
Run Code Online (Sandbox Code Playgroud)
我在我的浏览器(chrome 调试器)上测试了它并且它有效。但我不能最终确定这是否适用于您通过应用程序的情况。
归档时间: |
|
查看次数: |
110 次 |
最近记录: |