京东慧眼
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

157 lines
4.2 KiB

using CefSharp;
using CefSharp.Wpf;
using System;
using System.Collections.Generic;
using System.Security.Policy;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using .cefhelper;
namespace .Windows
{
/// <summary>
/// ItemDetailWindow.xaml 的交互逻辑
/// </summary>
public partial class ItemDetailWindow : Window
{
private ExtChromiumBrowser web;
private ItemDetailWindow(string url)
{
InitializeComponent();
KeyDown += ItemDetailWindow_KeyDown;
web = new ExtChromiumBrowser(url)
{
BrowserSettings =
{
DefaultEncoding = "UTF-8",
Plugins = CefState.Enabled,
//关于跨域限制
//WebSecurity = CefState.Disabled,
ApplicationCache = CefState.Enabled,
LocalStorage = CefState.Enabled
},
//RequestHandler =new MyRequestHandler()
};
grid.Children.Add(web);
}
private void LoadUrl(string url)
{
web.Load(url);
}
/// <summary>
/// 显示
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
public static bool? Show(string url)
{
ItemDetailWindow window = null ;
bool? result = null;
MainWindow.Main.Dispatcher.Invoke(new Action(() => {
window = new ItemDetailWindow(url);
result= window.ShowDialog();
}));
return result;
}
private void ItemDetailWindow_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == System.Windows.Input.Key.F12)
{
web.ShowDevTools();
}
}
bool isWait = false;
private void btn_add_Click(object sender, RoutedEventArgs e)
{
if (isWait)
{
web.LoadUrlAsync("www.baidu.com");
return;
}
isWait = true;
Task<JavascriptResponse> task = null;
Application.Current.Dispatcher.Invoke(() =>
{
task = web.EvaluateScriptAsPromiseAsync("return $(document.body).html();",TimeSpan.FromSeconds(10));
});
var result = task.Result;
//判断是否加载完成
if (!result.Success)
{
WpfNoticeMsg.NoticeMessage.Show("请等待页面加载完成后重试!");
return;
}
string imgUrl = EvalScript("return $('#spec-img').attr('src')").Replace(".avif","");
if (imgUrl.StartsWith("//"))
{
imgUrl = "https:" + imgUrl;
}
string title = EvalScript("return $($(\".sku-name\")[0]).text().trim()");
string price = EvalScript("return $($(\".p-price\")[0]).text().trim()").Replace("¥", "").Trim();
string comment = EvalScript("return $($(\"li[data-anchor=#comment]\")[0]).text().trim()");
var match= Regex.Match(comment, @"商品评价\((.*?)\)");
if (match.Success)
{
comment= match.Groups[1].Value;
}
string url = web.Address;
this.DialogResult = true;
}
private void btn_canel_Click(object sender, RoutedEventArgs e)
{
this.DialogResult = false;
}
string EvalScript(string js)
{
Task<JavascriptResponse> task = null;
Application.Current.Dispatcher.Invoke(() =>
{
task = web.EvaluateScriptAsPromiseAsync(js, TimeSpan.FromSeconds(10));
});
var result = task.Result;
if (result.Success)
{
return result.Result.ToString();
}
return string.Empty;
}
}
}