Last active
January 15, 2024 07:03
-
-
Save starxg/d84c42ab29b351514b31c907da67b258 to your computer and use it in GitHub Desktop.
Google Translate replaced with Microsoft Translate
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html lang="zh"> | |
<body> | |
<h1>My Web Page</h1> | |
<p>Hello everybody!</p> | |
<p>Translate this page:</p> | |
<div id="google_translate_element"></div> | |
<script type="text/javascript"> | |
function googleTranslateElementInit() { | |
new google.translate.TranslateElement({ pageLanguage: 'en' }, 'google_translate_element'); | |
} | |
</script> | |
<script src="https://cdn.staticfile.org/jquery/1.12.4/jquery.min.js"></script> | |
<script src="https://unpkg.com/ajax-hook@3.0.3/dist/ajaxhook.min.js"></script> | |
<script src="https://cdn.jsdelivr.net/npm/qs@6.11.2/dist/qs.min.js"></script> | |
<script type="text/javascript" | |
src="https://translate.google.com/translate_a/element.js?cb=googleTranslateElementInit"></script> | |
<script> | |
let auth = '' | |
$.get('https://edge.microsoft.com/translate/auth', function (res) { | |
auth = res | |
}) | |
</script> | |
<script> | |
console.log(ah.proxy({ | |
onRequest: (config, handler) => { | |
if (config.url.indexOf('https://translate.googleapis.com/translate_a/t') === 0) { | |
const params = Qs.parse(config.url) | |
config.body = JSON.stringify(Qs.parse(config.body).q.map(e => { return { Text: e } })) | |
config.url = `https://api-edge.cognitive.microsofttranslator.com/translate?from=${params.sl}&to=${params.tl}&api-version=3.0&includeSentenceLength=true` | |
config.headers['content-type'] = 'application/json' | |
config.headers['Authorization'] = 'Bearer ' + auth | |
} | |
handler.next(config); | |
}, | |
onResponse: (response, handler) => { | |
response.response = JSON.stringify(JSON.parse(response.response) | |
.map(e => e.translations[0].text)) | |
handler.next(response) | |
} | |
})) | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment