میدیاویکی:Gadget-CheckDictation.js

لە ئینسایکڵۆپیدیای ئازادی ویکیپیدیاوە
تێبینی: دوای پاشەکەوتکردن، پێویستە کاشی وێبگەڕەکەت پاک بکەیتەوە تا گۆڕانکارییەکان ببینیت. بۆ گووگڵ کڕۆم، فایەرفۆکس، مایکرۆسۆفت ئێج و سافاری: پەنجە لەسەر دوگمەی ⇧ Shift ڕاگرە و کرتە لەسەر Reload بکە. بۆ وردەکاری و ڕێنمایییەکان لەسەر وێبگەڕەکانی تر، بڕوانە ئێرە.
$.when($.ready, mw.loader.using(['mediawiki.api', 'mediawiki.util'])).then(function() {
    'use strict';

    var projectPageAllowedCats = [
        'ڕێنمایییەکانی ویکیپیدیا',
        'سیاسەتەکانی ویکیپیدیا',
        'یارمەتییەکانی ویکیپیدیا',
        'ئیسەیەکانی ویکیپیدیا',
        'ئیسەیە پاشکۆکانی ویکیپیدیا',
        'ئامرازەکانی ویکیپیدیا',
        'فێرکاریی ویکیپیدیا'
    ];

    var projectPageAllowedTitles = [
        'ویکیپیدیا:پشکنەری حونجە/تاقیکردنەوە',
        'ویکیپیدیا:پشکنەری حونجە/خۆڵەپەتانێ',
        'ویکیپیدیا:خۆڵەپەتانێ'
    ];

    var projectPageWrongListedTitles = 'ویکیپیدیا:پشکنەری_حونجە/دروستەکان/';

    if (mw.config.get('wgAction') !== 'view' ||
    	([0, 4, 12, 118].indexOf(mw.config.get('wgNamespaceNumber')) === -1 &&
    	 mw.config.get('wgPageName') !== 'بەکارھێنەر:' + mw.config.get('wgUserName') + '/خۆڵەپەتانێ') ||
        mw.util.getParamValue('diff') ||
        mw.util.getParamValue('oldid') ||
        $('.noarticletext').length) {
        return;
    }
    
    var siteSub = $('#siteSub');
    if (mw.config.get('skin') === 'timeless') {
    	siteSub = $('#p-namespaces');
    }

    if (mw.config.get('wgNamespaceNumber') === 4 && mw.config.get('wgPageName').search(projectPageWrongListedTitles) === 0) {
        var returnTitle = mw.config.get('wgPageName').split(projectPageWrongListedTitles)[1]
        siteSub.append(' ', $('<span>', {
            html: 'ئەم پەڕە دەستەواژەی قەدەغەکراوی تێدایە <a href="//ckb.wikipedia.org/wiki/' + returnTitle + '">' + returnTitle.replace(/\_/g, ' ') + '</a>.',
            class: 'CheckDictation-allgood-label'
        }));
    }

    if (mw.config.get('wgNamespaceNumber') === 4 &&
        projectPageAllowedTitles.indexOf(mw.config.get('wgPageName').replace(/_/g, ' ')) === -1 &&
        mw.config.get("wgCategories").every(function(x) {
            return projectPageAllowedCats.indexOf(x) === -1;
        })) {
        return;
    }
    if ($('ul[class="redirectText"]')[0]) {
        return;
    } //پەرەی ڕەوانەکەر مەپشکنە
    var pageContentCache;

    function getPageTextCache() {
        return pageContentCache ? $.Deferred().resolve(pageContentCache) : loadPage(mw.config.get('wgPageName'));
    }

    // Copyedited from https://de.wikipedia.org/wiki/MediaWiki:Gadget-Rechtschreibpruefung.js
    function markWord(node, text, word, hint, color, preparedId) {
        var pos, len, newnodes = 0;
        var newnode, middlenode, endnode;

        // textnode - search for word
        if (node.nodeType == 3) {
            pos = node.data.search(text);
            if (pos >= 0) {
                // create new span-element
                newnode = $('<span>', {
                    title: hint,
                    class: 'CheckDictation-marked CheckDictation-marked-' + word.replace(/ /g, '_'),
                    id: preparedId
                }).css('background-color', color)[0];

                // get length of the matching part
                len = node.data.match(text)[0].length;

                // splits content in three parts: begin, middle and end
                middlenode = node.splitText(pos);
                endnode = middlenode.splitText(len);

                // appends a copy of the middle to the new span-node
                newnode.appendChild(middlenode.cloneNode(true));
                // replace middlenode with the new span-node
                middlenode.parentNode.replaceChild(newnode, middlenode);

                newnodes = 1;
            }
        } else if ((node.nodeType == 1) // element node
            &&
            (node.hasChildNodes()) // with child nodes
            &&
            (node.tagName.toLowerCase() != "script") // no script, style and form
            &&
            (node.tagName.toLowerCase() != "style") && (node.tagName.toLowerCase() != "form")) {
            var this_child;
            for (this_child = 0; this_child < node.childNodes.length; this_child++) {
                this_child = this_child + markWord(node.childNodes[this_child], text, word, hint, color, preparedId);
            }
        }
        return newnodes;
    }

    function markWord2(node, text, word, hint, preparedId) {
        var pos, len, newnodes = 0;
        var newnode, middlenode, endnode;

        // textnode - search for word
        if (node.nodeType == 3) {
            pos = node.data.search(text);
            if (pos >= 0) {
                // create new span-element
                newnode = $('<span>', {
                    title: hint,
                    class: 'CheckDictation-marked CheckDictation-marked-' + word.replace(/ /g, '_'),
                    id: preparedId
                })[0];

                // get length of the matching part
                len = node.data.match(text)[0].length;

                // splits content in three parts: begin, middle and end
                middlenode = node.splitText(pos);
                endnode = middlenode.splitText(len);

                // appends a copy of the middle to the new span-node
                newnode.appendChild(middlenode.cloneNode(true));
                // replace middlenode with the new span-node
                middlenode.parentNode.replaceChild(newnode, middlenode);

                newnodes = 1;
            }
        } else if ((node.nodeType == 1) // element node
            &&
            (node.hasChildNodes()) // with child nodes
            &&
            (node.tagName.toLowerCase() != "script") // no script, style and form
            &&
            (node.tagName.toLowerCase() != "style") && (node.tagName.toLowerCase() != "form")) {
            var this_child;
            for (this_child = 0; this_child < node.childNodes.length; this_child++) {
                this_child = this_child + markWord2(node.childNodes[this_child], text, word, hint, preparedId);
            }
        }
        return newnodes;
    }

    function loadPage(title) {
        return (new mw.Api()).get({
            action: 'query',
            prop: 'revisions',
            titles: title,
            rvprop: 'content',
            format: 'json'
        }).then(function(data) {
            return ((data.query.pages[Object.keys(data.query.pages)[0] || ''].revisions || '')[0] || '')['*'];
        });
    }

    function savePage(title, text, summary) {
        return (new mw.Api()).post({
            action: 'edit',
            title: title,
            text: text,
            summary: summary,
            minor: '',
            token: mw.user.tokens.get('csrfToken')
        }).then(function(data) {
            if (data.error && data.error.info) {
                mw.notify(data.error.info);
            }
        }, function(data) {
            mw.notify(data);
        });
    }

    function postEdit(text){
        // چارەسەری [[:fa:Special:Permalink/25174868#متن هایلایت‌شده]]
        var otext = ''
        while (otext!=text){
            otext = text
            text=text.replace(/\<span[^>]+CheckDictation\-marked[^>]+\>([^<>]+)\<\/span\>/g, '$1')
        }
        return text
    }

    function dictationReplaceDialog(title, diatext, word, suggestions) {


        var defer = $.Deferred();
        $('#CheckDictation-form, #CheckDictation-form-input').remove();
        $('<div>', {
            html: title
        }).append('<br><br><br>' + diatext).append(
            '<br><br>',
            $('<input>', {
                id: 'CheckDictation-form-input',
                value: word,
                style: 'line-height: 2;'
            }),
            ' ',
            kurdishWikiTools && kurdishWikiTools.superTool ? $('<img>', {
                src: '//upload.wikimedia.org/wikipedia/fa/f/fc/Button_super_tool.png',
                alt: 'تووڵامراز',
                title: 'تووڵامراز'
            }).click(function retrySuperTool() {
                $('#CheckDictation-form-input').val((kurdishWikiTools.superTool(' ' + $('#CheckDictation-form-input').val() + ' ')).trim());
            }) : '<br><br>',
            $('<div>').append(suggestions.map(function(text) {
                return $('<button>', {
                    text: text.replace('|', '❙')
                }).css({
                    'margin-left': '8px'
                }).click(function() {
                    $('#CheckDictation-form-input').val(text);
                });
            })),
            '<br>دەستەواژەی زیاتر لە دەقەکەدا جێگۆڕ بکە:<br><br>',
            $('<input>', {
                id: 'CheckDictation-form-input1',
                value: '',
                style: 'line-height: 2;'
            }), ' > ',
            $('<input>', {
                id: 'CheckDictation-form-input2',
                value: '',
                style: 'line-height: 2;'
            })
        ).dialog({
            width: 800,
            title: 'بەئاشکرا دەستکاریی وشەیەکی نێو دەق بکە',
            buttons: [{
                text: 'بۆ پێڕستی وشە دروست و زۆر بەکاربراوەکان زیادی بکە',
                class: 'ui-button-red',
                click: function() {
                    if (confirm('ئەو وشانەی بە پێڕستەکە زیاد دەکرێن دەبێت دروست و زۆر بەکاربراو بن. ئەگەر وشەکە دروست و زۆر بەکاربراوە، ئەوا کرتە لە OK بکە. بەپێچەوانەوە، کرتە لە Cancel بکە.')) {
                        defer.reject();
                    }
                    $(this).dialog('close');
                }
            }, {
                text: 'تەنیا بۆ ئەم وتارە دروستە',
                class: 'ui-button-blue',
                click: function() {
                    var exceptionsPage = 'ویکیپیدیا:پشکنەری حونجە/دروستەکانی ' + mw.config.get('wgPageName').replace(/_/g, ' ');
                    var exceptionsUrl = mw.util.getUrl(exceptionsPage);
                    loadPage(exceptionsPage)
                    .then(function(exceptionsText) {
                        // Turn exceptions into an array
                        var exceptions = [];
                        if (exceptionsText !== undefined) {
                            var pattern = /(?:\* )([^\n\r]+)(?:[\r\n]|$)/g,
                                matches;
                            while (matches = pattern.exec(exceptionsText)) {
                                exceptions.push(matches[1]);
                            }
                        }
                        var newExceptions = exceptions;
                        newExceptions.push($('#CheckDictation-form-input').val());
                        newExceptions = '* ' + newExceptions.join('\n* ');
                        savePage(exceptionsPage, newExceptions, 'افزودن >' + $('#CheckDictation-form-input').val()).then(function() {
                            location.href = location.href;
                        });
                        $('#CheckDictation-form, #CheckDictation-form-input').remove();
                        $(this).dialog('close');
                    })
                }
            }, {
                text: 'اصلاح شود',
                class: 'ui-button-green',
                click: function() {
                    var box1 = $('#CheckDictation-form-input1', this).val();
                    var box2 = $('#CheckDictation-form-input2', this).val();
                    if (box1 === box2) {
                        box1 = '';
                        box2 = '';
                    }
                    defer.resolve($('#CheckDictation-form-input', this).val(), box1, box2);
                    $(this).dialog('close');
                }
            }],
            close: function() {
                $('#CheckDictation-form, #CheckDictation-form-input').remove();
            }
        }).parent().prop('id', 'CheckDictation-form');
        return defer;
    }

    var exceptionsPage = 'ویکیپیدیا:پشکنەری حونجە/دروستەکانی ' + mw.config.get('wgPageName').replace(/_/g, ' ');
    var exceptionsUrl = mw.util.getUrl(exceptionsPage);
    $.when(
        $.getJSON('//checkdictation-fa.toolforge.org/check/' + mw.config.get('wgPageName')),
        loadPage(exceptionsPage)
    ).then(
    	function(result, exceptionsText) {
	        result = result[0];
	        if (!Array.isArray(result.result)) {
	            console.error(result.error);
	            return;
	        }
	
	        var issues = result.result;
	        if (issues.length === 0) {
	            siteSub.append(' ', $('<span>', {
	                text: 'پشکنەری حونجە: ھیچ ھەڵەیەک نەدۆزرایەوە!',
	                class: 'CheckDictation-allgood-label',
	            }));
	            return;
	        }
	
	        // Turn exceptions into an array
	        var exceptions = [];
	        if (exceptionsText !== undefined) {
	            var pattern = /(?:\* )([^\n\r]+)(?:[\r\n]|$)/g,
	                matches;
	            while (matches = pattern.exec(exceptionsText)) {
	                exceptions.push(matches[1]);
	            }
	        }
	
	        if (exceptions.length > 0) {
	            var numberOfExceptions = exceptions.length.toLocaleString('ckb');
	            siteSub.append(' ', $('<span>', {
	                'class': 'CheckDictation-exceptions-label'
	            }).append(
	                'پشکنەری حونجە: ' + numberOfExceptions + ' وشە گومانیان لێ دەکرێت کە ھەڵە بن ',
	                $('<a>', {
	                    href: exceptionsUrl,
	                    text: 'قەدەغەکراو'
	                })
	            ));
	        }
	
	        // Remove those words that are in the exceptions list from the issues list
	        issues = issues.filter(function(issue) {
	            return exceptions.indexOf(issue.word) === -1;
	        });
	
	        if (issues.length === 0) {
	            return;
	        }
	
	        var availableTypes = {};
	        issues.forEach(function(x) {
	            availableTypes[x.type] = true;
	        });
	        var types = result.types;
	
	        var legend = Object.keys(types).filter(function(x) {
	            return availableTypes[x];
	        }).map(function(x) {
	            var type = types[x];
	            return [
	                $('<div>').css({
	                    width: '12px',
	                    height: '12px',
	                    display: 'inline-block',
	                    'background-color': type.color
	                }),
	                ' ',
	                $('<span>', {
	                    text: type.title
	                }).css({
	                    'font-size': '10pt'
	                })
	            ];
	        }).reduce(function(x, y) {
	            return x.concat('، ', y);
	        });
	
	        $('#CheckDictation-tool').remove();
	
	        // Fix interference with provit.js
	        var anchor1 = mw.config.get('wgAction') === 'edit' ?
	            '#firstHeading' :
	            'h1:first';
	        var numberOfIssues = issues.length.toLocaleString('ckb');
	        siteSub.append(' ', $('<span>', {
	            text: 'پشکنەری حونجە: ' + numberOfIssues + ' کێشەیەکی شێوازی پەڕە یان نووسراو دۆزرایەوە',
	            class: 'CheckDictation-issues-label'
	        }).click(function() {
	            $(this).hide();
	            $('#CheckDictation-tool').show();
	        }));
	        $(anchor1).after($('<div>', {
	            id: 'CheckDictation-tool'
	        }).hide().append(
	            $('<big>', {
	                text: numberOfIssues + ' حاڵەتی ھەڵەی نووسین یان شێوازی پەڕەی گومانلێکراو دۆزرایەوە!'
	            }).css('font-style', 'italic'),
	            ' (',
	            legend,
	            ')',
	            $('<div>', {
	                id: 'spellmarkedwords',
	                text: 'حاڵەتە گومانلێکراوەکانی ھەڵەکان: '
	            }),
	            '<br><br>',
	            $('<div>', {
	                id: 'CheckDictation-button-place'
	            }),
	            $('<div>', {
	                id: 'CheckDictation-button-place-after',
	            }).css('font-size', '10pt').append(
	                'ئەگەر کرتە لە دوگمەکەی سەرەوە بکەیت، ئەوا دەستەواژە دروستەکان لە ',
	                $('<a>', {
	                    href: exceptionsUrl,
	                    text: exceptionsPage
	                }),
	                ' پاشەکەوت دەبن.',
	                '<br>',
	                $('<div>', {
	                    id: 'CheckDictation-button-place-after-2',
	                    text: 'گەر بەنادروستی ئەم دوگمەیە بەکاربێنیت، ئەوا دەستپێگەیشتنی ئەم ئامرازەت لێ دەسەندرێتەوە. پێش کرتەکردن لەسەر دوگمەکە دڵنیا ببەوە کە تەواوی دەستەواژە پێشنیارکراوەکان دروستن. پێش کرتەکردن لەسەر دوگمەکە، دڵنیا ببەوە کە شائەمراز داگریت. زۆرینەی ھەڵەکانی وتارەکان لەڕێگەی شائەمرازەوە چارەسەر دەکرێن'
	                }).css({
	                    'font-size': '10pt',
	                    'font-weight': 'bold',
	                    'color': 'red'
	                }),
	                '<br>', [
	                    'تێبینیی ١: ھەندێک لەمانە تەنیا دەتواندرێت لە دۆخی دەستکاریکردندا ببیندرێن!',
	                    'تێبینیی ٢: بە کرتەکردن لەسەر وشەکانی سەرەوە، دەتوانیت خۆگەڕانە وشە دروستەکە جێگۆڕ بکەیت بەبێ چوونە سەر پەنجەرەی دەستکاریکردنەکە.',
	                    'تێبینیی ٣: دەتوانیت بە کرتەکردن چەند دەستەواژەیەکی ڕیزێک دیاری بکەیت، و بە کرتەکردن لەسەر ئەو دوگمەیەی کە دەردەکەوێت و بە یەک دەستکاری ھەموویان جێگۆڕ بکەیت'
	                ].map(function(x) {
	                    return $('<div>', {
	                        text: x
	                    }).css('font-size', '8pt');
	                })
	            )
	        ));
	        $('<button>', {
	            text: 'Disambiguation with another tool',
	            style: 'margin-right: 2em',
	            id: 'CheckDictation-Disambig'
	        }).click(function() {
	            var my_url = 'https://dispenser.info.tm/~dispenser/cgi-bin/dab_solver.py?lang=ckb&page=' + mw.config.get('wgPageName')
	            window.open(my_url, '_blank');
	        }).appendTo('#CheckDictation-button-place').append(
	            '<br><br>')
	        $('#CheckDictation-Disambig').hide();
	
	        if (mw.config.get('wgUserGroups').indexOf('sysop') > -1 || mw.config.get('wgUserGroups').indexOf('eliminator') > -1 || mw.config.get('wgUserGroups').indexOf('patroller') > -1) {
	            $('<button>', {
	                text: 'لە ژێرپەڕەدا پاشەکەوتی بکە: ھەموو دەستەواژەکانی ئەم وتارە دروستن!',
	                style: 'margin-right: 2em',
	                id: 'CheckDictation-close'
	            }).click(function() {
	                var newExceptions = exceptions;
	                var addToExceptions = issues.map(function(issue) {
	                    return issue.word;
	                });
	                addToExceptions.forEach(function(word) {
	                    newExceptions.push(word);
	                });
	                newExceptions = '* ' + newExceptions.join('\n* ');
	
	                savePage(exceptionsPage, newExceptions, 'زیادکردن >' + addToExceptions.join('، ')).then(function() {
	                    location.href = location.href;
	                });
	
	            }).appendTo('#CheckDictation-button-place');
	        } else {
	            $('#CheckDictation-button-place-after').hide();
	            $('#CheckDictation-button-place-after-2').hide();
	        }
	
	        var arabic_diacritics = "ًٌٍَُِّْٔ"
	        var main_regex = '\[؛؟\\s\\n\\r\\•●⚫⬤\\„\\”\\‚\\’\\‘\\“\\[\\]\\{\\}\\t\\<\\>\\.\\,\\"' + "\\'\\+\\!\\?\\-\\/\\»«،\\:\\|\\(\\)\٠١٢٣٤٥٦٧٨٩]";
	        var start_regex = '\(\^\|' + main_regex + '\)';
	        var end_regex = '\(\$\|[' + arabic_diacritics + ']*' + main_regex + '\)';
	        var articleBody = $('#bodyContent, #article')[0];
	        var replacements = [];
	        var markedItems = [];
	
	        issues.forEach(function(item, i) {
	            var word = item.word,
	                type = types[item.type],
	                suggestions = item.suggestions.sort(),
	                cleanedWord = item.cleaned_word,
	                regexp, preparedId;
	            var myhint = type.hint
	            if (type.autofix == 'D') {
	                $('#CheckDictation-Disambig').show();
	                myhint = suggestions.join(' | ')
	            }
	            if (i !== 0) {
	                markedItems.push('، ');
	            }
	            regexp = new RegExp(type.syntax ?
	                mw.util.escapeRegExp(word) :
	                (start_regex + '(' +
	                    word.substring(0, word.length - 1).replace(/[ء-يٓ-ٕپچژگکكڪﻙﻚیﻱﻲكﮑﮐﮏﮎﻜﻛﻚﻙىﻯيہەھﻰ-ﻴ]/g, '$&\[ً-ِّْٰٔ\]\*') + word.slice(-1) +
	                    ')' + end_regex),
	                'g'
	            );
	            preparedId = 'tool-' + encodeURI(word).replace(/%/g, '.');
	            if (type.autofix == 'D') {
	                markWord2(articleBody, regexp, word, myhint || suggestions[0], preparedId);
	                $("a.mw-disambig").css("background-color", "#fadbd8");
	                $("a.mw-disambig").css("color", "#999933");
	            } else {
	                markWord(articleBody, regexp, word, myhint || suggestions[0], type.color, preparedId);
	            }
	            var element = $('<span>', {
	                style: 'background-color: ' + type.color,
	                text: word,
	                title: myhint || suggestions[0]
	            });
	            element.css('cursor', 'pointer');
	            element.click(function() {
	                if (!type.autofix) {
	                    location.hash = preparedId;
	                    return;
	                }
	                getPageTextCache().then(function(content) {
	                    var nearWordsRegex = new RegExp('((?:\\S+\\s+){0,5}|\^)(?:[«\\[\\(\\)\\|\\-٠١٢٣٤٥٦٧٨٩0-9\\u200c\\]])?(' + word.substring(0, word.length - 1).replace(/[^ ]/g, '$&\[ً-ِّْٰٔ\]\*') + word.slice(-1) + ')(\\s*(?:\\S+\\s+){0,5}|\$)', 'g')
	                    var nearWordsList = [],
	                        found;
	                    while (found = nearWordsRegex.exec(content)) {
	                        var myfound = found[0]
	                        if (myfound[0] == '[') {
	                            myfound = '[' + myfound
	                        }
	                        if (type.autofix == 'D') {
	                            if (myfound.includes('[[' + word + ']]') || myfound.includes('[[' + word + '|')) {
	                                nearWordsList.push(myfound);
	                            } else {
	                                continue;
	                            }
	                        } else {
	                            nearWordsList.push(myfound);
	                        }
	                    }
	                    if (nearWordsList != null) {
	                        nearWordsList = '… ' + nearWordsList.join(' …<br>… ') + ' …'
	                        nearWordsList = nearWordsList.replace(/\t/g, '	')
	                        nearWordsList = nearWordsList.replace(/\n\n/g, '<br>')
	                        nearWordsList = nearWordsList.replace(/\n(\*|\#|\;|\=)/g, '<br>$1')
	                    } else {
	                        nearWordsList = ''
	                    }
	                    dictationReplaceDialog(
	                        ('لەم دەقە یان دەقانەی خوارەوە وشەی «' + word + '» لەگەڵ چیدا جێگۆڕ دەکرێت؟'),
	                        (nearWordsList.replace(new RegExp('(' + word.substring(0, word.length - 1).replace(/[^ ]/g, '$&\[ً-ِّْٰٔ\]\*') + word.slice(-1) + ')', 'g'), '<big><b> $1 </b></big>')),
	                        word,
	                        suggestions
	                    ).then(function(toWord, toWord1, toWord2) {
	                        // Hide mark as valid button and comments, it confuses the users
	                        $('#CheckDictation-close, #CheckDictation-button-place-after').hide();
	
	                        element.css('background-color', '').css('color', 'grey').text(word + '⟸' + toWord).off();
	                        toWord = toWord.replace(/\200c /g, ' ').replace(/ \200c/g, ' ');
	                        if (mw.config.get('wgAction') === 'edit') {
	                            $('#wpTextbox1').val($('#wpTextbox1').val().replace(regexp, '$1' + toWord + '$3'));
	                            $('#wpTextbox1').val($('#wpTextbox1').val().replace(toWord1, toWord2));
	                        } else {
	                            if (toWord !== word) {
	                                if (type.autofix == 'D') {
	                                    regexp = new RegExp('(\\[\\[) \*(' + word + ') \*(\\]\\]|\\|)', 'g')
	                                }
	                                replacements.push([regexp, '$1' + toWord + '$3', word + '⟸' + toWord]);
	                            }
	                            if (toWord1 !== toWord2) {
	                                replacements.push([toWord1, toWord2, toWord1 + '⟸' + toWord2]);
	                            }
	                            $('#CheckDictation-apply').show();
	                        }
	                    }, function() {
	                        if (/[كڪﻙﻚيىےۍېہەھﭖﭗﭘﭙﭺﭻﭼﭽﮊﮋﮎﮏﮐﮑﻙﻚﻛﻜﮒﮓﮔﮕﮤﮥﯼﯽﯾﯿﻯﻰﻱﻲﻳﻴﺁﺂﺄﺃﺅﺆﺇﺈﺉﺊﺋﺌﺎﺏﺐﺑﺒﺕﺖﺗﺘﺙﺚﺛﺜﺝﺞﺟﺠﺡﺢﺣﺤﺥﺦﺧﺨﺩﺪﺫﺬﺭﺮﺯﺰﺱﺲﺳﺴﺵﺶﺷﺸﺹﺺﺻﺼﺽﺾﺿﻀﻁﻂﻃﻄﻅﻆﻇﻈﻉﻊﻋﻌﻍﻎﻏﻐﻑﻒﻓﻔﻕﻖﻗﻘﻝﻞﻟﻠﻡﻢﻣﻤﻥﻦﻧﻨﻩﻪﻫﻬﻫﻭﻮﻰﻲﻶﻸﻺﻼ]/.exec(word)) {
	                            alert('وشە پێشنیاکراوەکە نووسەی ناکوردی یان نادروستی تێدایە. پێش ھەواڵدان لە وشەکە، تکایە شائەمراز لەسەر پەڕەکە داگرە.');
	                            return;
	                        }
	                        if (/((.*\u200c)$|^(\u200c.*))/.exec(word)) {
	                            alert('لەکۆتایی یان دەستپێکی وشە پێشنیارکراوەکە، بۆشاییی زیادە ھەیە. تکایە بە یارمەتیی ئامرازی جێگۆڕین یان شائەمراز لایبە.');
	                            return;
	                        }
	                        $('CheckDictation-marked-' + word.replace(/ /g, '_')).css('background-color', '');
	                        element.css('background-color', '').css('color', 'grey').off();
	                        loadPage('ویکیپیدیا:پشکنەری حونجە/پێڕستی دەستەواژە دروستەکان').then(function(text) {
	                            if ((text + '\n').indexOf('* ' + (cleanedWord || word) + '\n') !== -1) {
	                                return;
	                            }
	                            return savePage('ویکیپیدیا:پشکنەری حونجە/پێڕستی دەستەواژە دروستەکان', text + '\n* ' + (cleanedWord || word), 'وشەی دروست و زۆر بەکاربراوی «' + (cleanedWord || word) + '» بە پێڕستی وشەکانی [[وپ:پشکنەری حونجە|پشکنەری حونجە]] زیاد بکە. لە وتاری [[' + mw.config.get('wgPageName') + ']] ھەن');
	                        }).then(function() {
	                            mw.notify('وشەی «' + word + '» بە پێڕستی وشە دروست و زۆر بەکاربراوەکان زیاد کرا.');
	                        });
	                        $.get('//checkdictation-fa.toolforge.org/check/Botupdate').then(function() {}, function() {});
	                    });
	                });
	            });
	            markedItems.push(element[0]);
	
	        });
	
	        $('#spellmarkedwords').append(markedItems);
	
	        // reset page location to intended hash linked place
	        var hash = location.hash;
	        if (hash) {
	            location.hash = '';
	            location.hash = hash;
	        }
	
	        $('<button>', {
	            text: 'ئەو دەستەواژانەی ھەڵت بژاردن جێگۆڕیان پێ بکە!',
	            style: 'margin-right: 2em',
	            id: 'CheckDictation-apply'
	        }).click(function() {
	            $(this).prop('disabled', 'disabled').text('وەرگرتن و جێگۆڕکردنی پەڕەکە...');
	            loadPage(mw.config.get('wgPageName')).then(function(text) {
	                replacements.forEach(function(x) {
	                    text = text.replace(x[0], x[1]);
	                    //solving bug: [[:fa:Special:Permalink/24532423#اشتباه‌یاب، ایجاد پیوند درونی با دو آرگومان.مان]]
	                    if (x[1].indexOf("|")!==-1){
	                        var wrong_replacment=x[1].split('|')[1].replace(/\[/g, '').replace(/\$[13]/g, '');
	                        text = text.replace('|'+wrong_replacment+'|', '|');
	                    }
	                });
	                return savePage(
	                    mw.config.get('wgPageName'),
	                    text,
	                    'جێگۆڕکێکردن بە [[ویکیپیدیا:پشکنەری حونجە|پشکنەری حونجە]]: ' + replacements.map(function(x) {
	                        return x[2];
	                    }).join('، ')
	                );
	            }).then(function() {
	                mw.notify('کرا. تکایە بیپشکنە.');
	                location.href = mw.util.getUrl(mw.config.get('wgPageName'), {
	                    diff: 'last'
	                });
	            });
	        }).hide().appendTo('#spellmarkedwords');
	    },
	    // if the AJAX call to the webservice failed, show a nice notification to the user
	    function(){
	    	siteSub.append(' ', $('<span>', {
                'class': 'CheckDictation-exceptions-label'
            }).append(
                'پشکنەری حونجە: دابینکەری خزمەتگوزارییەکە بەردەست نییە؛ تکایە گەر ئەم کێشەیە بەردەوام بوو، ئەوا لە وپ:دیوەخان ھەواڵ بدە. '
            ));
	    }
    );
    // Edit after saving to resolve conflicts with visual editor
    //If the visual editor is enabled for the user
    // In the article the incorrect label is placed by the tool
    mw.hook('ve.saveDialog.stateChanged').add(function() {
        mw.hook('ve.deactivationComplete').add(function() {
            loadPage(mw.config.get('wgPageName')).then(function(text) {
                    return savePage(
                        mw.config.get('wgPageName'),
                        text = postEdit(text),
                        'ویرایش خودکار: اصلاح برچسب‌های نادرست'
                    );
            });
        });
    });
});