javascript iframe 的假用户代理
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12057890/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fake User Agent for iframe
提问by MANnDAaR
I'm new to Javascript. I have found this code to change user agent using Javascript.
我是 Javascript 的新手。我发现此代码可以使用 Javascript 更改用户代理。
var __originalNavigator = navigator;
navigator = new Object();
navigator.__defineGetter__('userAgent', function () {
return 'Custom';
});
var iframe='<iframe id="frame" name="widget" src ="http://www.useragentstring.com/" width="100%" height="400" marginheight="0" marginwidth="0" frameborder="no" scrolling="no"></iframe>';
document.write("User-agent header sent: " + navigator.userAgent + iframe);
This code works & returns fake user agent, Though how will I set same fake user agent for iframe ?
此代码有效并返回假用户代理,但我将如何为 iframe 设置相同的假用户代理?
Here is fiddle of what I'm up to : http://jsfiddle.net/ufKBE/1/
这是我在做什么的小提琴:http: //jsfiddle.net/ufKBE/1/
回答by Aero Wang
I already answer the same question at <Load iframe content with different user agent>
我已经在 <使用不同的用户代理加载 iframe 内容>回答了同样的问题
For your convenient, I copied and paste the answer here:
为方便起见,我将答案复制并粘贴到此处:
First of all, you must create a function to change the user agent string:
首先,您必须创建一个函数来更改用户代理字符串:
function setUserAgent(window, userAgent) {
if (window.navigator.userAgent != userAgent) {
var userAgentProp = { get: function () { return userAgent; } };
try {
Object.defineProperty(window.navigator, 'userAgent', userAgentProp);
} catch (e) {
window.navigator = Object.create(navigator, {
userAgent: userAgentProp
});
}
}
}
Then you need to target the iframe element:
然后你需要定位 iframe 元素:
setUserAgent(document.querySelector('iframe').contentWindow, 'MANnDAaR Fake Agent');
You may also set an ID to the iframe and target the ID instead of all iframe elements on the page.
您还可以为 iframe 设置一个 ID 并定位该 ID,而不是页面上的所有 iframe 元素。
回答by jerinho.com
That is not the right way to switch your user agent to the faked one. window.navigator = {userAgent:Custom_User_Agent}
is just a javascript execution. It will simply be ignored as you refresh the page, either it is on window or within the iframe, and then the default user agent which will be sent to the server instead. If you really want to switch your user agent, it has to be the browser setting you deal with. Some browsers allow this on their settings, and some others include user agent switcher or support some kind of plugin that do this
这不是将您的用户代理切换到伪造的用户代理的正确方法。window.navigator = {userAgent:Custom_User_Agent}
只是一个 javascript 执行。当您刷新页面时,它会被简单地忽略,无论是在窗口上还是在 iframe 内,然后是默认的用户代理,它将被发送到服务器。如果你真的想切换你的用户代理,它必须是你处理的浏览器设置。一些浏览器在其设置中允许这样做,而其他一些浏览器包括用户代理切换器或支持某种插件来执行此操作
The alternatives are, you can also try to access the website from the server or build your own web accessing application. These ways, you can freely alter your header or use your own customized user agent
替代方案是,您也可以尝试从服务器访问网站或构建自己的 Web 访问应用程序。这些方式,您可以自由更改您的标题或使用您自己的自定义用户代理
Another way is by using AJAX. but of course it is limited by cross-origin-policy
另一种方法是使用 AJAX。但当然它受到跨域策略的限制