Html 使用不同的用户代理加载 iframe 内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12845445/
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
Load iframe content with different user agent
提问by Aakash Chakravarthy
Is it possible to load an iframe with different user agent ?? Using a mobile user agent for an iframe will help my app to show mobile sites as hover popup.
是否可以使用不同的用户代理加载 iframe?为 iframe 使用移动用户代理将帮助我的应用程序将移动网站显示为悬停弹出窗口。
For example, Google shows the mobile search results page only if the user agent is from a mobile one.
例如,只有当用户代理来自移动端时,Google 才会显示移动端搜索结果页面。
Is there any alternate solutions or is there any security risks involved in this idea ??
这个想法是否有任何替代解决方案或是否存在任何安全风险?
回答by Aero Wang
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, 'Aakash Chakravarthy Mobile 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 Fluidbyte
You can do it with JavaScript:
你可以用 JavaScript 做到:
navigator.__defineGetter__('userAgent', function(){
return 'foo' // customized user agent
});
navigator.userAgent; // 'foo'
However, this will only work with a select number of browsers.
但是,这仅适用于选定数量的浏览器。
See this question: Mocking a useragent in javascript?
回答by Billy Moon
You could create a proxy that makes requests with a different user agent, and then load the iframe content through the proxy.
您可以创建一个使用不同用户代理发出请求的代理,然后通过代理加载 iframe 内容。
回答by ChuckE
From what I see, you have two solutions:
据我所知,您有两种解决方案:
Most of the websites don't render per user agent, they just recognize the user agent and redirect the user to the mobile view, reachable under a different domain (http://m.youtube.com/, for instance) or some other URL segment. You just have to check their documentation, get their mobile URL and load it in the iframe.
The other solution is point the Iframe to your application, and then fetch the document from your backend. Then you can change the request user agent.
大多数网站不呈现每个用户代理,它们只是识别用户代理并将用户重定向到移动视图,可在不同域(例如 http://m.youtube.com/)或其他域下访问网址段。你只需要检查他们的文档,获取他们的移动 URL 并将其加载到 iframe 中。
另一种解决方案是将 Iframe 指向您的应用程序,然后从后端获取文档。然后您可以更改请求用户代理。
From HTML is impossible to influence the request headers. But you can do it with javascript.
从 HTML 不可能影响请求标头。但是你可以用javascript来做。
回答by MSZ
I'm guessing one may only change the user agent information for the entire browser window or tab. By loading the iframes at different times, one could change the user agent between loads to get two different iframes on one page with different user agent related content. I tested this with a trivial example page:
我猜可能只会更改整个浏览器窗口或选项卡的用户代理信息。通过在不同时间加载 iframe,可以在加载之间更改用户代理,以便在一个页面上获得两个不同的 iframe,其中包含不同的用户代理相关内容。我用一个简单的示例页面对此进行了测试:
<!DOCTYPE html> <html> <body>
<iframe src="http://whatsmyuseragent.com/" name="mobile" width="60%" height="400px"></iframe>
<a href="http://whatsmyuseragent.com/" target="mobile">Mobile Spoof</a>
<iframe src="http://whatsmyuseragent.com/" name="pc" width="60%" height="400px" ></iframe>
</body> </html>
After loading this page in Firefox 36.0.1, I used the User Agent Switcher extension https://addons.mozilla.org/en-US/firefox/addon/user-agent-switcher/to select iPhone 3.0, and then clicked the "Mobile Spoof" link to reload that frame. At that point I saw the same page showing a different user agent in each iframe. I'm sure the page load or reload timing could be automated with the setTimeout() method, or perhaps via event handlers. I'm not sure how to automate the user agent switch. I tried the setUserAgent function suggested by Aero Windwalker, but could not get it to work in Firefox 36 or Chrome 41. I would also like to create a page that does this well.
在 Firefox 36.0.1 中加载此页面后,我使用 User Agent Switcher 扩展https://addons.mozilla.org/en-US/firefox/addon/user-agent-switcher/选择 iPhone 3.0,然后单击“移动欺骗”链接以重新加载该框架。那时我看到同一个页面在每个 iframe 中显示了不同的用户代理。我确信页面加载或重新加载时间可以通过 setTimeout() 方法或通过事件处理程序自动进行。我不确定如何自动执行用户代理切换。我尝试了 Aero Windwalker 建议的 setUserAgent 函数,但无法在 Firefox 36 或 Chrome 41 中使用它。我还想创建一个可以很好地执行此操作的页面。