1

我得到以下代码

using DotNetBrowser;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Browsium
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private Browser browser;
        private DotNetBrowser.WinForms.WinFormsBrowserView bview;
        private void Form1_Load(object sender, EventArgs e)
        {
            browser = BrowserFactory.Create(BrowserContext.DefaultContext,BrowserType.HEAVYWEIGHT);
            browser.ScriptContextCreated += Browser_ScriptContextCreated;
            bview = new DotNetBrowser.WinForms.WinFormsBrowserView(browser);
            this.Controls.Add(bview);
            browser.LoadURL("https://whoer.net/#extended");
        }

        private void Browser_ScriptContextCreated(object sender, DotNetBrowser.Events.ScriptContextEventArgs e)
        {
            // demo
            browser.ExecuteJavaScriptAndReturnValue(e.Context.FrameId, System.IO.File.ReadAllText("test.js"));
        }
    }
}

test.js 文件包含

window = new function() {
    this.innerWidth = 240,
    this.innerHeight = 182
};

window.screen = new function() {
    this.availHeight = 320,
    this.availLeft = 0,
    this.availTop = 0,
    this.availWidth = 240,
    this.clientHeight = 320,
    this.clientWidth = 240,
    this.colorDepth = 24,
    this.height = 320,
    this.left = 0,
    this.offsetHeight = 320,
    this.offsetWidth = 240,
    this.pixelDepth = 24,
    this.top = 0,
    this.width = 240,
    this.orientation = "landscape-primary",
    this.mozEnabled = true
};

window.navigator = new function() {
    this.userAgent = "Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.70 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.70",
    this.appCodeName = "Mozilla",
    this.appName = "Netscape",
    this.appVersion = "5.0 (Windows NT 10.0) AppleWebKit/537.70 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.70",
    this.language = "en",
    this.languages = "en",
    this.platform = "x86",
    this.oscpu = "x86",
    this.hardwareConcurrency = "8",
    this.vendor = "Test Inc.",
    this.vendorSub = "",
    this.buildID = "",
    this.product = "Chrome",
    this.productSub = "20030107"
};

窗口和 window.screen 正在工作,但我无法覆盖 window.navigator。我在 mozilla dev 上找到了 window.navigator 对象。但由于某些奇怪的原因,我的解决方案不起作用。我尝试做的是修改 window.navigator 属性并添加插件。关于如何做到这一点的任何想法?

4

1 回答 1

2

'window.navigator' 属性是只读的,因此您无法更新其值。但是,如果您需要更改 DotNetBrowser 中的用户代理字符串,您可以使用以下方法执行此操作:

Browser browser = BrowserFactory.Create(); browser.UserAgent = "Modified User Agent String";

您可以在以下文章中找到与用户代理字符串相关的信息:https ://dotnetbrowser.support.teamdev.com/support/solutions/articles/9000110164-user-agent

于 2017-11-15T11:28:00.210 回答