JS 打开窗口并更改用户代理 [英] JS Open Window And Alter User Agent

查看:42
本文介绍了JS 打开窗口并更改用户代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个只使用 IE 的供应商的问题.我正在打开一个窗口并传递凭据.如果是 Chrome/FireFox/等,他们会阻止它.我能够成功更改用户代理以模仿 IE,但不能与打开窗口结合使用.

I have a problem with a vendor ONLY working with IE. I am opening a window and passing credentials. If it is Chrome/FireFox/etc they block it. I am able to successfully change the user agent to imitate IE but not in combo with opening a window.

openWindow('https://www.IEOnlyVendor.com?credentials=abc123');
var __originalNavigator = navigator; // alter user agent string to IE 11
navigator = new Object();
navigator.__proto__ = __originalNavigator;
navigator.__defineGetter__('userAgent', function () { return 'Mozilla/5.0 (Windows NT 6.1;     WOW64; Trident/7.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; Media Center PC 6.0; .NET4.0C; .NET4.0E; MDDR; MS-RTC LM 8; rv:11.0) like Gecko'; }); 

有人知道怎么做吗?也许创建一个窗口,更改用户代理,然后打开窗口.

Does anyone know how to do this? Perhaps create a window, change the user agent, then open the window.

推荐答案

可能可以尝试类似于 这个答案 改变了 iframe 的用户代理.

You might be able to try something similar to how this answer changes the user agent of an iframe.

重要部分(我添加了 contentWindow 参数,以便您可以(?)在新窗口上调用):在将内容注入空窗口后调用此函数.

The important part (I added contentWindow param so you could (?) call on new window): calling this function after injecting contents into empty window.

    var setUA = function(contentWindow) {
        if (Object.defineProperty) {
            Object.defineProperty(contentWindow.navigator, 'userAgent', {
                configurable: true,
                get: function () {
                    return 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5';
                }
            });
        } else if (Object.prototype.__defineGetter__) {
            contentWindow.navigator.__defineGetter__('userAgent', function () {
                return 'Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5';
            });
        } else {
            alert('browser not supported');
        }
    };

这篇关于JS 打开窗口并更改用户代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆