- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在构建一个自定义浏览器作为远程网站界面的一部分。他们的 GUI 很糟糕,所以我做了一些 JavaScript 修改以使其看起来更好。
目前,为了修改其 UI,我使用以下 GreaseMonkey 脚本(在 Firefox 上):
// ==UserScript==
// @name winman-load
// @namespace winman
// @description stuff to do when winman.js loads
// @include https://addons.mozilla.org/en-US/firefox/addon/greasemonkey/
// @version 1
// @grant none
// @run-at document-start
// ==/UserScript==
document.addEventListener("beforescriptexecute", function(e) {
src = e.target.src;
content = e.target.text;
//console.log("src: " + src);
if (src.search("winman.js") > -1) {
console.info("============ new winman ===========\n" + src);
var newContent = "";
$.ajax({
async: false,
type: 'GET',
url: '/script/winman.js',
success: function(data) {
newContent = data.replace('pos += currentPos;', 'pos += currentPos + 100;');
newContent = newContent.replace('var enable = false;', 'var enable = true;');
newContent = newContent.replace('var available = true;', 'var available = false;');
}
});
// Stop original script
e.preventDefault();
e.stopPropagation();
unsafeWindow.jQuery(e.target).remove();
var script = document.createElement('script');
script.textContent = newContent;
(document.head || document.documentElement).appendChild(script);
script.onload = function() {
this.parentNode.removeChild(this);
}
}
});
我希望能够使用 CefSharp 做一些事情,我可以在浏览器加载页面时动态修改脚本。
最佳答案
好吧,我明白了。您可以使用如下方法创建一个 RequestHandler:
IResponseFilter IRequestHandler.GetResourceResponseFilter(IWebBrowser browserControl, IBrowser browser, IFrame frame, IRequest request, IResponse response) {
var url = new Uri(request.Url);
if (request.Url.Equals(scriptToUpdate, StringComparison.OrdinalIgnoreCase)) {
Dictionary<string, string> dictionary = new Dictionary<string, string>();
dictionary.Add(search1, replace1);
dictionary.Add(search2, replace2);
return new FindReplaceResponseFilter(dictionary);
}
return null;
}
然后对于多重搜索/替换,您创建一个 FindReplaceResponseFilter:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using CefSharp;
namespace CefFilters {
public class FindReplaceResponseFilter : IResponseFilter {
private static readonly Encoding encoding = Encoding.UTF8;
/// <summary>
/// The portion of the find string that is currently matching.
/// </summary>
private int findMatchOffset;
/// <summary>
/// Overflow from the output buffer.
/// </summary>
private readonly List<byte> overflow = new List<byte>();
/// <summary>
/// Number of times the the string was found/replaced.
/// </summary>
private int replaceCount;
private Dictionary<string, string> dictionary;
public FindReplaceResponseFilter(Dictionary<string, string> dictionary) {
this.dictionary = dictionary;
}
bool IResponseFilter.InitFilter() {
return true;
}
FilterStatus IResponseFilter.Filter(Stream dataIn, out long dataInRead, Stream dataOut, out long dataOutWritten) {
// All data will be read.
dataInRead = dataIn == null ? 0 : dataIn.Length;
dataOutWritten = 0;
// Write overflow then reset
if (overflow.Count > 0) {
// Write the overflow from last time.
WriteOverflow(dataOut, ref dataOutWritten);
}
// Evaluate each character in the input buffer. Track how many characters in
// a row match findString. If findString is completely matched then write
// replacement. Otherwise, write the input characters as-is.
for (var i = 0; i < dataInRead; ++i) {
var readByte = (byte) dataIn.ReadByte();
var charForComparison = Convert.ToChar(readByte);
if (replaceCount < dictionary.Count) {
var replace = dictionary.ElementAt(replaceCount);
if (charForComparison == replace.Key[findMatchOffset]) {
//We have a match, increment the counter
findMatchOffset++;
// If all characters match the string specified
if (findMatchOffset == replace.Key.Length) {
// Complete match of the find string. Write the replace string.
WriteString(replace.Value, replace.Value.Length, dataOut, ref dataOutWritten);
// Start over looking for a match.
findMatchOffset = 0;
replaceCount++;
}
continue;
}
// Character did not match the find string.
if (findMatchOffset > 0) {
// Write the portion of the find string that has matched so far.
WriteString(replace.Key, findMatchOffset, dataOut, ref dataOutWritten);
// Start over looking for a match.
findMatchOffset = 0;
}
}
// Write the current character.
WriteSingleByte(readByte, dataOut, ref dataOutWritten);
}
if (overflow.Count > 0) {
//If we end up with overflow data then we'll need to return NeedMoreData
// On the next pass the data will be written, then the next batch will be processed.
return FilterStatus.NeedMoreData;
}
// If a match is currently in-progress we need more data. Otherwise, we're
// done.
return findMatchOffset > 0 ? FilterStatus.NeedMoreData : FilterStatus.Done;
}
private void WriteOverflow(Stream dataOut, ref long dataOutWritten) {
// Number of bytes remaining in the output buffer.
var remainingSpace = dataOut.Length - dataOutWritten;
// Maximum number of bytes we can write into the output buffer.
var maxWrite = Math.Min(overflow.Count, remainingSpace);
// Write the maximum portion that fits in the output buffer.
if (maxWrite > 0) {
dataOut.Write(overflow.ToArray(), 0, (int) maxWrite);
dataOutWritten += maxWrite;
}
if (maxWrite < overflow.Count) {
// Need to write more bytes than will fit in the output buffer.
// Remove the bytes that were written already
overflow.RemoveRange(0, (int) (maxWrite - 1));
}
else {
overflow.Clear();
}
}
private void WriteString(string str, int stringSize, Stream dataOut, ref long dataOutWritten) {
// Number of bytes remaining in the output buffer.
var remainingSpace = dataOut.Length - dataOutWritten;
// Maximum number of bytes we can write into the output buffer.
var maxWrite = Math.Min(stringSize, remainingSpace);
// Write the maximum portion that fits in the output buffer.
if (maxWrite > 0) {
var bytes = encoding.GetBytes(str);
dataOut.Write(bytes, 0, (int) maxWrite);
dataOutWritten += maxWrite;
}
if (maxWrite < stringSize) {
// Need to write more bytes than will fit in the output buffer. Store the
// remainder in the overflow buffer.
overflow.AddRange(encoding.GetBytes(str.Substring((int) maxWrite, (int) (stringSize - maxWrite))));
}
}
private void WriteSingleByte(byte data, Stream dataOut, ref long dataOutWritten) {
// Number of bytes remaining in the output buffer.
var remainingSpace = dataOut.Length - dataOutWritten;
// Write the byte to the buffer or add it to the overflow
if (remainingSpace > 0) {
dataOut.WriteByte(data);
dataOutWritten += 1;
}
else {
// Need to write more bytes than will fit in the output buffer. Store the
// remainder in the overflow buffer.
overflow.Add(data);
}
}
public void Dispose() {}
}
}
请记住,这会从左到右、从上到下迭代,因此请确保您的搜索和替换顺序正确。
关于javascript - 在使用 CefSharp 加载远程 JavaScript 时修改它们?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38096349/
CefSharp 有许多运行所需的依赖项和库。构建文件夹很困惑。如何将所需的 .dll 和 .pak 依赖项移至子文件夹? 最佳答案 首先,为了让一切变得更容易,我建议向 Visual Studios
我正在考虑将项目移动到 WinForm 应用程序中的嵌入式 WebView 类型体系结构中,并且正在考虑 DotNetBrowser 和 CefSharp。 经过多次搜索,我似乎找不到免费 CefSh
我正在尝试使用 CefSharp 访问 URL 并捕获在加载给定页面期间检索到的特定资源。大概作为每个资源的流或字节数组。 CefSharp 提供接口(interface) IRequestHandl
为了使用 WebClient 而不是 Chrome 进行下载,我截获了一个下载 URL。然后我在使用 获取 cookie 时遇到了问题。访客 .我陷入的陷阱是我不明白访问者是异步运行的,因此只有 30
使用 CEFSharp 浏览器构建应用程序,在我的机器上运行良好,但在服务器上崩溃并出现以下错误: System.IO.FileLoadException:无法加载由“CefSharp.Core.dl
我已经在 CefSharp WinForms 应用程序上工作了几个星期,并且没有遇到任何问题。今天早上,在向应用程序添加一些东西时,我试图运行它来测试一些东西并得到以下错误: System.IO.Fi
使用 XMLHttpRequest which is the subject of my previous question使用自定义方案我正在加载本地文件。为了测试,我使用了 disable-web
我关注了这个link实现 CefSharp 应用程序。 但是我在编写 MainWindow.xaml 时卡住了。 Blend for VS 2015 说, The name ChromiumWebBr
我收到消息命名空间“clr-namespace:CefSharp.Wpf;assembly=CefSharp.Wpf 中不存在名称 ChromiumWebBrowser 当我运行这个
是否可以使用 CefSharp 获取网页的屏幕截图?我找到了关于GetImage()的信息方法,但它似乎不再受支持。有没有其他办法?我需要从屏幕外浏览器获取屏幕截图,因此使用显示浏览器的屏幕截图不是解
我正在使用 CEFsharp 浏览器并通过 LoadingStateChanged 事件确定页面已完成加载,但它会触发多次。 我需要它只在页面完全加载后触发,这怎么办? private async v
我需要在 Wpf 应用程序中使用 Web 浏览器,我尝试使用工具箱中的浏览器,但遇到一些问题并转到 CefSharp。 public MainWindow() { InitializeCompone
如何使用CefSharp获取HTML元素的值? 我知道如何使用此默认WebBrowser控件: Dim Elem As HtmlElement = WebBrowser1.Document.GetEl
我们希望在 wpf 应用程序中的基于 chromium 的浏览器中显示网页。在浏览器中显示的网站也应该显示在另一个屏幕上,但没有交互。我想将cefsharp wpf浏览器控件和cefsharp离屏渲染
有人可以帮助我了解如何在使用 CefSharp winforms 控件时捕获鼠标事件吗? 最佳答案 我没有详细检查以下示例链接,但搜索“global mouse hook”应该会带您到正确的方向。 示
我使用 64 位版本的 CefSharp 3 创建了一个示例应用程序。当所有必需的库与主应用程序可执行文件位于同一文件夹时,它工作正常。我想知道是否有办法从另一个路径读取 CefSharp 所需的 d
我正在开发一个项目,我希望使用 Chromium Web 浏览器并能够读取通常通过 DevTools“网络”选项卡获取的数据。所以基本上我真正需要的是 URL 和状态代码(404、200、50x 等)
在我的项目中加载 CefSharp 程序集时出现错误。 CefSharp 和 CefSharp.WinForms 都没有强命名。有什么办法解决这个问题吗? 错误: Referenced assembl
我已经成功地将 WebView 添加到 WPF 中的窗口。如何向浏览器添加选项卡?我用谷歌搜索但没有找到任何关于在 CefSharp 中制作标签式浏览器的好文档。 var settings = new
我正在使用 CefSharp Winforms x86 构建。我想播放此链接中的视频: http://developers.canal-plus.com/rx-player/ 在 google chr
我是一名优秀的程序员,十分优秀!