trante
trante

Reputation: 33986

Opening javascript links in new tab

(Question1, question2 and question3 looks how to force users open link in new tab)
But in my situation I visit some sites regularly and they have links like this:

<a href='javascript:window.open("/view.php?id=1234","_self")'>Link name</a>

This type of link makes me impossible to open link in new tab with a mouse click. Every time I see these links, I duplicate the tab in Chrome and click link inside the cloned tab. And go back to original tab and continue to surf. Is it possible to open these links in new tab with a chrome extension, js code or something?

You can try one of the links here: http://bit.ly/12dUk4V

Upvotes: 1

Views: 5389

Answers (2)

diego nunes
diego nunes

Reputation: 2828

. . The problem is that these links can be kind of "about:blank" because they are not specified in the href attribute normally, so it breaks your expected behavior when using ctrl+click, middle click or something alike. Sometimes sites links to "javascript:" pseudo-protocol, sometimes the link is for "#" with a "onclick" trigger... It depends on the situation.

. . For this specific case it's easy enough to write a user script that will rewrite these kind of links, if you're willing to use something like Tampermonkey:

// ==UserScript==
// @name       SelfLinks Fixer
// @namespace  http://dnun.es./
// @version    0.1
// @description  This script rewrites "window.open(..., '_self')" links so that you can click them as you wish.
// @match      http://libgen.info/*
// @copyright  2013, http://dnun.es.
// ==/UserScript==

var tRegExp = '^javascript: *'+
  '(window\\.)?open\\('+
  ' *(([\'"])([^\\3]+)\\3) *,'+
  ' *[\'"]_self[\'"] *'+
  '\\) *;? *$';
var fixLinksCheck = new RegExp(tRegExp);
var as = document.getElementsByTagName('a'), i = 0, n = as.length, a;
for (;i<n;i++) { a = as[i];
  if (fixLinksCheck.test(a.href)) { //damn you _self link!
    a.href = a.href.replace(fixLinksCheck, '$4');
  }
}

. . This code "fixes" only the "_self" links by changing them to normal links. You can then click them with middle button, holding ctrl/shift or whatever. It also leave the "_blank" or "_top" links untouched.

Upvotes: 1

Ali Bahraminezhad
Ali Bahraminezhad

Reputation: 326

Yes, it is possible. All you need is to inject a simple line of JavaScript code in every page. I had done it before in a Firefox extension.

You just need to override window.open method:

var open_= window.open;         

window.open = function(url, name, opts) {
   if (name === '_self') { name = '_blank'; }
   open_(url, '_blank', opts);
};

Complete code on JsFiddle: http://jsfiddle.net/dp4Uz/

Upvotes: 1

Related Questions