gpt4 book ai didi

javascript - CefSharp 在任何文档加载/处理之前注入(inject) Javascript

转载 作者:数据小太阳 更新时间:2023-10-29 04:14:29 27 4
gpt4 key购买 nike

对于我正在处理的项目,我需要在任何网页文档处理开始之前注入(inject) javascript。这可以通过 WebBrowser 组件轻松实现,但我在使用 CefSharp 时遇到困难。

这里是问题的一个简化,网页需要一个“InjectedObject”才能发挥作用。调用网页而不注入(inject)发生在文档的最顶部,或者在处理文档之前被评估/执行将导致:

=====失败时的 html 示例输出=====

存在对象吗?

错误

=====

我需要显示网页的地方:

=====成功时的 html 示例输出=====

存在对象吗?

正确

=====

<html>
<head>
<script>
isObjectPresent = typeof InjectedObject == "object";
</script>
</head>
<body>
<p>isObjectPresent?</p>
<div id="result"></div>
<script>
document.getElementById("result").innerHTML = isObjectPresent;
</script>
</body>
</html>

查看所有可用的建议表明我应该使用 LoadingStateChanged() 或 FrameLoadEnd() 来注入(inject)脚本,即:

public void OnFrameLoadEnd(object sender, FrameLoadEndEventArgs args) {
if (args.Frame.IsMain) {
args.Frame.ExecuteJavascriptAsync("window.InjectedObject = {};");
}
}

然而,我尝试过的所有迭代,甚至使用 FrameLoadStart,都导致插入的 javascript 在文档开始处理后发生。是否有任何真正的 javascript 注入(inject)示例确保它在文档处理开始之前发生。 (确保避免竞争条件/时间问题)。

作为我想要模仿的 WebBrowser 组件行为的示例是:

private void uiWebBrowser_Navigated(object sender, System.Windows.Navigation.NavigationEventArgs e) 
{
var browser = (WebBrowser)sender;
var document = browser.Document as HTMLDocument;
var head = document.getElementsByTagName("head").Cast<HTMLHeadElement>().First();
if (head != null)
{
var script = document.createElement("script") as IHTMLScriptElement;
script.text = "window.InjectedObject = {};"
if (head.firstChild != null)
{
head.insertBefore((IHTMLDOMNode)script, head.firstChild);
}
else
{
head.appendChild((IHTMLDOMNode)script;
}
}
}

欢迎任何帮助或建议,理想情况下我想避免通过互联网请求解析和插入下载页面,然后使用 loadhtml,因为我希望我可能必须为影响的所有导航操作执行此操作主框架,这听起来像是一项黑客工作。

根据评论,建议 javascript V8 引擎上下文足以满足上述用例。尝试从 IRenderProcessMessageHandler 接口(interface)实现 OnContextCreated 方法具有相同的结果。

==MainWindow.xaml==

<Window x:Class="ExampleCefSharp001.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:cefSharp="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ExampleCefSharp001"
mc:Ignorable="d"
Title="MainWindow" Height="1000" Width="1100">
<Grid>
<cefSharp:ChromiumWebBrowser x:Name="uiWebView"></cefSharp:ChromiumWebBrowser>
</Grid>
</Window>

==MainWindow.xaml.cs==

public partial class MainWindow : Window
{
JavascriptManager jsmanager;

public MainWindow()
{
InitializeComponent();

jsmanager = new JavascriptManager(uiWebView);
}
}

public class JavascriptManager : ILoadHandler, IRenderProcessMessageHandler
{
string injection = "window.InjectedObject = {};";

public JavascriptManager(ChromiumWebBrowser browser)
{
browser.LoadHandler = this;
browser.RenderProcessMessageHandler = this;

// Lets just pretend this is a real url with the example html above.
browser.Address = "https://www.example.com/timingtest.htm"
}

public void OnContextCreated(IWebBrowser browserControl, IBrowser browser, IFrame frame)
{
frame.ExecuteJavaScriptAsync(injection);
}
}

非常感谢您的意见和建议。如果我遗漏了什么,请告诉我!

最佳答案

终于回到了这里。主要基于以下示例:CefSharp.Example/Filters/FindReplaceResponseFilter.cs

实现 IRequestHandler 和 IResponseFilter 接口(interface):

==MainWindow.xaml==

<Window x:Class="ExampleCefSharp001.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:cefSharp="clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ExampleCefSharp001"
mc:Ignorable="d"
Title="MainWindow" Height="1000" Width="1100">
<Grid>
<cefSharp:ChromiumWebBrowser x:Name="uiWebView"></cefSharp:ChromiumWebBrowser>
</Grid>
</Window>

==MainWindow.xaml.cs==

public partial class MainWindow : Window
{
JavascriptManager jsmanager;

public MainWindow()
{
InitializeComponent();

jsmanager = new JavascriptManager(uiWebView);
}
}

public class JavascriptManager : IRequestHandler
{
string injection = "window.InjectedObject = {};";

public JavascriptManager(ChromiumWebBrowser browser)
{
browser.RequestHandler = this;

// Lets just pretend this is a real url with the example html above.
browser.Address = "https://www.example.com/timingtest.htm"
}

public IResponseFilter GetResourceResponseFilter(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response)
{
if (frame.IsMain && request.ResourceType == ResourceType.MainFrame)
{
return new JavascriptInjectionFilter(injection);
}
return null;
}
}

public class JavascriptInjectionFilter : IResponseFilter
{
/// <summary>
/// Location to insert the javascript
/// </summary>
public enum Locations
{
/// <summary>
/// Insert Javascript at the top of the header element
/// </summary>
head,
/// <summary>
/// Insert Javascript at the top of the body element
/// </summary>
body
}

string injection;
string location;
int offset = 0;
List<byte> overflow = new List<byte>();

/// <summary>
/// Constructor
/// </summary>
/// <param name="injection"></param>
/// <param name="location"></param>
public JavascriptInjectionFilter(string injection, Locations location = Locations.head)
{
this.injection = "<script>" + injection + "</script>";
switch (location)
{
case Locations.head:
this.location = "<head>";
break;

case Locations.body:
this.location = "<body>";
break;

default:
this.location = "<head>";
break;
}
}

/// <summary>
/// Disposal
/// </summary>
public void Dispose()
{
//
}

/// <summary>
/// Filter Processing... handles the injection
/// </summary>
/// <param name="dataIn"></param>
/// <param name="dataInRead"></param>
/// <param name="dataOut"></param>
/// <param name="dataOutWritten"></param>
/// <returns></returns>
public FilterStatus Filter(Stream dataIn, out long dataInRead, Stream dataOut, out long dataOutWritten)
{
dataInRead = dataIn == null ? 0 : dataIn.Length;
dataOutWritten = 0;

if (overflow.Count > 0)
{
var buffersize = Math.Min(overflow.Count, (int)dataOut.Length);
dataOut.Write(overflow.ToArray(), 0, buffersize);
dataOutWritten += buffersize;

if (buffersize < overflow.Count)
{
overflow.RemoveRange(0, buffersize - 1);
}
else
{
overflow.Clear();
}
}


for (var i = 0; i < dataInRead; ++i)
{
var readbyte = (byte)dataIn.ReadByte();
var readchar = Convert.ToChar(readbyte);
var buffersize = dataOut.Length - dataOutWritten;

if (buffersize > 0)
{
dataOut.WriteByte(readbyte);
dataOutWritten++;
}
else
{
overflow.Add(readbyte);
}

if (char.ToLower(readchar) == location[offset])
{
offset++;
if (offset >= location.Length)
{
offset = 0;
buffersize = Math.Min(injection.Length, dataOut.Length - dataOutWritten);

if (buffersize > 0)
{
var data = Encoding.UTF8.GetBytes(injection);
dataOut.Write(data, 0, (int)buffersize);
dataOutWritten += buffersize;
}

if (buffersize < injection.Length)
{
var remaining = injection.Substring((int)buffersize, (int)(injection.Length - buffersize));
overflow.AddRange(Encoding.UTF8.GetBytes(remaining));
}

}
}
else
{
offset = 0;
}

}

if (overflow.Count > 0 || offset > 0)
{
return FilterStatus.NeedMoreData;
}

return FilterStatus.Done;
}

/// <summary>
/// Initialization
/// </summary>
/// <returns></returns>
public bool InitFilter()
{
return true;
}

}

感谢amaitland为我指明了正确的方向,并为上面绝大多数代码所基于的示例程序。最终结果:

<html><head></head><body><script>window.InjectedObject = {}</script>
<script>
isObjectPresent = typeof InjectedObject == "object";
</script>
<p>isObjectPresent?</p>
<div id="result"></div>
<script>
document.getElementById("result").innerHTML = isObjectPresent;
</script>
</body></html>

这满足了我在标题顶部使用一些文本预处理文档的需求,确保在注入(inject)代码之前运行现有代码时不会出现计时问题。

编辑几个小修复。添加控制逻辑以仅在加载大型机时插入。

关于javascript - CefSharp 在任何文档加载/处理之前注入(inject) Javascript,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41745827/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com