Skip to content

Instantly share code, notes, and snippets.

@mzuvin
Created December 29, 2019 18:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mzuvin/33b3bec09204e6794bfbc4647905afcc to your computer and use it in GitHub Desktop.
Save mzuvin/33b3bec09204e6794bfbc4647905afcc to your computer and use it in GitHub Desktop.
FREERİCE TRANSLATE Js BOOKMARKLET
/*
Coder by Mustafa
29.12.19 21:30
Ajax Mudehale ile FreeRice sitesinde çeviri işlemi
*/
// thanks https://gist.github.com/liron-navon/eff96016a233f102fe43030c3a16f4a5
const APIKEY = 'trnsl.1.1.20191229T171159Z.1c8e8e7908ec7585.bedb9463e1aa12faa0798882dc1742e44dc0ad53';
const FROM = 'en';
// default source language
const TO = 'tr';
// default target language
const yandexTranslateOrigin = 'https://translate.yandex.net';
const baseAPI = `${yandexTranslateOrigin}/api/v1.5/tr.json`;
const translateAPI = `${baseAPI}/translate?key=${APIKEY}`;
// to prevent cors in the browser
const withCors = (url)=>`https://cors-anywhere.herokuapp.com/${url}`;
// used to cache some of the text, making the translation faster and cheaper
const maxCacheSize = 10000;
const cache = new Map();
// translates a text from one language to another
async function translate(originalText, from=FROM, to=TO) {
if (typeof originalText !== 'string' || originalText === '') {
return '';
// no text to translate ¯\_(ツ)_/¯
}
const sourceText = originalText.trim();
// if you have long texts you should hash them here
const key = `${sourceText}${from}${to}`;
if (cache.has(key)) {
return cache.get(key);
// already cached this text (ᵔᴥᵔ)
}
if (cache.size >= maxCacheSize) {
cache.clear();
// too much stuff in the cache (ಠ_ಠ)
}
// getting free translation from yandex (Яндекс ☭ 🇷🇺)
const url = `${translateAPI}&lang=${from}-${to}&text=${encodeURIComponent(sourceText)}`;
const response = await fetch(withCors(url),{method:'POST',headers:{Origin:yandexTranslateOrigin // used for cors-anywhere
}});
const responseData = await response.json();
const text = responseData.text[0];
cache.set(key, text);
return text;
}
function sleep(time) {
return new Promise((resolve)=>setTimeout(resolve, time));
}
var urlx="";
var open = window.XMLHttpRequest.prototype.open, send = window.XMLHttpRequest.prototype.send, onReadyStateChange;
function openReplacement(method, url, async, user, password) {
var syncMode = async !== false ? 'async' : 'sync';
urlx=url;
console.warn('Preparing ' + syncMode + ' HTTP request : ' + method + ' ' + url);
return open.apply(this, arguments);
}
function sendReplacement(data,url) {
if (urlx.search("https://engine.freerice.com/")>-1){
sleep(4000).then(()=>{
console.log("url"+url)
console.warn('Sending HTTP request data : ', data);
if (this.onreadystatechange) {
this._onreadystatechange = this.onreadystatechange;
}
this.onreadystatechange = onReadyStateChangeReplacement;
return send.apply(this, arguments);
});
}//if
var button = document.querySelectorAll('.card-button');
var title = document.querySelectorAll('.card-title');
translate(title[0].innerText, 'en', 'tr').then((text)=>{
title[0].innerText = title[0].innerText + " " + text;
})
button.forEach(function(entry) {
translate(entry.innerText, 'en', 'tr').then((text)=>{
entry.innerText = entry.innerText + " " + text;
console.log(text);
console.log('bekleniyor..');
})
});
}
function onReadyStateChangeReplacement() {
console.warn('HTTP request ready state changed : ' + this.readyState);
if (this._onreadystatechange) {
return this._onreadystatechange.apply(this, arguments);
}
}
window.XMLHttpRequest.prototype.open = openReplacement;
window.XMLHttpRequest.prototype.send = sendReplacement;
@mzuvin
Copy link
Author

mzuvin commented Dec 29, 2019

javascript: var APIKEY = 'trnsl.1.1.20191229T171159Z.1c8e8e7908ec7585.bedb9463e1aa12faa0798882dc1742e44dc0ad53';
var FROM = 'en';
var TO = 'tr';
var yandexTranslateOrigin = 'https://translate.yandex.net';
var baseAPI = yandexTranslateOrigin + '/api/v1.5/tr.json';
var translateAPI = baseAPI + '/translate?key=' + APIKEY;
var withCors = (url)=>'https://cors-anywhere.herokuapp.com/' + url;
var maxCacheSize = 10000;
var cache = new Map();
async function translate(originalText, from=FROM, to=TO) {
    if (typeof originalText !== 'string' || originalText === '') {
        return '';
    }
    var sourceText = originalText.trim();
    var key = sourceText+from+to;
    if (cache.has(key)) {
        return cache.get(key);
    }
    if (cache.size >= maxCacheSize) {
        cache.clear();
    }
    var url = translateAPI+'&lang='+from+'-'+to+'&text='+encodeURIComponent(sourceText);
    var response = await fetch(withCors(url),{method:'POST',headers:{Origin:yandexTranslateOrigin}});
    var responseData = await response.json();
    var text = responseData.text[0];
    cache.set(key, text);
    return text;
}
function sleep(time) {
    return new Promise((resolve)=>setTimeout(resolve, time));
}
var urlx = "";
var open = window.XMLHttpRequest.prototype.open, send = window.XMLHttpRequest.prototype.send, onReadyStateChange;
function openReplacement(method, url, async, user, password) {
    var syncMode = async !== false ? 'async' : 'sync';
    urlx = url;
    return open.apply(this, arguments);
}
function sendReplacement(data, url) {
    if (urlx.search("https://engine.freerice.com/") > -1) {
        sleep(4000).then(()=>{
            if (this.onreadystatechange) {
                this._onreadystatechange = this.onreadystatechange;
            }
            this.onreadystatechange = onReadyStateChangeReplacement;
            return send.apply(this, arguments);
        }
        );
    }
    var button = document.querySelectorAll('.card-button');
    var title = document.querySelectorAll('.card-title');
    translate(title[0].innerText, 'en', 'tr').then((text)=>{
        title[0].innerText = title[0].innerText + "        " + text;
    }
    );
    button.forEach(function(entry) {
        translate(entry.innerText, 'en', 'tr').then((text)=>{
            entry.innerText = entry.innerText + "        " + text;
        }
        );
    });
}
function onReadyStateChangeReplacement() {
    if (this._onreadystatechange) {
        return this._onreadystatechange.apply(this, arguments);
    }
    
}
window.XMLHttpRequest.prototype.open = openReplacement;
window.XMLHttpRequest.prototype.send = sendReplacement;

@mzuvin
Copy link
Author

mzuvin commented Dec 29, 2019

Bookmarklet için yukarıdaki kodu yerimlerine ekleyin!

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