I want to write a bookmarklet that will modify any webpage so that when the JavaScript code on that page does something like document.location = 'http://site.tld' or document.location.href = 'http://site.tld' the page will open a new tab or window instead of changing location; same as if code was window.open('http://site.tld');
Can I modify the prototype somehow to achieve this goal? (I use this word carelessly because although I’ve read about modifying prototypes a few times, I’ve never actually done it.) Or is there some other way?
You didn’t say which browser you’re using, but the only way I can think of to accomplish this is using Firefox’s
Object.watch.This will intervene on any attempt to set
document.location, open a window with the same argument instead, and then change the assignment to'#'instead (which will do nothing, usually).In bookmarklet form, and expanded to cover both
document.locationandwindow.location:But even this won’t work against assignment to
document.location.href; the location object is some special thing thatObject.watchrefuses to work on.If that’s not enough, you could use
onbeforeunloadto prompt you when you try to navigate away and cancel it (if that’s your goal here).Best I’ve got, sorry!