Change window.location prototype to disable certain redirects?
Asked Answered
R

3

9

I'm trying to selectively disable window.location, using Greasemonkey, at the start of the document.

I don't want to fully disable javascript, just disable some redirects done with javascript. They look like this:

window.location = "unwanted url";
Rancho answered 16/8, 2013 at 23:20 Comment(3)
Why do you want to do this?Mallissa
How are the redirects being done, exactly? There's at least 5 possible ways.Hanes
@brock, window.location=urltoredirect; (at)crazy I want to stop some redirections without disabling javascript...Rancho
H
5

You can't change the window.location prototype, because this a "native property" of window and it is not configurable.

On Firefox (Greasemonkey), if you try to override this then you will get errors like:

TypeError: can't redefine non-configurable property 'location'

and

Error: Permission denied to shadow native property

...depending on how you attempt it. Other browsers give similar errors.


To block this kind of relocation, you need to interfere with the page's javascript on a case-by-case basis.

See "Stop execution of Javascript function (client side) or tweak it" for a general approach that works in Firefox. Although it may be much easier, depending on your target page's exact code.

Hanes answered 17/8, 2013 at 6:25 Comment(2)
alright, this works when it's <script>content</script>, but what about <script src="url"></script>? is there a way to stop it too?Rancho
Yes, checkForBadJavascripts() has a mode that does that. Or, you could just use Adblock to block that exact script URL.Hanes
T
9

I don't think it's possible.

  • You can't overwrite properties or methods on window.location (fails silently)
  • you can't redefine its prototype with location.__proto__ = Something.prototype
  • location.constructor.prototype is basically Object.prototype
  • the constructor doesn't really do anything (like create the attributes or methods)
  • __definesetter__ fails silently
  • Object.defineProperty gives an error like, TypeError: Cannot redefine property: href
  • delete window.location and delete window.location.href don't do anything

I'm out of ideas...

Typecast answered 17/8, 2013 at 4:38 Comment(1)
This sucks! Seems that nothing has changed in 2016 :-(Valadez
H
5

You can't change the window.location prototype, because this a "native property" of window and it is not configurable.

On Firefox (Greasemonkey), if you try to override this then you will get errors like:

TypeError: can't redefine non-configurable property 'location'

and

Error: Permission denied to shadow native property

...depending on how you attempt it. Other browsers give similar errors.


To block this kind of relocation, you need to interfere with the page's javascript on a case-by-case basis.

See "Stop execution of Javascript function (client side) or tweak it" for a general approach that works in Firefox. Although it may be much easier, depending on your target page's exact code.

Hanes answered 17/8, 2013 at 6:25 Comment(2)
alright, this works when it's <script>content</script>, but what about <script src="url"></script>? is there a way to stop it too?Rancho
Yes, checkForBadJavascripts() has a mode that does that. Or, you could just use Adblock to block that exact script URL.Hanes
G
-4

quite too late but you can do it simply without removing the whole script with Object.prototype.watch:

window.watch('location', function( attr ,_ , target){
    if(target.indexOf('unwanted url') !== -1)return '#';
});
Georgenegeorges answered 11/5, 2015 at 18:58 Comment(4)
"TypeError: can't watch objects of non-native class Proxy"Kamp
cannot watch window or other paths to locationSanasanabria
This function is implemented only in Gecko as the op asked.And there was a bug in Firefox 23 to 27 that causes this type error but it's now fixed.Georgenegeorges
This does not work in FF 49, if it ever did at all. JS redirects still function with no interference.Hanes

© 2022 - 2024 — McMap. All rights reserved.