பயனர்:Maathavan/CustomizedEditTools.js

விக்கிமூலம் இல் இருந்து
Jump to navigation Jump to search

குறிப்பு - சேமித்த பின்னர், நீங்கள் செய்த மாற்றங்களைக் காண்பதற்கு உங்கள் உலவியின் இடைமாற்று அகற்றப்பட வேண்டும்.

  • மொஸில்லா பயர்பாக்ஸ் / சபாரி: Shift+Reload, அல்லது Ctrl-F5 அல்லது Ctrl-R (⌘-R Mac ல்)
  • கூகிள் குரோம் Ctrl-Shift-R அழுத்தவும். (⌘-Shift-R Mac ல்) ;
  • இண்டர்நெட் எக்ஸ்ப்ளோரர்: Ctrl-Refresh அல்லது Ctrl-F5 ஐ அழுத்தவும்.
  • ஒபேரா: Tools → Preferences இல் இடைமாற்றை அகற்றவும்;
importStylesheet('User:Maathavan/CustomizedEditTools.css');

var replaceSampleData = [['Replace1', 'ReplaceWith1'], ['Replace2', 'ReplaceWith2']];
var replaceData = JSON.parse(localStorage.getItem('replaceData')) || replaceSampleData;

var customSampleData = [['{{', '}}'], ['[[', ']]'], ['"', '"'], ['Example', ''], ['(', ')']];
var customData = JSON.parse(localStorage.getItem("customData")) || customSampleData;

//Icons
var icon1 = "//upload.wikimedia.org/wikipedia/commons/thumb/c/c7/Delhi_Metro_Line_1.svg/21px-Delhi_Metro_Line_1.svg.png";
var icon2 = "//upload.wikimedia.org/wikipedia/commons/thumb/2/26/Paris_m_2_jms.svg/21px-Paris_m_2_jms.svg.png";
var icon3 = "//upload.wikimedia.org/wikipedia/commons/thumb/d/db/Icon_3_red.svg/21px-Icon_3_red.svg.png";
var icon4 = "//upload.wikimedia.org/wikipedia/commons/thumb/8/87/BKV_m_4_jms.svg/21px-BKV_m_4_jms.svg.png";
var icon5 = "//upload.wikimedia.org/wikipedia/commons/thumb/5/57/Channel_5_logo_2011.svg/21px-Channel_5_logo_2011.svg.png";

var customizeToolbar = function () {
    $('#wpTextbox1').wikiEditor('addToToolbar', {
        'section': 'main',
        'group': 'insert',
        'tools': {
            'CETReplaceSingleQuotes': {
                label: 'Replace All!',
                type: 'button',
                icon: 'https://upload.wikimedia.org/wikipedia/commons/thumb/0/0d/Cquote1_single_black.svg/10px-Cquote1_single_black.svg.png',
                action: {
                    type: "callback",
                    execute: function () {
                        var selectedText = window.getSelection().toString();
                        var replaceText;
                        replaceText = selectedText.indexOf("'") === 0 || selectedText.indexOf('"') === 0 ? '‘' + selectedText.substring(1) : '‘' + selectedText;
                        replaceText = (/.$/).exec(replaceText) == "'" || (/.$/).exec(replaceText) == '"' ? replaceText.replace(/.$/, '’') : replaceText + '’';
                        $('#wpTextbox1').val($('#wpTextbox1').val().replace(selectedText, replaceText));
                    }
                }
            },
            'CETReplaceDoubleQuotes': {
                label: 'Replace All!',
                type: 'button',
                icon: 'https://upload.wikimedia.org/wikipedia/commons/thumb/c/c0/OOjs_UI_icon_quotes-ltr.svg/21px-OOjs_UI_icon_quotes-ltr.svg.png',
                action: {
                    type: "callback",
                    execute: function () {
                        var selectedText = window.getSelection().toString();
                        replaceText = selectedText.indexOf("'") === 0 || selectedText.indexOf('"') === 0 ? '“' + selectedText.substring(1) : '“' + selectedText;
                        replaceText = (/.$/).exec(replaceText) == "'" || (/.$/).exec(replaceText) == '"' ? replaceText.replace(/.$/, '”') : replaceText + '”';
                        $('#wpTextbox1').val($('#wpTextbox1').val().replace(selectedText, replaceText));
                    }
                }
            }
        }
    });
    $('#wpTextbox1').wikiEditor('addToToolbar', {
        'sections': {
            'CET': {
                'type': 'toolbar',
                'label': 'CET',
                'groups': {
                    'Replace': {
                        'tools': {
                            'CETReplace': {
                                label: 'Replace All!',
                                type: 'button',
                                icon: '//upload.wikimedia.org/wikipedia/commons/thumb/4/4c/Correct.svg/21px-Correct.svg.png',
                                action: {
                                    type: "callback",
                                    execute: function () {
                                        for (var i = 0; i < replaceData.length; i++) {
                                            $('#wpTextbox1').val($('#wpTextbox1').val().replace(new RegExp(replaceData[i][0], 'g'), replaceData[i][1]));
                                        }
                                    }
                                }
                            },
                            'CETReplaceQuotes': {
                                label: 'Replace All!',
                                type: 'button',
                                icon: 'https://upload.wikimedia.org/wikipedia/commons/thumb/3/3e/Dollar_sign_in_circle.svg/21px-Dollar_sign_in_circle.svg.png',
                                action: {
                                    type: "callback",
                                    execute: function () {
                                        var doubleQuoteTexts = $('#wpTextbox1').val().match(/\$.*?\$/g);
                                        var doubleQuoteTextToReplace;
                                        for (var i = 0; i < doubleQuoteTexts.length; i++) {
                                            doubleQuoteTextToReplace = doubleQuoteTexts[i].replace(/[\$]/g, '');
                                            doubleQuoteTextToReplace = doubleQuoteTextToReplace.indexOf("'") === 0 || doubleQuoteTextToReplace.indexOf('"') === 0 ? '“' + doubleQuoteTextToReplace.substring(1) : '“' + doubleQuoteTextToReplace;
                                            doubleQuoteTextToReplace = (/.$/).exec(doubleQuoteTextToReplace) == "'" || (/.$/).exec(doubleQuoteTextToReplace) == '"' ? doubleQuoteTextToReplace.replace(/.$/, '”') : doubleQuoteTextToReplace + '”';
                                            $('#wpTextbox1').val($('#wpTextbox1').val().replace(doubleQuoteTexts[i], doubleQuoteTextToReplace));
                                        }
                                        var singleQuoteTexts = $('#wpTextbox1').val().match(/\#.*?\#/g);
                                        var singleQuoteTextToReplace;
                                        for (var i = 0; i < singleQuoteTexts.length; i++) {
                                            singleQuoteTextToReplace = singleQuoteTexts[i].replace(/[\#]/g, '');
                                            singleQuoteTextToReplace = singleQuoteTextToReplace.indexOf("'") === 0 || singleQuoteTextToReplace.indexOf('"') === 0 ? '‘' + singleQuoteTextToReplace.substring(1) : '‘' + singleQuoteTextToReplace;
                                            singleQuoteTextToReplace = (/.$/).exec(singleQuoteTextToReplace) == "'" || (/.$/).exec(singleQuoteTextToReplace) == '"' ? singleQuoteTextToReplace.replace(/.$/, '’') : singleQuoteTextToReplace + '’';
                                            $('#wpTextbox1').val($('#wpTextbox1').val().replace(singleQuoteTexts[i], singleQuoteTextToReplace));
                                        }
                                    }
                                }
                            },
                            'CETReplaceCustomize': {
                                label: 'Customize Replace!',
                                type: 'button',
                                icon: 'https://upload.wikimedia.org/wikipedia/commons/thumb/8/82/Configure.svg/21px-Configure.svg.png',
                                action: {
                                    type: "callback",
                                    execute: function () {
                                        $('#CustEditModal, #cet-save-replace, #cet-add').show();
                                        $('#cet-save-btns').hide();
                                        var inputs = '';
                                        for (var i = 0; i < replaceData.length; i++) {
                                            inputs += '<label class="cet-label">' + (i + 1) + ': </label><input type="text" class="cet-input" id="r' + i + '" value="' + replaceData[i][0] + '"> <label> with: </label><input type="text" class="cet-input" id="rw' + i + '" value="' + replaceData[i][1] + '"></br>';
                                        }
                                        $('.cet-modal-body').html(inputs);
                                        $('#cet-save-replace').click(function () {
                                            var replaceDataToSave = [];
                                            var l = $('.cet-modal-body .cet-label').length;
                                            for (var i = 0; i < l; i++) {
                                                replaceDataToSave.push([$('#r' + i).val().replace(/"/g, "&quot;"), $('#rw' + i).val().replace(/"/g, "&quot;")]);
                                            }
                                            localStorage.setItem('replaceData', JSON.stringify(replaceDataToSave));
                                            replaceData = replaceDataToSave;
                                            saveToCustomReplace(replaceDataToSave);
                                            $('#CustEditModal').hide();
                                        });
                                        $('#cet-add').unbind().click(function () {
                                            var i = $('.cet-modal-body .cet-label').length;
                                            $('.cet-modal-body').append('<label class="cet-label">' + (i + 1) + ': </label><input type="text" class="cet-input" id="r' + i + '" value=""> <label> with: </label><input type="text" class="cet-input" id="rw' + i + '" value=""></br>');
                                        });
                                    }
                                }
                            },
                        }
                    },
                    'CustomButtons': {
                        'tools': {
                            'button1': {
                                label: customData[0][0] + 'உரை' + customData[0][1] + ' உள்ளிடுக',
                                type: 'button',
                                icon: icon1,
                                action: {
                                    type: 'encapsulate',
                                    options: {
                                        pre: customData[0][0],
                                        peri: '',
                                        post: customData[0][1]
                                    }
                                }
                            },
                            'button2': {
                                label: customData[1][0] + 'உரை' + customData[1][1] + ' உள்ளிடுக',
                                type: 'button',
                                icon: icon2,
                                action: {
                                    type: 'encapsulate',
                                    options: {
                                        pre: customData[1][0],
                                        peri: '',
                                        post: customData[1][1]
                                    }
                                }
                            },
                            'button3': {
                                label: customData[2][0] + 'உரை' + customData[2][1] + ' உள்ளிடுக',
                                type: 'button',
                                icon: icon3,
                                action: {
                                    type: 'encapsulate',
                                    options: {
                                        pre: customData[2][0],
                                        peri: '',
                                        post: customData[2][1]
                                    }
                                }
                            },
                            'button4': {
                                label: customData[3][0] + 'உரை' + customData[3][1] + ' உள்ளிடுக',
                                type: 'button',
                                icon: icon4,
                                action: {
                                    type: 'encapsulate',
                                    options: {
                                        pre: customData[3][0],
                                        peri: '',
                                        post: customData[3][1]
                                    }
                                }
                            },
                            'button5': {
                                label: customData[4][0] + 'உரை' + customData[4][1] + ' உள்ளிடுக',
                                type: 'button',
                                icon: icon5,
                                action: {
                                    type: 'encapsulate',
                                    options: {
                                        pre: customData[4][0],
                                        peri: '',
                                        post: customData[4][1]
                                    }
                                }
                            },
                            'CETCustomizeButtons': {
                                label: 'Customize Buttons!',
                                type: 'button',
                                icon: '//upload.wikimedia.org/wikipedia/commons/thumb/6/61/Crystal128-configure.svg/21px-Crystal128-configure.svg.png',
                                action: {
                                    type: "callback",
                                    execute: function () {
                                        $('#CustEditModal, #cet-save-btns').show();
                                        $('#cet-add, #cet-save-replace').hide();
                                        var inputs = '';
                                        for (var i = 0; i < customData.length; i++) {
                                            inputs += '<label class="cet-label">' + (i + 1) + ': </label><input type="text" class="cet-input" id="cs' + i + '" value="' + customData[i][0] + '"> <label> with: </label><input type="text" class="cet-input" id="ce' + i + '" value="' + customData[i][1] + '"></br>';
                                        }
                                        $('.cet-modal-body').html(inputs);

                                        $('#cet-save-btns').click(function () {
                                            var customDataToSave = [];
                                            var l = $('.cet-modal-body .cet-label').length;
                                            for (var i = 0; i < l; i++) {
                                                customDataToSave.push([$('#cs' + i).val().replace(/"/g, "&quot;"), $('#ce' + i).val().replace(/"/g, "&quot;")]);
                                            }
                                            localStorage.setItem('customData', JSON.stringify(customDataToSave));
                                            customData = customDataToSave;
                                            location.reload();
                                        });

                                    }
                                }
                            },
                        }
                    }
                }
            }
        }
    });
};

if (['edit', 'submit'].indexOf(mw.config.get('wgAction')) !== -1) {
    mw.loader.using('user.options').then(function () {
        // This can be the string "0" if the user disabled the preference ([[phab:T54542#555387]])
        if (mw.user.options.get('usebetatoolbar') == 1) {
            $.when(
                mw.loader.using('ext.wikiEditor'), $.ready
            ).then(customizeToolbar);
        }
    });
}

$('body').append('<div id="CustEditModal" class="cet-modal" style="display: none;"><div class="cet-modal-content"><div class="cet-modal-header"><span class="cet-close">&times;</span><h2>Customize Replace</h2></div><div class="cet-modal-body"></div><div class="cet-modal-footer"><div id="cet-btn-set"><button id="cet-save-replace" class="cet-btn">Save</button><button id="cet-save-btns" class="cet-btn">Save</button><button id="cet-add" class="cet-btn">Add</button></div></div></div></div>');

$(".cet-close").click(function () {
    $('#CustEditModal').hide();
});

// ***
var api = new mw.Api();
function saveToCustomReplace(data) {
    var text = '';
    $.each(data, function (i, val) {
        text += val[0] + '~' + val[1] + '\n';
    });
    api.postWithToken("edit", {
        action: "edit",
        title: 'User:' + mw.config.get("wgUserName") + '/CustomReplace',
        summary: 'Added Custom Replaces through CustomEditTools',
        text: text
    }).done(function (result, jqXHR) {
        mw.log("Saved successfully");
    });
}
function replaceContent(content) {
    api.postWithToken("edit", {
        action: "edit",
        title: mw.config.get("wgPageName"),
        summary: 'Replacing through CustomEditTools',
        text: content
    }).done(function (result, jqXHR) {
        mw.log("Saved successfully");
        location.reload();
    });
}

if (mw.config.get("wgNamespaceNumber") !== -1) {
    $(".mw-indicators").append('<div class="mw-indicator"><a class="CETLinkDisable" id="CETReplaceQuotesBtn" href="#" title="Replace Quotes!"><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/5/5b/OOjs_UI_icon_quotesAdd-ltr.svg/24px-OOjs_UI_icon_quotesAdd-ltr.svg.png" decoding="async" width="21" height="21"></a></div>');
    $(".mw-indicators").append('<div class="mw-indicator"><a class="CETLinkDisable" id="CETReplaceItBtn" href="#" title="Replace All Customs!"><img src="//upload.wikimedia.org/wikipedia/commons/thumb/4/4c/Correct.svg/21px-Correct.svg.png" decoding="async" width="21" height="21"></a></div>');
    $(".mw-indicators").append('<div class="mw-indicator"><a class="CETLinkDisable" id="CETReplaceCustomizeBtn" href="#" title="Customize Replace!"><img src="https://upload.wikimedia.org/wikipedia/commons/thumb/8/82/Configure.svg/21px-Configure.svg.png" decoding="async" width="21" height="21"></a></div>');
    $(".mw-indicators").append('<div class="mw-indicator"><a class="CETLinkDisable" id="CETReplaceRefreshBtn" href="#" title="Refresh to Replace!"><img src="//upload.wikimedia.org/wikipedia/commons/thumb/9/96/View-refresh_Gion.svg/48px-View-refresh_Gion.svg.png" decoding="async" width="21" height="21"></a></div>');

    $('.CETLinkDisable').click(function (e) {
        e.preventDefault();
    });

    var wikiPageContent;
    var replacePgData;
    $.ajax({
        url: mw.config.get("wgScriptPath") + '/api.php?',
        data: 'action=query&format=json&prop=revisions&indexpageids=1&titles=' + encodeURIComponent('User:' + mw.config.get("wgUserName") + '/CustomReplace') + '&utf8=1&rvprop=content',
        dataType: 'json',
        success: function (data) {
            var pageid = data.query.pageids[0];
            var t = '*';
            replacePgData = data.query.pages[pageid].revisions[0][t];
        }
    });

    $.ajax({
        url: mw.config.get("wgScriptPath") + '/api.php?',
        data: 'action=query&format=json&prop=revisions&indexpageids=1&titles=' + encodeURIComponent(mw.config.get("wgPageName")) + '&utf8=1&rvprop=content',
        dataType: 'json',
        success: function (data) {
            var pageid = data.query.pageids[0];
            var t = '*';
            wikiPageContent = data.query.pages[pageid].revisions[0][t];
        }
    });

    $('#CETReplaceQuotesBtn').click(function () {
        var replacedContent = wikiPageContent;
        var doubleQuoteTexts = wikiPageContent.match(/\"(.*?)\"/g);
        //var neededDoubleQuotes = wikiPageContent.match(/(?<=\=)\"(.*?)\"/g); Lookbehind only support chrome.
        var neededDoubleQuotes = wikiPageContent.match(/\=\"(.*?)\"/g);
        if (neededDoubleQuotes) {
            for (var i = 0; i < neededDoubleQuotes.length; i++) {
                neededDoubleQuotes[i] = neededDoubleQuotes[i].replace('=', '');
            }
            doubleQuoteTexts = doubleQuoteTexts.filter((i) => !neededDoubleQuotes.includes(i));
        }
        var doubleQuoteTextToReplace;
        if (doubleQuoteTexts) {
            for (var i = 0; i < doubleQuoteTexts.length; i++) {
                doubleQuoteTextToReplace = '“' + doubleQuoteTexts[i].replace(/[\"]/g, '') + '”';
                replacedContent = replacedContent.replace(doubleQuoteTexts[i], doubleQuoteTextToReplace);
            }
        }
        var singleQuoteTexts = replacedContent.match(/'(.*?)'/g);
        var neededSingleQuotes = wikiPageContent.match(/''(.*?)''/g);
        if (neededSingleQuotes) {
            for (var i = 0; i < neededSingleQuotes.length; i++) {
                neededSingleQuotes[i] = neededSingleQuotes[i].replace(/'{2,}/g, "'");
            }
            neededSingleQuotes.push("''");
            singleQuoteTexts = singleQuoteTexts.filter((i) => !neededSingleQuotes.includes(i));
        }
        var singleQuoteTextToReplace;
        if (singleQuoteTexts) {
            for (var i = 0; i < singleQuoteTexts.length; i++) {
                singleQuoteTextToReplace = '‘' + singleQuoteTexts[i].replace(/[\']/g, '') + '’';
                replacedContent = replacedContent.replace(singleQuoteTexts[i], singleQuoteTextToReplace);
            }
        }
        replaceContent(replacedContent);
    });

    $('#CETReplaceItBtn').click(function () {
        var replacedContent = wikiPageContent;
        for (var i = 0; i < replaceData.length; i++) {
            replacedContent = replacedContent.replace(new RegExp(replaceData[i][0], 'g'), replaceData[i][1]);
        }
        replaceContent(replacedContent);
    });
    $('#CETReplaceCustomizeBtn').click(function () {
        $('#CustEditModal, #cet-save-replace, #cet-add').show();
        $('#cet-save-btns').hide();
        var inputs = '';
        for (var i = 0; i < replaceData.length; i++) {
            inputs += '<label class="cet-label">' + (i + 1) + ': </label><input type="text" class="cet-input" id="r' + i + '" value="' + replaceData[i][0] + '"> <label> with: </label><input type="text" class="cet-input" id="rw' + i + '" value="' + replaceData[i][1] + '"></br>';
        }
        $('.cet-modal-body').html(inputs);
        $('#cet-save-replace').click(function () {
            var replaceDataToSave = [];
            var l = $('.cet-modal-body .cet-label').length;
            for (var i = 0; i < l; i++) {
                replaceDataToSave.push([$('#r' + i).val().replace(/"/g, "&quot;"), $('#rw' + i).val().replace(/"/g, "&quot;")]);
            }
            localStorage.setItem('replaceData', JSON.stringify(replaceDataToSave));
            replaceData = replaceDataToSave;
            saveToCustomReplace(replaceDataToSave);
            $('#CustEditModal').hide();
        });
        $('#cet-add').unbind().click(function () {
            var i = $('.cet-modal-body .cet-label').length;
            $('.cet-modal-body').append('<label class="cet-label">' + (i + 1) + ': </label><input type="text" class="cet-input" id="r' + i + '" value=""> <label> with: </label><input type="text" class="cet-input" id="rw' + i + '" value=""></br>');
        });
    });
    $('#CETReplaceRefreshBtn').click(function () {
        var replacePgDataToSplit = replacePgData.split('\n');
        var replacePgDataToSave = [];
        $.each(replacePgDataToSplit, function (i, val) {
            replacePgDataToSave.push([val.split('~')[0].replace(/"/g, "&quot;"), val.split('~')[1].replace(/"/g, "&quot;")]);
        });
        localStorage.setItem('replaceData', JSON.stringify(replacePgDataToSave));
        replaceData = replacePgDataToSave;
    });
}
"https://ta.wikisource.org/w/index.php?title=பயனர்:Maathavan/CustomizedEditTools.js&oldid=1118809" இருந்து மீள்விக்கப்பட்டது