User:Psl631/common.js: Difference between revisions

From TestWiki
Content added Content deleted
(Created page with "// jshint jquery:true, esversion:5 →‎globals require, module, mediaWiki, mw, OO: 'use strict'; $( function() { $( '.mw-rollback-link > a' ).after( $( '<span>' ).addClass(...")
 
No edit summary
Line 1: Line 1:
importScript('User:Psl631/block.js');
// jshint jquery:true, esversion:5
/* globals require, module, mediaWiki, mw, OO */
'use strict';


/**
$( function() {
* This script lets you use a custom summary with the rollback feature
$( '.mw-rollback-link > a' ).after( $( '<span>' ).addClass( 'edit-rollback' ).prop( 'title', 'Edit rollback summary' ) );
* by adding a "sum" link wherever a rollback link appears. When you
$( '#mw-content-text' ).on( 'click', '.edit-rollback', function() {
* click "sum" you are prompted for a custom summary, and the script
var $rollback = $( '#rollback-summary' );
* adds an additional summary parameter to the rollback URL submitting it.
*
if ( $( this ).parent().is( $rollback.parent() ) ) {
* For usage and extra options, see the talk page.
$rollback.toggle();
*/
} else {

$rollback.remove();
function addSumLink() {
var rbnode = [], diffnode, index = {}, gebcn = document.getElementsByClassName
var name = decodeURIComponent( $( this ).prev().prop( 'href' ).match( /&from=(.+)&token/ )[1].replace( /\+/g, ' ' ) );
? function(a, b, c) { return a.getElementsByClassName(c) }
$rollback = $( '<div id="rollback-summary">' ).append(
: getElementsByClassName;
$( '<input type="text">' ).addClass( 'mw-ui-input rollback-text' ).prop( { maxlength: 250, spellcheck: true } ).val(
if (typeof rollbackLinksDisable == 'object' && rollbackLinksDisable instanceof Array)
'Revert consecutive edits by [[Special:Contribs/' + name + '|' + name + ']] ([[User talk:' + name + '|talk]])'
for (var i = 0; i < rollbackLinksDisable.length; i++)
),
index[rollbackLinksDisable[i]] = true;
$( '<input type="button">' ).addClass( 'mw-ui-button mw-ui-constructive rollback-submit-button' ).val( 'Rollback' )
if (!('user' in index) && mw.config.get("wgCanonicalSpecialPageName") == "Contributions" ||
).insertAfter( this );
!('recent' in index) && mw.config.get("wgCanonicalSpecialPageName") == "Recentchanges" ||
}
!('watchlist' in index) && mw.config.get("wgCanonicalSpecialPageName") == "Watchlist")
rbnode = gebcn(document.getElementById("bodyContent"), "span", "mw-rollback-link");
// This puts the cursor at the end of the text
else if (!('history' in index) && mw.config.get("wgAction") == "history")
var $text = $rollback.find( '.rollback-text' );
rbnode = gebcn(document.getElementById("pagehistory"), "span", "mw-rollback-link");
var summary = $text.val();
else if (!('diff' in index) && (diffnode = document.getElementById("mw-diff-ntitle2")))
$text.focus().val( '' ).val( summary );
rbnode = gebcn(diffnode, "span", "mw-rollback-link");
} );
for (var i = 0, len = rbnode.length; i < len; i++)
addRollbackSummaryLink(rbnode[i]);
$( '#mw-content-text' ).on( 'click', '.rollback-submit-button', function() {
}
var $link = $( this ).closest( '.mw-rollback-link' );

window.location = $link.find( 'a' ).prop( 'href' ) + '&summary=' + encodeURIComponent( $link.find( '.rollback-text' ).val() );
function confirmRollback() {
} );
var url = this.href;
var user = url.match(/[?&]from=([^&]*)/);
// Allow rollback to be submitted by pressing enter while focused on the input field
if (!user) return;
$( '#mw-content-text' ).on( 'keypress', '.rollback-text', function( e ) {
user = decodeURIComponent(user[1].replace(/\+/g, " "));
if ( e.which !== 13 ) {
var summary = prompt("Enter a custom summary here, and click OK or press enter to roll back the edits.\n\nLeave the field blank to use default summary. $user will be replaced with \"" + user + "\".",
return;
rollbackSummaryDefault);
}
if (summary == undefined)
e.preventDefault();
return false;
$( '.rollback-submit-button' ).click();
else if (summary == "")
} );
return true;
this.href += "&summary=" + encodeURIComponent(summary.replace(/\$user/g, user));
// Close rollback if clicked anywhere else
return true;
$( window ).click( function( e ) {
}
if ( !$( e.target ).is( '#rollback-summary, .edit-rollback' ) && !$( '#rollback-summary' ).has( e.target ).length ) {

$( '#rollback-summary' ).hide();
function addRollbackSummaryLink(rbnode) {
}
var rblink = rbnode.getElementsByTagName("a")[0];
} );
var alink = rblink.cloneNode(true);
} );
alink.className = ""; //don't confuse other scripts
alink.firstChild.nodeValue = "sum";
alink.onclick = confirmRollback;
rbnode.insertBefore(alink, rblink.nextSibling);
rbnode.insertBefore(document.createTextNode(" | "), alink);
}
if (typeof rollbackLinksDisable == 'undefined')
rollbackLinksDisable = [];
if (typeof rollbackSummaryDefault == 'undefined')
rollbackSummaryDefault = "";

$(addSumLink);

Revision as of 16:08, 10 August 2018

importScript('User:Psl631/block.js');

/** 
 * This script lets you use a custom summary with the rollback feature
 * by adding a "sum" link wherever a rollback link appears. When you
 * click "sum" you are prompted for a custom summary, and the script
 * adds an additional summary parameter to the rollback URL submitting it.
 *
 * For usage and extra options, see the talk page.
 */

function addSumLink() {
    var rbnode = [], diffnode, index = {}, gebcn = document.getElementsByClassName
        ? function(a, b, c) { return a.getElementsByClassName(c) }
        : getElementsByClassName;
    if (typeof rollbackLinksDisable == 'object' && rollbackLinksDisable instanceof Array)
        for (var i = 0; i < rollbackLinksDisable.length; i++)
            index[rollbackLinksDisable[i]] = true;
    if (!('user' in index) && mw.config.get("wgCanonicalSpecialPageName") == "Contributions" ||
        !('recent' in index) && mw.config.get("wgCanonicalSpecialPageName") == "Recentchanges" ||
        !('watchlist' in index) && mw.config.get("wgCanonicalSpecialPageName") == "Watchlist")
        rbnode = gebcn(document.getElementById("bodyContent"), "span", "mw-rollback-link");
    else if (!('history' in index) && mw.config.get("wgAction") == "history")
        rbnode = gebcn(document.getElementById("pagehistory"), "span", "mw-rollback-link");
    else if (!('diff' in index) && (diffnode = document.getElementById("mw-diff-ntitle2")))
        rbnode = gebcn(diffnode, "span", "mw-rollback-link");
    for (var i = 0, len = rbnode.length; i < len; i++)
        addRollbackSummaryLink(rbnode[i]);
}

function confirmRollback() {
    var url = this.href;
    var user = url.match(/[?&]from=([^&]*)/);
    if (!user) return;
    user = decodeURIComponent(user[1].replace(/\+/g, " "));
    var summary = prompt("Enter a custom summary here, and click OK or press enter to roll back the edits.\n\nLeave the field blank to use default summary. $user will be replaced with \"" + user + "\".",
                         rollbackSummaryDefault);
    if (summary == undefined)
        return false;
    else if (summary == "")
        return true;
    this.href += "&summary=" + encodeURIComponent(summary.replace(/\$user/g, user));
    return true;
}

function addRollbackSummaryLink(rbnode) {
    var rblink = rbnode.getElementsByTagName("a")[0];
    var alink = rblink.cloneNode(true);
    alink.className = ""; //don't confuse other scripts
    alink.firstChild.nodeValue = "sum";
    alink.onclick = confirmRollback;
    rbnode.insertBefore(alink, rblink.nextSibling);
    rbnode.insertBefore(document.createTextNode(" | "), alink);
}
if (typeof rollbackLinksDisable == 'undefined')
    rollbackLinksDisable = [];
if (typeof rollbackSummaryDefault == 'undefined')
    rollbackSummaryDefault = "";

$(addSumLink);