Discussies » Scriptaanvragen

More of a script modification than of a script creation : opening it in a new tab

Deleted user 128
§
Geplaatst: 20-04-2017
Bijgewerkt: 20-04-2017

More of a script modification than of a script creation : opening it in a new tab

Hi,

I have a script I use as a bookmarklet and what I'd wish is that the result opens in a new tab instead of the current one. It's a basic script intended to Google Translate either highlighted text if applicable otherwise the whole page. Works OK except that I'd prefer the Google Translated page to appear in a new tab.

Here it is :

javascript:var%20t=((window.getSelection&&window.getSelection())||(document.getSelection&&document.getSelection())||(document.selection&&document.selection.createRange&&document.selection.createRange().text));var%20e=(document.charset||document.characterSet);if(t!=''){location.href='https://translate.google.com/translate_t?text='+t+'&hl=en&langpair=auto|en&tbb=1&ie='+e;}else{location.href='https://translate.google.com/translate?u='+escape(location.href)+'&hl=en&langpair=auto|en&tbb=1&ie='+e;};

Many thanks :)

§
Geplaatst: 21-04-2017

Instead of

location.href = URI;

try

window.open(URI, '_blank');

As long as you normally divert new windows to a new tab instead, that should work.

Deleted user 128
§
Geplaatst: 21-04-2017

Thanks, Jefferson Scher, for your help.

I tried replacing
location.href = URI;
with
window.open(URI, '_blank');
and indeed the result opened in a new tab, but the original (current tab) was blankened with only the mention of javascript at the top ...

javascript:var%20t=((window.getSelection&&window.getSelection())||(document.getSelection&&document.getSelection())||(document.selection&&document.selection.createRange&&document.selection.createRange().text));var%20e=(document.charset||document.characterSet);if(t!=''){window.open('https://translate.google.com/translate_t?text='+t+'&hl=en&langpair=auto|en&tbb=1&ie='+e,'_blank');}else{window.open('https://translate.google.com/translate?u='+escape(location.href)+'&hl=en&langpair=auto|en&tbb=1&ie='+e,'_blank');};

Maybe have I incorrectly replaced the setting?
But your suggestion got me on the right path, and I remembered another bookmarklet I had which opens result in a new tab, with
void(open((URI));

I have the following which runs fine : original tab is preserved, result opens in a new tab :

javascript:var%20t=((window.getSelection&&window.getSelection())||(document.getSelection&&document.getSelection())||(document.selection&&document.selection.createRange&&document.selection.createRange().text));var%20e=(document.charset||document.characterSet);if(t!=''){void(open('https://translate.google.com/translate_t?text='+t+'&hl=en&langpair=auto|en&tbb=1&ie='+e));}else{void(open('https://translate.google.com/translate?u='+escape(location.href)+'&hl=en&langpair=auto|en&tbb=1&ie='+e));};

Thanks again, because the fact is I had no clue where to even start from (I couldn't "conceptualize" the process). I'm no coder!
Now i'll know better how to dig in the stuff!!

§
Geplaatst: 05-05-2017

Don't you want to try this plugin?
Tab Mix Plus

Reactie plaatsen

Log in om antwoord te geven.