更新时间:2023-11-26 21:31:40
我已经在< 使用不同的用户代理加载iframe内容>
I already answer the same question at <Load iframe content with different user agent>
为方便起见,我复制并粘贴了答案:
For your convenient, I copied and paste the answer here:
首先,您必须创建一个函数来更改用户代理string:
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
});
}
}
}
然后你需要定位iframe元素:
Then you need to target the iframe element:
setUserAgent(document.querySelector('iframe').contentWindow, 'MANnDAaR Fake Agent');
您还可以为iframe设置ID并定位ID而不是页面上的所有iframe元素。
You may also set an ID to the iframe and target the ID instead of all iframe elements on the page.