ผู้ใช้:ZilentFyld/Zilentscriptwp.js

จากวิกิพีเดีย สารานุกรมเสรี

หมายเหตุ: หลังเผยแพร่ คุณอาจต้องล้างแคชเว็บเบราว์เซอร์ของคุณเพื่อดูการเปลี่ยนแปลง

  • ไฟร์ฟอกซ์ / ซาฟารี: กด Shift ค้างขณะคลิก Reload หรือกด Ctrl-F5 หรือ Ctrl-R (⌘-R บนแมค)
  • กูเกิล โครม: กด Ctrl-Shift-R (⌘-Shift-R บนแมค)
  • อินเทอร์เน็ตเอกซ์พลอเรอร์ และ Edge: กด Ctrl ค้างขณะคลิก Refresh หรือกด Ctrl-F5
  • โอเปร่า: กด Ctrl-F5
var pageName = mw.config.get('wgPageName');
var pageTitle = mw.config.get('wgTitle');
var indexUrl = mw.util.wikiScript('index');
var apiUrl = mw.util.wikiScript('api');
var commonsUrl = '//upload.wikimedia.org/wikipedia/commons';
var thmonth = {
    'January': 'มกราคม',
    'February': 'กุมภาพันธ์',
    'March': 'มีนาคม',
    'April': 'เมษายน',
    'May': 'พฤษภาคม',
    'June': 'มิถุนายน',
    'July': 'กรกฎาคม',
    'August': 'สิงหาคม',
    'September': 'กันยายน',
    'October': 'ตุลาคม',
    'November': 'พฤศจิกายน',
    'December': 'ธันวาคม',
};

function createAnchor() {
if (!($('#entireanchor').length)) {
$('.wikiEditor-ui').before(`<div id="entireanchor" class="toccolours" style="overflow:auto">
<div style="float:left">Anchor (use when searching): <a onclick="clearAnchor()">clear</a></div><div style="float:right">More tools: <a onclick="blockSwitch('#enwiktblock')">wiktionary</a> <a onclick="blockSwitch('#longdoblock')">longdo</a></div>
<div id="enwiktblock" style="float:right;position: absolute;z-index: 10;right: 0px;margin-top: 93px;display:none;width: 50%;min-width: 300px;overflow: auto;height: 45%;" class="toccolours">
<a href="javascript:blockSwitch('#enwiktblock')" style="float: right;">Close</a><br>
Go to: <a href="#top">Top</a> <a href="#Translations">Translations</a>` + /*<p id="wiktblocktop"></p>*/ `
<div id="enwiktdiv" style="overflow: scroll;height: 95%;width">
If you see this, you can highlighted text and click the wiktionary icon in toolbar to search
</div>
</div>
<div id="longdoblock" style="float:right;position: absolute;z-index: 10;right: 0px;margin-top: 93px;display:none;width: 50%;min-width: 300px;overflow: auto;height: 45%;" class="toccolours">
<a href="javascript:blockSwitch('#longdoblock')" style="float: right;">Close</a><br>
<div id="longdodiv" style="overflow: scroll;height: 95%;width">If you see this, you can highlighted text and click at dictionary icon in toolbar to search</div>
</div>
<br>
<div id="anchor" style="overflow:auto"></div>
</div>`);
}
}

createAnchor();

function clearAnchor() {
    $('#anchor').html('');
}

function blockSwitch(ele) {
    var ewd = $(ele);
    if (ewd.css('display') == 'block') {
        ewd.css('display', 'none');
    } else {
        ewd.css('display', 'block');
    }
}

mw.loader.using('ext.wikiEditor').then(function () {
    $('#wpTextbox1').wikiEditor('addToToolbar', {
        'section': 'main',
        'groups': {
            'zilentscript': {
                'label': "ZilentScript:"
            }
        }
    });
    $('#wpTextbox1').wikiEditor('addToToolbar', {
        'section': 'main',
        'group': 'zilentscript',
        'tools': {
            'qlink': {
                'label': 'ลิงค์รวดเร็ว',
                'type': 'button',
                'icon': commonsUrl + '/b/b7/Interlink_alt.png',
                'action': {
                    'type': 'encapsulate',
                    'options': {
                        'pre': '[[', 'peri': 'ลิงค์', 'post': ']]'
                    }
                }
            },
            'thname': {
                label: 'หน้าภาษาไทยที่เชื่อมกับวิกิภาษาอังกฤษ',
                type: 'button',
                icon: commonsUrl + '/thumb/8/80/Wikipedia-logo-v2.svg/22px-Wikipedia-logo-v2.svg.png',
                action: {
                    type: 'callback',
                    execute: function () {
                        var textbox = $('#wpTextbox1');
                        var selsave = saveSelection('#wpTextbox1');
                        var val = textbox.val().substring(selsave[0], selsave[1]);
                        console.log(val);
                        searchEng(val);
                    }
                }
            },
            'mymemory': {
                label: 'แปลอัตโนมัติ',
                type: 'button',
                icon: commonsUrl + '/thumb/0/0e/Noun_Project_translate_icon_2822454.svg/22px-Noun_Project_translate_icon_2822454.svg.png',
                action: {
                    type: 'callback',
                    execute: function () {
                        $('#anchor').html('Seaching...');
                        $('#enwiktblock').css('display', 'none');
                        var textbox = $('#wpTextbox1');
                        var selsave = saveSelection('#wpTextbox1');
                        var val = textbox.val().substring(selsave[0], selsave[1]);
                        console.log(val);
                        $.get('https://api.mymemory.translated.net/get', {
                            'q': val,
                            'langpair': 'en|th',
                            'of': 'json'
                        }, function (data) {
                            createAnchor();
                            $('#anchor').html('<div style="float:left">Translated:</div><textarea id="anchorbox" style="display:inline;float:left;width:200px;resize:none"></textarea>');
                            $('#anchorbox').val(data.responseData.translatedText);
                        });
                    }
                }
            }
        }
    });
    $('#wpTextbox1').wikiEditor('addToToolbar', {
        'sections': {
            'otherscript': {
                'type': 'toolbar',
                'label': 'Script อื่น ๆ',
                'groups': {
                    '่dateformat': {
                        'label': "เปลี่ยนวันที่",
                        'tools': {
                            'date': {
                                label: 'วันที่จากอัง-ไทย',
                                type: 'button',
                                icon: commonsUrl + '/thumb/3/34/US_to_Thai_date_format_with_BE.svg/37px-US_to_Thai_date_format_with_BE.svg.png',
                                action: {
                                    type: 'replace',
                                    options: {
                                        regex: /^(.*?)$/g,
                                        regexReplace: function (match) {
                                            var exp = match;
                                            exp = exp.replace(/([0-9]+) ([A-z]+) (|[1-2])(|[0-9])(|[0-9])([0-9])/g, function (match, d, m, y) {
                                                return d + ' ' + thmonth[m] + ' พ.ศ.' + (parseInt(y) + 543);
                                            });
                                            exp = exp.replace(/([A-z]+) ([0-9]+), ([0-9]+)/g, function (match, m, d, y) {
                                                return d + ' ' + thmonth[m] + ' พ.ศ.' + (parseInt(y) + 543);
                                            });
                                            return exp;
                                        }
                                    }
                                }
                            },
                            'datece': {
                                label: 'วันที่จากอัง-ไทยมี ค.ศ.',
                                type: 'button',
                                icon: commonsUrl + '/thumb/9/9c/US_to_Thai_date_format_with_CE.svg/37px-US_to_Thai_date_format_with_CE.svg.png',
                                action: {
                                    type: 'replace',
                                    options: {
                                        regex: /^(.*?)$/g,
                                        regexReplace: function (match) {
                                            var exp = match;
                                            exp = exp.replace(/([0-9]+) ([A-z]+) (|[1-2])(|[0-9])(|[0-9])([0-9])/g, function (match, d, m, y) {
                                                return d + ' ' + thmonth[m] + ' ค.ศ. ' + (parseInt(y));
                                            });
                                            exp = exp.replace(/([A-z]+) ([0-9]+), ([0-9]+)/g, function (match, m, d, y) {
                                                return d + ' ' + thmonth[m] + ' ค.ศ. ' + (parseInt(y));
                                            });
                                            return exp;
                                        }
                                    }
                                }
                            },
                            'year': {
                                label: 'คศ-พศ',
                                type: 'button',
                                icon: commonsUrl + '/thumb/c/ca/Calendar_icon_2.svg/22px-Calendar_icon_2.svg.png',
                                action: {
                                    type: 'replace',
                                    options: {
                                        regex: /([0-9]+)/g,
                                        regexReplace: function (match, y) {
                                            return 'พ.ศ. ' + (parseInt(y) + 543);
                                        }
                                    }
                                }
                            }
                        }
                    },
                    'dictionarytab': {
                        'label': 'แท็บ',
                        'tools': {
                            'wiktblock': {
                                label: 'กล่อง wikt',
                                type: 'button',
                                icon: commonsUrl + '/thumb/8/83/En.wiktionary_favicon.svg/22px-En.wiktionary_favicon.svg.png',
                                action: {
                                    type: 'callback',
                                    execute: function () {
                                        blockSwitch('#enwiktblock');
                                        createAnchor();
                                    }
                                }
                            },
                            'longdoblock': {
                                label: 'กล่อง longdo',
                                type: 'button',
                                icon: commonsUrl + '/thumb/f/f7/Accessories-dictionary_Faenza.svg/22px-Accessories-dictionary_Faenza.svg.png',
                                action: {
                                    type: 'callback',
                                    execute: function () {
                                        blockSwitch('#longdoblock');
                                        createAnchor();
                                    }
                                }
                            }
                        }
                    },
                    'dictionary': {
                        label: 'พจนานุกรม',
                        tools: {
                            'enwikt': {
                                label: 'ค้นหาวิกิพจนานุกรมอังกฤษ',
                                type: 'button',
                                icon: commonsUrl + '/thumb/8/83/En.wiktionary_favicon.svg/22px-En.wiktionary_favicon.svg.png',
                                action: {
                                    type: 'callback',
                                    execute: function () {
                                        $('#anchor').html('Seaching...');
                                        $('#enwiktblock').css('display', 'none');
                                        var textbox = $('#wpTextbox1');
                                        var selsave = saveSelection('#wpTextbox1');
                                        var val = textbox.val().substring(selsave[0], selsave[1]);
                                        $.get('https://en.wiktionary.org/w/api.php', {
                                            action: 'parse',
                                            prop: 'text',
                                            page: val,
                                            section: '1',
                                            format: 'json',
                                            disabletoc: true,
                                            origin: location.origin
                                        }, function (data) {
                                            $('#anchor').html('Done...');
                                            $('#wiktblocktop').html('<a href="https://en.wiktionary.org/wiki/"' + data.parse.title + '">This page</a>');
                                            $('#enwiktdiv').html(data.parse.text['*']);
                                            $('#enwiktblock').css('display', 'block');
                                            setTimeout(function () { clearAnchor() }, 500);
                                            console.log(data);
                                        });
                                    }
                                }
                            },
                            'thwikt': {
                                label: 'ค้นหาวิกิพจนานุกรมไทย',
                                type: 'button',
                                icon: commonsUrl + '/thumb/f/f9/Wiktionary_small.svg/22px-Wiktionary_small.svg.png',
                                action: {
                                    type: 'callback',
                                    execute: function () {
                                        $('#anchor').html('Seaching...');
                                        $('#enwiktblock').css('display', 'none');
                                        var textbox = $('#wpTextbox1');
                                        var selsave = saveSelection('#wpTextbox1');
                                        var val = textbox.val().substring(selsave[0], selsave[1]);
                                        $.get('https://th.wiktionary.org/w/api.php', {
                                            action: 'parse',
                                            prop: 'text',
                                            page: val,
                                            section: '1',
                                            format: 'json',
                                            disabletoc: true,
                                            origin: location.origin
                                        }, function (data) {
                                            $('#anchor').html('Done...');
                                            $('#wiktblocktop').html('<a href="https://th.wiktionary.org/wiki/"' + data.parse.title + '">This page</a>');
                                            $('#enwiktdiv').html(data.parse.text['*']);
                                            $('#enwiktblock').css('display', 'block');
                                            setTimeout(function () { clearAnchor() }, 500);
                                            console.log(data);
                                        });
                                    }
                                }
                            },
                            'longdo': {
                                label: 'ค้นหาลองดู',
                                type: 'button',
                                icon: commonsUrl + '/thumb/f/f7/Accessories-dictionary_Faenza.svg/22px-Accessories-dictionary_Faenza.svg.png',
                                action: {
                                    type: 'callback',
                                    execute: function () {
                                        $('#anchor').html('Seaching...');
                                        $('#enwiktblock').css('display', 'none');
                                        var textbox = $('#wpTextbox1');
                                        var selsave = saveSelection('#wpTextbox1');
                                        var val = textbox.val().substring(selsave[0], selsave[1]);
                                        $.ajax({
                                            method: "GET",
                                            url: 'https://cors-anywhere.herokuapp.com/https://dict.longdo.com/mobile.php',
                                            data: { search: val }
                                        }).done(function (data) {
                                            $('#anchor').html('Done...');
                                            $('#longdodiv').html(data);
                                            $('#longdoblock').css('display', 'block');
                                            setTimeout(function () { clearAnchor() }, 500);
                                            console.log(data);
                                        });
                                    }
                                }
                            },
                        }
                    }
                }
            }
        }
    });
});
function searchEng(title) {
    $.get('https://en.wikipedia.org/w/api.php', {
        action: 'query',
        prop: 'langlinks',
        titles: title,
        lllang: 'th',
        format: 'json',
        origin: location.origin
    }, function (data) {
        var langdata = Object.values(data.query.pages)[0];
        if (langdata.langlinks) {
            createAnchor();
            console.log(langdata.langlinks[0]['*']);
            $('#anchor').html('<div style="float:left">Wikipedia Search:</div><textarea id="anchorbox" style="display:inline;float:left;width:200px;resize:none"></textarea>');
            $('#anchorbox').val(langdata.langlinks[0]['*']);
            var selsave = saveSelection('#wpTextbox1');
            document.getElementById('anchorbox').select();
            document.execCommand('copy');
            restoreSelection(selsave, '#wpTextbox1');
        } else {
            mw.notify('ไม่พบ');
            $('#anchor').val('Not found');
        }
    });
}

function saveSelection(selection) {
    var sele = $(selection);
    if (true) {
        var exp = [];
        exp[0] = sele.prop('selectionStart');
        exp[1] = sele.prop('selectionEnd');
        console.log(exp);
        return exp;
    } else {
        console.log('not select');
        return [0, 0];
    }
}

function restoreSelection(selection, ele) {
    if (selection !== [0, 0]) {
        var sele = $(ele);
        sele.focus();
        sele.prop('selectionStart', selection[0]);
        sele.prop('selectionEnd', selection[1]);
        console.log(selection);
    }
}