首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DotNetBrowser覆盖window.navigator

DotNetBrowser覆盖window.navigator
EN

Stack Overflow用户
提问于 2017-11-15 02:07:59
回答 1查看 509关注 0票数 1

我得到了以下代码

代码语言:javascript
复制
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文件包含

代码语言:javascript
复制
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属性并添加插件。你有什么建议吗?

EN

回答 1

Stack Overflow用户

发布于 2017-11-15 19:28:00

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

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

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47292552

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档