Skip to content

Instantly share code, notes, and snippets.

@michimani
Last active November 23, 2020 13:05
Show Gist options
  • Save michimani/02cb8de134d1fd54d80b34006d9a439b to your computer and use it in GitHub Desktop.
Save michimani/02cb8de134d1fd54d80b34006d9a439b to your computer and use it in GitHub Desktop.
A bookmarklet for translating web pages using DeepL.
javascript: (function () {
var API_KEY = '<your_deel_api_key>';
var DEEPL_TRANSLATE_EP = 'https://api.deepl.com/v2/translate';
var TARGET_LANBG = 'JA';
var bodyHtml = document.getElementsByTagName('body')[0].innerHTML;
var params = {
'auth_key': API_KEY,
'text': bodyHtml,
'target_lang': TARGET_LANBG,
'tag_handling': 'xml'
};
var data = new URLSearchParams();
Object.keys(params).forEach(key => data.append(key, params[key]));
fetch(DEEPL_TRANSLATE_EP, {
method: 'POST',
headers: {
'Content-Type': 'application/x-www-form-urlencoded; utf-8'
},
body: data
}).then(res => res.json()).then(resData => {
console.log(resData);
document.getElementsByTagName('body')[0].innerHTML = resData.translations[0].text;
});
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment