Error “Cannot redefine non-configurable property 'location'” on window.location in javascript using in Universal Windows app (UAP/UWP)

Gopal Devra 6 Reputation points
2020-01-30T05:58:59.46+00:00

I had a Windows 8.1 jsproject, now updated to the Universal Windows (UAP/UWP)app.

Previously this function was working, it was calling like

installLocationProxy(window);  
installLocationProxy(document);  

function installLocationProxy(obj) {  
            var descriptor;  
            var descriptorProto = obj;  
            do {  
                descriptor = Object.getOwnPropertyDescriptor(descriptorProto, "location");  
                descriptorProto = !descriptor && descriptorProto && Object.getPrototypeOf(descriptorProto);  
            } while (descriptor == null);  
            if (!descriptor || !descriptor.get || !descriptor.set)  
                return;  
            var proxy = new LocationProxy(descriptor.get.call(obj));  
            Object.defineProperty(obj, "location", {  
                "get": function () {  
                    return proxy;  
                },  
                "set": function (location) {  
                    if (typeof location === 'string' || location && location instanceof String) {  
                        proxy.assign(location, NavigationMethods.Location);  
                    }  
                    else {  
                        return descriptor.set.call(obj, location);  
                    }  
                }  
            });  
        }  

but now I am getting error "Cannot redefine non-configurable property 'location'" on this line Object.defineProperty(obj, "location".

While googling I found that the now the window.location is readonly and not writable, so it cannot be overridden.

For reference, it can be checked here
https://gist.github.com/zacharytamas/082e538784ebe07e40f9

But I am not sure how to override the window.location property? Can anybody help?

Universal Windows Platform (UWP)
{count} votes

2 answers

Sort by: Most helpful
  1. Roy Li - MSFT 31,766 Reputation points Microsoft Vendor
    2020-02-11T03:02:14.837+00:00

    Hello,

    Welcome to Microsoft Q&A!

    So you are trying to intercept and redirect the navigation, right?

    If it's your own site, you just call a JS function, do your logic in it and assign the new URL to the windows.location.

    But if it's not your site, it belongs to others, we don't recommend you to do this because of security issues.


  2. XEO 11 Reputation points
    2023-09-19T18:18:45.58+00:00

    i used this in my personal little adblocker script for years, and now this security feature is gone, and the little malicious javascripts are free to set new locations in my windows :-/

    0 comments No comments