Can you help me update my Chrome search engine for Google Translate?
April 14, 2019 4:44 PM   Subscribe

I had this Chrome search engine from years and years ago. It worked great until something changed a while back (possibly the Google Translate URL parameters), and now no dice. I've been getting by just updating the 3 arguments in a bookmark for the search URL, but can you help me update my search engine instead?

This is the code for my Chrome search engine, which was initially a Firefox bookmarklet and used to work just fine in Chrome as well.
javascript:strRaw=%22%s%22;strArray=strRaw.split(%22%20%22);if(strArray.length==2){location=%22http://translate.google.com/translate?prev=_t&hl=en&ie=UTF-8&u=%22+location.href+%22&sl=%22+strArray[0]+%22&tl=%22+strArray[1];}else{for(i=3;i
And this is the URL I end up using, just replacing the three arguments ("es," "en," and "guia de discusion") with 1. whatever language I'm translating from, 2. the language I'm translating to, and 3. the phrase to be translated
https://translate.google.com/?sl=la&tl=guia#es/en/la%20guia%20de%20discusion
This URL works as well (it's what's generated by the Google Translate web app), and doesn't have an extra "guia":
https://translate.google.com/#view=home&op=translate&sl=es&tl=en&text=guia%20de%20discusion
Can you suggest how I might put these together and update the bookmarklet/search engine code so it works again, knowing that the URLs above are now what works?
posted by limeonaire to Computers & Internet (3 answers total)
 
Your bookmarklet above cuts off at:

else{for(i=3;i

... almost certainly the next character is < which is the beginning of markup and is causing it to get cut off when it's interpreted as the beginning of an html tag. Try running your bookmarklet code through an encoder such as this one and re-posting it.
posted by axiom at 5:11 PM on April 14 [1 favorite]


Here's a bookmarklet to do translation. It doesn't work with the Chrome browser search mechanism b/c javascript is entirely disabled there (this is why your above bookmarklet stopped working; the change to the translate URL formatting is incidental, no amount of rewriting that is going to cause chrome to allow javascript there).

langs=("aa,ab,ae,af,ak,am,an,ar,as,av,ay,az,ba,be,bg,bh,bi,bm,bn,bo,br,bs,ca,ce,ch,co,cr,cs,cu,cv,cy,da,de,dv,dz,ee,el,en,"+
       "eo,es,et,eu,fa,ff,fi,fj,fo,fr,fy,ga,gd,gl,gn,gu,gv,ha,he,hi,ho,hr,ht,hu,hy,hz,ia,id,ie,ig,ii,ik,io,is,it,iu,ja,jv,"+
       "ka,kg,ki,kj,kk,kl,km,kn,ko,kr,ks,ku,kv,kw,ky,la,lb,lg,li,ln,lo,lt,lu,lv,mg,mh,mi,mk,ml,mn,mr,ms,mt,my,na,nb,nd,ne,"+
       "ng,nl,nn,no,nr,nv,ny,oc,oj,om,or,os,pa,pi,pl,ps,pt,qu,rm,rn,ro,ru,rw,sa,sc,sd,se,sg,si,sk,sl,sm,sn,so,sq,sr,ss,st,"+
       "su,sv,sw,ta,te,tg,th,ti,tk,tl,tn,to,tr,ts,tt,tw,ty,ug,uk,ur,uz,ve,vi,vo,wa,wo,xh,yi,yo,za,zh,zu").split(',');
query = prompt("Translate",window.getSelection());
if (!query || query.trim().length == 0) {
  alert("Nothing to do!");
} else {
  args = query.split(" ");
  src = "auto";
  trg = "en";
  if (args[0].indexOf(",") > 0) {
    lngCodes = args.shift().split(",");
    if (langs.indexOf(lngCodes[0]) >= 0 && langs.indexOf(lngCodes[1]) >= 0) {
      src = lngCodes[0];
      trg = lngCodes[1];
    }
  }
  trans = "https://translate.google.com/#view=home&op=translate&sl="+src+"&tl="+trg;
  comma = "&text=";
  for (i = 0; i < args.length; i++) {
    trans+=comma+args[i];
    comma=" ";
  }
  window.open(encodeURI(trans),"_blank");
}

Mefi won't let me make a "javascript:" link (for security reasons), so you'll have to cut-and-paste that code into a bookmarklet creator like this one. Just paste it in, name it (i.e., replace "this link" with something like "Translate"), then hit "convert to bookmarklet". Then drag the bookmarklet link (in blue) to your browser toolbar.

The way this bookmarklet works is that you can highlight some text on the page and then click the bookmarklet button in your browser toolbar. It will prompt you with a text input (which will contain the highlighted text, if any). You can just hit OK (after putting some text in, if you didn't highlight anything before clicking the bookmarklet) and it will attempt to have the translation service guess the source language (and will translate to english). Alternately you can edit the contents of the box, prepending it with a comma-separated pair of language codes which will specify the source and target languages respectively. So "es,fr hola mundo" will result in "salut monde" for example.
posted by axiom at 6:47 PM on April 14 [1 favorite]


Ah good call, here's the non-cut-off version.

javascript:strRaw=%22%s%22;strArray=strRaw.split(%22%20%22);if(strArray.length==2){location=%22http://translate.google.com/translate?prev=_t&hl=en&ie=UTF-8&u=%22+location.href+%22&sl=%22+strArray[0]+%22&tl=%22+strArray[1];}else{for(i=3;

Edit: It still got cut off, because it's Javascript. So never mind.

But yeah, I thought I'd vaguely read something about Javascript not being allowed for Chrome search engines anymore, so maybe it doesn't matter. For the meantime, for expediency's sake, I might just switch to a URL that only requires one argument and defaults to Spanish, since that's what I translate from the most. I'm interested in other suggestions!
posted by limeonaire at 8:41 PM on April 14


« Older My house is built like a tank that I can't drill...   |   Do you love your wedge sleep pillow? Newer »

You are not logged in, either login or create an account to post comments