DG.
DG.

Reputation: 3517

Modify document.location so a new tab/window is opened instead of navigation in current tab

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?

Upvotes: 15

Views: 3785

Answers (5)

user3166578
user3166578

Reputation: 1

FWIW: This works on the latest firefox to search for the selected text in a new tab:

javascript:Qr=document.getSelection();if(!Qr){void(Qr=prompt('Keywords...',''))};if(Qr)window.open("http://google.com/search?query="+escape(Qr));void(0)

Upvotes: -1

Bruno
Bruno

Reputation: 1368

I was going to suggest using the ES5 Object.defineProperty, but as it should be, document.location is a "configurable: false" object, meaning that it can't be changed. For the curious, if you would be able to change it the code would be like this:

Object.defineProperty(document.location, 'href', {
     get: function() { return this.href },
     set: function(value) { this.href = value; window.open(value); return false} //Not sure about this part, but anyways...
});

Edit: Answering your question, i don't think that this is achievable via a bookmarlet.

Upvotes: 3

Gabriel Lupu
Gabriel Lupu

Reputation: 1819

I tried different methods but it's almost impossible to trace the href of the target window.

Using

window.onbeforeunload = function(e) {
    window.open(document.location.href);
};

will open your current window in another tab and the target in current window. The problem is that onbeforeunload is triggered not only on changing the location of the window, even on closing window or refreshing it. Depends where you want to use it.

PS:

window.onunload = function() {
    document.location = document.location;
}

if you test this, when you change the page location, you will be able to see the new url in the address bar for a moment then you will be redirected to your current page. I didn't managed to get the new target. Good luck!

Upvotes: 2

Eevee
Eevee

Reputation: 48536

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.

document.watch('location', function(prop, oldval, newval) {
    window.open(newval);
    return '#';
});

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.location and window.location:

javascript:(function(){var handle=function(prop, oldval, newval){window.open(newval); return '#';};document.watch('location',handle);window.watch('location',handle);})();

But even this won't work against assignment to document.location.href; the location object is some special thing that Object.watch refuses to work on.

If that's not enough, you could use onbeforeunload to prompt you when you try to navigate away and cancel it (if that's your goal here).

Best I've got, sorry!

Upvotes: 8

Jeroen K
Jeroen K

Reputation: 10438

You could only do something with the prototype if it were a function call rather than an assignment. For example in the case of document.write

document.write = function(txt) {
   alert("txt");
};

document.write("test");   

Upvotes: 1

Related Questions