Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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