Skip to content

Instantly share code, notes, and snippets.

@thanksshu
Last active August 14, 2025 08:45
Show Gist options
  • Select an option

  • Save thanksshu/81c0d5eb2262d5f88770ce4691e56ae9 to your computer and use it in GitHub Desktop.

Select an option

Save thanksshu/81c0d5eb2262d5f88770ce4691e56ae9 to your computer and use it in GitHub Desktop.
deeplx for cloundflare workers (DEPRECATED)
export default {
async fetch(request, env, ctx) {
const DEEPL_BASE_URL = 'https://www2.deepl.com/jsonrpc';
const DEEPLX_TOKEN = env.DEEPLX_TOKEN;
const headers = new Headers({
'Content-Type': 'application/json',
'Accept': '*/*',
'x-app-os-name': 'iOS',
'x-app-os-version': '16.3.0',
'Accept-Language': 'en-US,en;q=0.9',
'Accept-Encoding': 'gzip, deflate, br',
'x-app-device': 'iPhone13,2',
'User-Agent': 'DeepL-iOS/2.9.1 iOS 16.3.0 (iPhone13,2)',
'x-app-build': '510265',
'x-app-version': '2.9.1',
'Connection': 'keep-alive',
});
if (request.headers.get('Deeplx-Token') !== DEEPLX_TOKEN) {
return new Response(JSON.stringify({}),
{ status: 403 });
}
const requestData = await request.json();
const iCount = ((requestData.text || '').split('i').length - 1);
const id = Math.floor(Math.random() * (8399998 - 8300000) + 8300000) * 1000;
const timestamp = Date.now();
const deeplTimestamp = (iCount === 0) ? timestamp : (timestamp - (timestamp % iCount) + iCount);
const postData = {
jsonrpc: '2.0',
method: 'LMT_handle_texts',
id: id,
params: {
texts: [{
text: requestData.text,
requestAlternatives: Math.max(Math.min(3, requestData.numberAlternative), 0)
}],
splitting: 'newlines',
lang: {
source_lang_user_selected: requestData.sourceLang.toUpperCase() || 'AUTO',
target_lang: requestData.targetLang.toUpperCase() || 'EN',
},
timestamp: deeplTimestamp,
},
};
let postDataJson = JSON.stringify(postData);
if ((id + 5) % 29 === 0 || (id + 3) % 13 === 0) {
postDataJson = postDataJson.replace('"method":"', '"method" : "');
} else {
postDataJson = postDataJson.replace('"method":"', '"method": "');
}
console.log(postDataJson);
return await fetch(DEEPL_BASE_URL,
{ body: postDataJson, method: 'POST', headers: headers });
},
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment