Skip to content

Instantly share code, notes, and snippets.

@illusionfield
Last active March 21, 2024 05:25
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save illusionfield/db576c73a103f37e0044 to your computer and use it in GitHub Desktop.
Save illusionfield/db576c73a103f37e0044 to your computer and use it in GitHub Desktop.
Translate Bookmarklets
/**
* Web browsers use URIs for the href attribute of the <a> tag and for bookmarks. The URI scheme, such as http:, file:, or ftp:, specifies the protocol and the format for the rest of the string.
* Browsers also implement a prefix javascript: that to a parser is just like any other URI.
* Internally, the browser sees that the specified protocol is javascript, treats the rest of the string as a JavaScript application which is then executed, and uses the resulting string as the new page.
*
* The executing script has access to the current page, which it may inspect and change.
* If the script returns an undefined type (rather than, for example, a string), the browser will not load a new page, with the result that the script simply runs against the current page content.
* This permits changes such as in-place font size and color changes without a page reload.
*/
/* An anonymous function that does not return a value, define a function etc., can be used to force the script to return an undefined type: */
javascript:(function(){
//Statements returning a non-undefined type, e.g. assignments
})();
/* However, if a script includes a function definition/redefinition, such as function Use_this_globally(){...}, the environment will not be populated with it. For this reason an {arbitrary script} should be suffixed with ;void(...);. */
javascript:{arbitrary script};void(0);
javascript:{var d,b,o,v,p;b=(d=document).body;o=d.createElement('script');o.setAttribute('src','https://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit');o.setAttribute('type','text/javascript');b.appendChild(o);v=b.insertBefore(d.createElement('div'),b.firstChild);v.id='google_translate_element';v.style.display='none';p=d.createElement('script');p.text='function googleTranslateElementInit(){new google.translate.TranslateElement({pageLanguage:""},"google_translate_element");}';p.setAttribute('type','text/javascript');b.appendChild(p);};void(0);
translator = document.createElement "script"
translator.setAttribute "src", "https://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"
translator.setAttribute "type", "text/javascript"
document.body.appendChild translator
element = document.body.insertBefore document.createElement("div"), document.body.firstChild
element.id = "google_translate_element"
element.style.display = "none"
elementInit = document.createElement "script"
elementInit.text = 'function googleTranslateElementInit(){new google.translate.TranslateElement({pageLanguage:""},"google_translate_element");}'
elementInit.setAttribute "type", "text/javascript"
document.body.appendChild elementInit
javascript: {
s=document.createElement("script");
s.setAttribute("type", "text/javascript");
s.setAttribute("src", "http://labs.microsofttranslator.com/bookmarklet/default.aspx?f=js&to=hu");
document.body.appendChild(s);
};void(0);
javascript:{s=document.createElement("script");s.setAttribute("type","text/javascript");s.setAttribute("src","http://labs.microsofttranslator.com/bookmarklet/default.aspx?f=js&to=hu");document.body.appendChild(s);};void(0);
javascript:(function()%20%7Bvar%20protocol%20=%20window.location.protocol%20===%20'file:'%20?'http:'%20:%20'';var%20url%20=%20protocol+'//www.sprymedia.co.uk/VisualEvent/VisualEvent_Loader.js';if(%20typeof%20VisualEvent!='undefined'%20)%20%7Bif%20(%20VisualEvent.instance%20!==%20null%20)%20%7BVisualEvent.close();%7Delse%20%7Bnew%20VisualEvent();%7D%7Delse%20%7Bvar%20n=document.createElement('script');n.setAttribute('language','JavaScript');n.setAttribute('src',url+'?rand='+new%20Date().getTime());document.body.appendChild(n);%7D%7D)();
@tathastu871
Copy link

tathastu871 commented Oct 21, 2021

Google changed their site/code/etc. Now for: www.test.com/foo/bar.html you need to call: https://www-test-com.translate.goog/foo/bar.html?_x_tr_sl=auto&_x_tr_tl=en&_x_tr_hl=en&_x_tr_pto=nui

could you make a google translate bookmarklet?
The url that you have given has an intermediate url that parses into yours: Below bookmarklet works

javascript:( function() { var url = "https://translate.google.com/translate?sl=auto&tl=en&u=" + location.href; window.location.assign(url); } ) ();

@illusionfield
Copy link
Author

Google changed their site/code/etc. Now for: www.test.com/foo/bar.html you need to call: https://www-test-com.translate.goog/foo/bar.html?_x_tr_sl=auto&_x_tr_tl=en&_x_tr_hl=en&_x_tr_pto=nui
could you make a google translate bookmarklet?
The url that you have given has an intermediate url that parses into yours: Below bookmarklet works

javascript:( function() { var url = "https://translate.google.com/translate?sl=auto&tl=en&u=" + location.href; window.location.assign(url); } ) ();

The problem is that it cause page reload. I should think about how can solve the problem.

@anonim1979-2
Copy link

anonim1979-2 commented Oct 21, 2021

@tathastu871
Thank you for that updated bookmarklet, it seems all was needed was changing http to https in the old code for it to work.

@eNV25
Copy link

eNV25 commented Oct 6, 2023

Bookmarklet with icon:

<!DOCTYPE NETSCAPE-Bookmark-file-1>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=UTF-8">
<meta http-equiv="Content-Security-Policy"
      content="default-src 'self'; script-src 'none'; img-src data: *; object-src 'none'"></meta>
<TITLE>Bookmarks</TITLE>
<H1>Bookmarks Menu</H1>

<!--
Chrome: Bookmark Manager > Import bookmarks
Firefox: Bookmark Manager > Import and Backup > Import Bookmarks from HTML
-->

<DL><p>
    <DT><A HREF="javascript:location.assign(%22https://translate.google.com/translate?sl=auto&tl=en&u=%22+encodeURIComponent(location.href))" ADD_DATE="1696591387" LAST_MODIFIED="1696592155" ICON_URI="https://ssl.gstatic.com/translate/favicon.ico">Translate</A>
</DL>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment