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 :-/
Error “Cannot redefine non-configurable property 'location'” on window.location in javascript using in Universal Windows app (UAP/UWP)
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?
2 answers
Sort by: Newest
-
-
Roy Li - MSFT 32,051 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.