User:DannyS712/communicator.js - Wikipedia


Article Images

Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump.
This code will be executed when previewing this page.

Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.

//Copied from [[User:FR30799386/communicator.js]]
//<nowiki>
$.when(mw.loader.using(['mediawiki.util']), $.ready).then(function() {
    if (mw.config.get('wgNamespaceNumber') < 0) //don't load when on special pages
    {
        return;
    }
    var music = function(clickEvent) {
        if (clickEvent) {
            clickEvent.preventDefault();
        }
        if (window.Morebits === null) {
            importScript('MediaWiki:Gadget-morebits.js');
        }
        var page = new Morebits.wiki.page(mw.config.get('wgPageName'));
        var check = page.lookupCreator(function(page) {
            var guy = page.getCreator();
            var preload;
            if (mw.storage.get('postCreator') !== null) {
                preload = mw.storage.get('postCreator');
            } else {
                preload = '==[[' + mw.config.get('wgPageName') + ']]==\nHello, I\'m ' + mw.config.get('wgUserName') + '. I noticed that a page you started [[' + mw.config.get('wgPageName') + ']] is ....~~~~';
            }
            $('#firstHeading').next().before('<div class="comment-ui" style="padding: 1em; background:#f2f2f2;' +
                'max-width: 1200px; width: 100%; margin-top: 0.5em; font-size:100%;"><label for="comment-main-dialog-label">Comment: </label>' + '<textarea id="comment-dialog-field" class="mw-ui-input" style="font-family:sans-serif;" rows="5">' + preload + '</textarea>' +
                '<br/><label for="comment-dailog-summary-label" style="padding-top:1em;">Edit Summary: </label>' +
                '<input id="comment-dialog-summary" class="mw-ui-input" style="width:100%; max-width:1200px;"' +
                'value="Suggesting improvements to ' + guy + '"/>&nbsp;' +
                '<table style="border-collapse:collapse"><tr><td id="comment-link-buttons" style="width: 255;px">' +
                '<button id="comment-dialog-button" class="mw-ui-button mw-ui-progressive">Comment</button>&nbsp;&nbsp;' +
                '<button id="comment-link-preview-button" class="mw-ui-button">Preview</button>&nbsp;&nbsp;<button id="comment-link-cancel-button" class="mw-ui-button mw-ui-quiet mw-ui-destructive">Cancel</button></td>' +
                '</tr></table><div id="comment-dailog-status" style="padding:0.5em"></div>' +
                '<div id="comment-link-options" class="I-empty" style="margin-top: 0.5em"></div>' +
                '<div id="comment-link-preview" class="I-empty" style="border: thin dashed gray; padding: 0.5em; margin-top: 0.5em; background:white;"></div></div>');
            mw.util.addCSS(".I-empty:empty { display: none; }");
            // Event listener for the text area
            document.getElementById("comment-dialog-field")
                .addEventListener("input", function() {

                    // If the user has started a comment, ask for a
                    // confirmation before closing
                    if (this.value && !window.onbeforeunload) {
                        window.onbeforeunload = function(e) {
                            var txt = "You've started a comment but haven't posted it";
                            e.returnValue = txt;
                            return txt;
                        };
                    } else if (!this.value && window.onbeforeunload) {
                        window.onbeforeunload = null;
                    }
                }); // End event listener for the text area

            // Event listener for the "comment" button
            document.getElementById("comment-dialog-button")
                .addEventListener("click", function() {
                    var sanitizedCode = document.getElementById("comment-dialog-field").value
                        .replace(/&/g, "%26");
                     document.getElementById("comment-dialog-button").disabled = true;
                    document.getElementById('comment-dialog-summary').disabled = true;
                    document.getElementById('comment-dialog-field').disabled = true;
                    var text = sanitizedCode;
                    mw.storage.set('postCreator', text);
                    var guyedit = new Morebits.wiki.page(('User talk:' + guy));
                    guyedit.setEditSummary(document.getElementById("comment-dialog-summary").value
                        .replace(/&/g, "%26") + ' ([[User:FR30799386/communicator|communicator]])');
                    guyedit.setAppendText('\n' + text);
                    guyedit.append(function() {
                        $('#comment-dailog-status').html('Done...Reloading page....');
                        window.onbeforeunload = null;
                        document.location.reload();
                    }, function() {
                        $('#comment-dialog-status').html('There was an error in publishing your comment. Please report this to <a href"//en.wikipedia.org/wiki/User_talk:FR30799386">the maintainer\'s talk page</a>. If possible please add the content of the console log to your report');
                        document.getElementById("comment-dialog-button").disabled = false;
                    });
                }); // End event listener for the "comment" button

            // Event listener for the "Preview" button
            document.getElementById("comment-link-preview-button")
                .addEventListener("click", function() {
                    var sanitizedCode = document.getElementById("comment-dialog-field").value
                        .replace(/&/g, "%26");
                         mw.storage.set('postCreator', sanitizedCode);
                    $.post("https://en.wikipedia.org/api/rest_v1/transform/wikitext/to/html",
                        "wikitext=" + sanitizedCode + "&body_only=true",
                        function(html) {
                            document.getElementById("comment-link-preview").innerHTML = html;
                        });
                });
            document.getElementById("comment-link-cancel-button")
                .addEventListener("click", function() {
                    $('.comment-ui').remove();
                    window.onbeforeunload = null;
                });
        });
    };
    mw.util.addPortletLink('p-cactions', '#', 'Communicate with creator', 'ca-communicate', 'Communicate with creator of article', '6');
    $('#ca-communicate').click(music);
});
//</nowiki>