DotNetBrowser覆盖window.navigator

时间:2017-11-14 18:07:59

标签: c# dotnetbrowser

我收到了以下代码

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属性并添加插件。 关于如何做到的任何想法?

1 个答案:

答案 0 :(得分: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