简体   繁体   English

检测网站是否在 Windows 窗体中使用 webview 运行

[英]Detect if website runs using webview in windows forms

As above.如上。 I don't need to do anything fancy with it.我不需要对它做任何花哨的事情。 I've done a bit of a research around, but I can't seem to find proper solution.我已经做了一些研究,但似乎找不到合适的解决方案。 I am planning on hiding side bar in my React app if user is running website via Webview in Windows forms app.如果用户在 Windows 表单应用程序中通过 Webview 运行网站,我计划在我的 React 应用程序中隐藏侧边栏。 What would be the best way of approaching it?接近它的最佳方法是什么? Checking for version or what browser is running on, is a no go, since Webview is using either default browser or browser that is described in method, so it would just return that.检查版本或正在运行的浏览器是不行的,因为 Webview 使用的是默认浏览器或方法中描述的浏览器,所以它只会返回。 Bear in mind that detection could be done in either Forms app or React app, it doesnt matter which.请记住,检测可以在 Forms 应用程序或 React 应用程序中完成,这并不重要。 Any suggestions would be appreciated.任何建议,将不胜感激。

I also can think of the following solutions:我还可以想到以下解决方案:

  • Handle it by DOM manipulation in the applicatoin: As an option your application can hide the side bar by finding the element in DOM and hide it.通过应用程序中的 DOM 操作来处理它:作为一个选项,您的应用程序可以通过在 DOM 中查找元素并将其隐藏来隐藏侧边栏。

  • Send a new user agent string to Server and render different output by server: As another option, you can think of your application as a new user agent.向服务器发送一个新的用户代理字符串并按服务器呈现不同的输出:作为另一种选择,您可以将您的应用程序视为一个新的用户代理。 So you can send a custom user agent header along with your request.因此,您可以随请求一起发送自定义用户代理标头。 This is the way that I've already used to pretend the request is coming from a mobile phone .这是我已经用来假装请求来自移动电话的方式 You can do something similar for your application.您可以为您的应用程序做类似的事情。

Example例子

[DllImport("urlmon.dll", CharSet = CharSet.Ansi)]
private static extern int UrlMkSetSessionOption(int dwOption, string pBuffer,
    int dwBufferLength, int dwReserved);
const int URLMON_OPTION_USERAGENT = 0x10000001;

string additionalHeaders = "User-Agent:MyApp\r\n";
private void Form1_Load(object sender, EventArgs e)
{
    UrlMkSetSessionOption(URLMON_OPTION_USERAGENT,
        additionalHeaders, additionalHeaders.Length, 0);
    webBrowser1.Navigate("http://google.com");
}

Then your site can decide about rendering base on the user agent.然后您的站点可以根据用户代理决定渲染。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM