Control User Agent to call iFrame

1

Good morning !! I was doing some things here and decided to put an iFrame on the page, but it was loaded with the User Agent from some other browser. Just to understand the behavior of the site.

Is this possible with HTML5 and Javascript?

Otherwise, can you do this with cURL in PHP? And how do I do it?

I was able to do this with cURL, but I wanted to do it with iFrame. The solution in PHP was:

<?php
$url = "http://example.com";
$ch = curl_init();
$curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Linux; U; Android 4.0.3; de-ch; HTC Sensation Build/IML74K) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30");
curl_exec($ch);    

Playing all this in a DIV, I get something, but I want to do with iFrame, because then I have problem with images, which I solved with a base tag, but I prefer iFrame.

    
asked by anonymous 27.07.2015 / 13:06

1 answer

2

To change the user agent of a iframe , or a page is possible with javascript as follows:

Set the function to set user agent :

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 select the object window from where you want to change the user agent :

var mWindowFrame = document.querySelector('iframe').contentWindow;

Call the function by passing the object window and the new user agent value to be set:

setUserAgent(mWindowFrame, 'Meu User Agent falso!');

Ready from that point on the user agent value will be what you set!

Complete jsFiddle sample

    
27.07.2015 / 13:57