0

I currently do window.open like so:

window.open(this.fetchClientUrl(), this.clientWindowName, utill.setWindowOptions())

Basically the this.fetchClientUrl() is https://www.facebook.com/ads/dia?origin=http%3A%2F%2Fmysite.test , the problem is in mobile the url changes to m.facebook.com

How do I avoid this by changing the user agent in the new opened window ? I.E. How do I do window.open() and then set the user agent ?

I have tried the below line but it does't seem to work:

this.clientWindow =
      window.open(this.fetchClientUrl(), this.clientWindowName, utill.setWindowOptions())

UserAgent(window , "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1");

UserAgent function below:

window.UserAgent = function(window, userAgent) {
        // Works on Firefox, Chrome, Opera and IE9+
        if (navigator.__defineGetter__) {
            navigator.__defineGetter__('userAgent', function () {
                return userAgent;
            });
        } else if (Object.defineProperty) {
            Object.defineProperty(navigator, 'userAgent', {
                get: function () {
                    return userAgent;
                }
            });
        }
        // Works on Safari
        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
                });
            }
        }
    }

I've looked around StackOverflow and the solution provided here, does't work.

Shiny
  • 4,945
  • 3
  • 17
  • 33
Alexander Solonik
  • 9,838
  • 18
  • 76
  • 174
  • *"How do i avaoid this by changing the user agent in the new opened window"* write a browser-plugin and tell your users to use it. If FB detects your UA, it will do it from the User-Agent [HTTP header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers), not from the property exposed in JS that you try to manipulate. A Page as big as FB with such a redirect should allow you to navigate back to the desktop version. Usually by setting some flag in the query String. Find out what that flag is and add it yourself to the URL you open. – Thomas Feb 04 '20 at 11:52
  • @Quentin, not a dupe of that question, as OP needs to set a http-header for a `window.open()` not for a `XHR`, and that's a significant difference. The problem here: afaik there's no solution in JS for OPs request, but there is (most likely) one for his problem. #XY-problem – Thomas Feb 04 '20 at 12:00
  • @Thomas — The duplicate is not asking specifically about XHR, and the answer is still the same. – Quentin Feb 04 '20 at 12:02
  • @Thomas , the parameter is ?m2w , but it no longer works – Alexander Solonik Feb 04 '20 at 12:10

0 Answers0