User:Darafsh/common.js

From translatewiki.net

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
mw.loader.load('http://meta.wikimedia.org/w/index.php?title=User:Reza1615/smalltools.js&action=raw&ctype=text/javascript');
//mw.loader.load('http://meta.wikimedia.org/w/index.php?title=User:Reza1615/smalltools.js&action=raw&ctype=text/javascript');

//targoman
//targoman
$(function () {
        $('<button>', { text: 'ترگمان', title: 'ترجمه از ترگمان' }).css('float', 'left').click(function (e) {
        var txt= $("div.row.tux-message.open > div.row.tux-message-editor > div.seven.columns.editcolumn >div.row >span.twelve.columns.sourcemessage")[0]
        if (txt==undefined){
            mw.notify('ابتدا یکی از موارد ترجمه را انتخاب کنید بعد بر روی این دکمه کلیک کنید')
            return
        }
        txt=txt.textContent.replace(/ /g,'%20').replace(/\_/g,'%20')
        txt=txt.replace(/\{\{[^\|\}]+\|([^\}]+)\}\}/g,'$1')
        txt=txt.replace(/$[0-9]+/g,'')
        $.getJSON('//tools.wmflabs.org/rezabot/targoman2.php', { entitle: mw.config.get('wgSiteName'), type:txt }).then(function (result) {
                $("div.row.tux-message.open > div.row.tux-message-editor > div.seven.columns.editcolumn >div.row.tux-editor-editarea-block >div.editarea.twelve.columns >div.tux-notices-block ").next('textarea').val(result.TrResult)
                $('.tux-editor-save-button', dialog).removeAttr('disabled')                
                mw.notify('ترجمه پیشنهادی ترگمان در بخش ویرایش قرار داده شد')
            });
    }).insertBefore('.row.tux-message-selector');
    
});

function auto_translations(e) {
    var dialog = $('.row.tux-message.open .row.tux-message-editor');
    var quality = $("div.columns.quality", dialog);
    var matchPercent =0
    if (!quality.length) {
        var passbutton = $('.tux-editor-skip-button', dialog).removeAttr('disabled')
        passbutton[0].focus();
        passbutton.click();
    }
    try {
            var matchPercent = +quality[0].textContent.split(/[%٪]/)[0].replace(/[۰-۹]/g, function (x) {
                return String.fromCharCode(x.charCodeAt(0) - 1728/*'۰'.charCodeAt('0') - '0'.charCodeAt('0')*/);
            });
            var suggestedTranslation = $("div.columns.suggestiontext", dialog)[0].textContent;
    }catch(err) {
        var passbutton = $('.tux-editor-skip-button', dialog).removeAttr('disabled')
        passbutton[0].focus();
        passbutton.click();
    }

    
    if (matchPercent > 80) {
        var editbox = $('textarea', dialog).val(suggestedTranslation);
        var button = $('.tux-editor-save-button', dialog).removeAttr('disabled')
        button[0].focus();
        if (matchPercent === 100 && editbox.val() !== '') {
            button.click();
        }
    }else{
        var passbutton = $('.tux-editor-skip-button', dialog).removeAttr('disabled')
        passbutton[0].focus();
        passbutton.click();
    }
}

$('<button>', { text: 'ترجمه خودکار', title: 'ترجمه خودکار' }).css('float', 'left').click(function (e) {auto_translations(e)}).insertBefore('.row.tux-message-selector');

//$('<button>', { text: 'ترجمه خودکار', title: 'ترجمه خودکار' }).css('float', 'left').click([...Array(10).keys()].forEach(function (e,index) {setTimeout(auto_translations(e),1000*index)})).insertBefore('.row.tux-message-selector');