User:Enterprisey/cv-revdel.js
Appearance
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. |
This user script seems to have a documentation page at User:Enterprisey/cv-revdel. |
// <nowiki>
$( function () {
var ADVERT = " ([[User:Enterprisey/cv-revdel|cv-revdel]])";
var SUMMARY = "Requesting copyvio revdel" + ADVERT;
var SUMMARY_TALK = "Add note about recent copyvio" + ADVERT;
var CCLEAN_TPL = "Cclean";
var pageName; // Current page name
var saved = false; // has the submit button been clicked?
var urlCounter = 1;
function iterableToList( nl ) {
var len = nl.length;
var arr = new Array( len );
for( var i = 0; i < len; i++ ) arr[i] = nl[i];
return arr;
}
/**
* Make a link to a given oldid of the current page.
*/
function makeOldidLink( oldid ) {
var href = "/w/index.php?title=" + encodeURIComponent( pageName ) +
"&oldid=" + oldid;
var link = document.createElement( "a" );
link.href = href;
link.textContent = oldid;
return link;
}
/**
* Add the appropriate copyvio-revdel template to the current page.
*/
function addCvRevdel( urls ) {
var deferred = $.Deferred();
var template = "{" + "{copyvio-revdel";
template += urls.map( function ( u, idx ) {
var num = idx == 0 ? "" : ( idx + 1 );
return "|url" + num + "=" + u;
} ).join( "" );
var rows = document.querySelectorAll( "#cv-revdel tr" );
var num;
for( var i = 1, n = rows.length; i < n; i++ ) {
template += "|start" + i + "=" + rows[i].childNodes[0].textContent
+ ( rows[i].childNodes[2].childNodes[0].checked ? ( "|end" + i + "=" + rows[i].childNodes[1].textContent ) : "" );
}
template += "}}";
( new mw.Api() ).postWithToken( "csrf", {
action: "edit",
title: pageName,
summary: SUMMARY,
prependtext: template + "\n"
} ).done( function ( d ) {
if( d && d.edit && d.edit.result && d.edit.result == "Success" ) {
deferred.resolve();
} else {
console.log( d );
deferred.reject();
}
} ).fail( function ( code, result ) {
console.log( code, result );
deferred.reject();
} );
return deferred;
}
function addCclean( urls ) {
var deferred = $.Deferred(),
template = "{" + "{subst:cclean|url=" + urls.join( " " ) + "}}",
talkNs = mw.config.get( "wgNamespaceNumber" ) | 1,
talkNsName = mw.config.get( "wgFormattedNamespaces" )[ talkNs ],
talkPage = talkNsName + ":" + mw.config.get( "wgTitle" );
( new mw.Api() ).postWithToken( "csrf", {
action: "edit",
title: talkPage,
summary: SUMMARY_TALK,
appendtext: "\n" + template
} ).done( function ( d ) {
if( d && d.edit && d.edit.result && d.edit.result == "Success" ) {
deferred.resolve();
} else {
deferred.reject();
console.log( d );
}
} ).fail( function ( code, result ) {
deferred.reject();
console.log( code, result );
} );
return deferred;
}
/**
* Load the main cv-revdel panel and add buttons/other stuff to
* the history UI
*/
function load( evt ) {
if( evt ) evt.preventDefault();
// Don't load the panel for a second time if it's already there
if( document.getElementById( "cv-revdel" ) ) return;
// Style for the panel
mw.util.addCSS(
"#cv-revdel { border: thin solid rgb(197, 197, 197); " +
"box-shadow: 0px 3px 8px rgba(0, 0, 0, 0.25); border-radius: 3px;" +
"padding: 2em; display: inline-block }" +
"#cv-revdel table { margin: 1em 0 }" +
"#cv-revdel td { padding: 0 0.5em }" +
"#cv-revdel a.disabled {color: gray;text-decoration: line-through;font-style: italic;}" +
"#cv-rd-submit { margin-right: 2em }"+
"#cv-rd-urls input { width: 50%; min-width: 30em; }"+
"#cv-rd-urls div { margin-bottom: 0.35em; }"+
"#cv-revdel div.buttons { margin-top: 1em; }"
);
// Add the panel itself
var panel = document.createElement( "div" );
panel.id = "cv-revdel";
var urlHTML = "<div id='cv-rd-urls'>" +
"<div><label for='cv-rd-url0'>URL: </label><input type='text' "+
"id='cv-rd-url0' class='mw-ui-input mw-ui-input-inline'/>"+
"<button class='mw-ui-button mw-ui-quiet'>Remove</button></div>" +
"</div>"+
"<button id='cv-rd-url-add' class='mw-ui-button'>Add another</button>";
panel.innerHTML = urlHTML +
"<table id='cv-rd-ranges'><tr><th>Start</th><th>End</th>" +
"<th>Include end?</th><th>Remove</th></table>" +
"<input type='checkbox' id='cv-rd-cclean' />" +
"<label for='cv-rd-cclean'>Add {{subst:<a href='" +
mw.util.getUrl( "Template:" + CCLEAN_TPL ) + "'>Cclean</a>}} to " +
( ( mw.config.get( "wgNamespaceNumber" ) % 2 ) ? "this" : "talk" ) +
" page</label><br />"+
"<div class='buttons'><button id='cv-rd-submit' class='mw-ui-button"+
" mw-ui-progressive'>Submit</button>" +
"<button id='cv-rd-close' class='mw-ui-button mw-ui-quiet'>Close</button></div>";
document.getElementById( "bodyContent" ).insertBefore( panel,
document.getElementById( "mw-content-text" ) );
// Add range-add buttons before each of the buttons
// that say "Compare selected revisions"
var cmpSelRevsBtns = document.getElementsByClassName( "historysubmit" );
for( var i = 0, n = cmpSelRevsBtns.length; i < n; i++ ) {
var rangeBtn = document.createElement( "button" );
rangeBtn.textContent = "Add range to revdel template";
rangeBtn.className = "cv-rd-add-range";
cmpSelRevsBtns[i].parentNode.insertBefore( rangeBtn, cmpSelRevsBtns[i] );
rangeBtn.addEventListener( "click", function ( evt ) {
evt.preventDefault();
var oldidStart = document.querySelector( "li.selected.after" ).dataset.mwRevid;
var oldidEnd = document.querySelector( "li.selected.before" ).dataset.mwRevid;
// Add new row to ranges table
var rangesTable = document.getElementById( "cv-rd-ranges" ).getElementsByTagName( "tbody" )[0];
var newRow = rangesTable.insertRow( rangesTable.rows.length );
newRow.insertCell( 0 ).appendChild( makeOldidLink( oldidStart ) );
newRow.insertCell( 1 ).appendChild( makeOldidLink( oldidEnd ) );
newRow.insertCell( 2 ).innerHTML = "<input type='checkbox' />";
newRow.cells[2].childNodes[0].checked = true;
newRow.cells[2].childNodes[0].addEventListener( "click", function () {
this.parentNode.previousElementSibling.childNodes[0].className = this.checked ? "" : "disabled";
} );
var deleteBtn = document.createElement( "button" );
deleteBtn.textContent = "Delete";
deleteBtn.className = "delete";
deleteBtn.addEventListener( "click", function () {
this.parentNode.parentNode.parentNode.removeChild(
this.parentNode.parentNode );
} );
newRow.insertCell( 3 ).appendChild( deleteBtn );
} );
}
// Panel submission handler
document.getElementById( "cv-rd-submit" ).addEventListener( "click", function () {
if( saved ) return;
var urls = iterableToList( document.getElementById( "cv-rd-urls" ).children )
.map( function ( e ) { return e.children[1].value; } );
var deferreds = [ addCvRevdel( urls ) ];
if( document.getElementById( "cv-rd-cclean" ).checked ) {
deferreds.push( addCclean( urls ) );
}
saved = true;
$.when.apply( $, deferreds ).then( function () {
document.querySelector( "#ca-view a" ).click();
} );
} );
// "Add URL" handler
document.getElementById( "cv-rd-url-add" ).addEventListener( "click", function () {
var numUrls = document.querySelectorAll( "#cv-rd-urls div" ).length;
if( numUrls < 3 ) {
var newDiv = document.createElement( "div" );
newDiv.innerHTML = "<label for='cv-rd-url" + urlCounter + "'>URL: </label>"+
"<input type='text' id='cv-rd-url" + urlCounter +
"' class='mw-ui-input mw-ui-input-inline'/>"+
"<button class='mw-ui-button mw-ui-quiet'>Remove</button>";
document.getElementById( "cv-rd-urls" ).appendChild( newDiv );
urlCounter++;
this.disabled = numUrls >= 2;
}
} );
// Remove URL handler
document.getElementById( "cv-rd-urls" ).addEventListener( "click", function ( e ) {
var numUrls = document.querySelectorAll( "#cv-rd-urls div" ).length;
if( e.target && e.target.nodeType === 1 && numUrls > 1 &&
e.target.tagName.toLowerCase() === "button" ) {
this.removeChild( document.getElementById( e.target.previousElementSibling.getAttribute( "id" ) ).parentNode );
document.getElementById( "cv-rd-url-add" ).disabled = false;
}
} );
// Close handler
document.getElementById( "cv-rd-close" ).addEventListener( "click", function () {
$( "#cv-revdel" ).remove();
$( ".cv-rd-add-range" ).remove();
} );
document.querySelector( "#cv-rd-urls input" ).focus();
}
mw.loader.using( [ "mediawiki.api", "mediawiki.util" ], function () {
importStylesheet( "User:Enterprisey/mw-ui-button.css" );
importStylesheet( "User:Enterprisey/mw-ui-input.css" );
pageName = mw.config.get( "wgPageName" );
if( mw.config.get( "wgAction" ) == "history" ) {
var link = mw.util.addPortletLink( "p-cactions", "#", "Request CV revdel", "pt-cv-revdel" );
link.addEventListener( "click", load );
if( mw.util.getParamValue( "open_cv_revdel" ) === "true" ) {
load();
}
} else if( mw.config.get( "wgNamespaceNumber" ) >= 0 ) {
var historyPage = mw.util.getUrl( pageName, { "action": "history", "open_cv_revdel": "true" } );
var link = mw.util.addPortletLink( "p-cactions", historyPage, "Request CV revdel", "pt-cv-revdel" );
}
} );
} );
// </nowiki>