User:Gryllida/js/draft-rater.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. |
Documentation for this user script can be added at User:Gryllida/js/draft-rater. |
// <nowiki>
mw.loader.using(['mediawiki.api'], function () {
"use strict";
var api = new mw.Api();
//validProjectsArray = ['Australia', '']
/*if(mw.config.get('wgCanonicalNamespace') != 'Draft'){
return;
}*/
var visitNextDraft = function(){
window.open("https://en.wikipedia.org/wiki/Special:RandomInCategory/Pending_AfC_submissions","_self");
};
// if the talk page does not exist, add a textbox for input
if($('#ca-talk').attr('class') && $('#ca-talk').attr('class') == 'new'){
$('#p-namespaces').append($('<div></div>', {id: 'gryllida-draft-rater-div'}));
var $input = $('<input/>', {
type:'text',
name: 'gryllida-wikiproject',
id: 'gryllida-wikiproject',
form: 'gryllida-wpjs-form'
});
var $label =$('<label>', {
for:'gryllida-wikiproject'
}).text('WPrjs:');
var $submitButton =$('<input>', {
type: 'submit',
style: 'display: none'
});
$input.keyup(function(event) {
if ($input.is(":focus") && event.key == "Enter") {
// if user pressed enter, add the marker
if($input.val().length > 0){
var $span = $('<span></span>', {
class: 'gryllida-draft-rater',
text: $input.val()
});
$span.click(function(){
$span.remove();
});
$span.css('margin-left','10px');
$span.css('margin-right','10px');
$span.css('background-color', 'beige');
// check whether wikiproject name is valid
var projname = $input.val();
api.get( {
action: 'query',
titles: 'Wikipedia:WikiProject '+projname,
prop: 'info'}).done( function ( data ) {
if(data.query.pages['-1']){
return;
}
});
// XXX extend this
// https://en.wikipedia.org/w/api.php?
// action=query
// titles=Page%20that%20does%20not%20exist
// &prop=info&inprop=&format=json
/*if(validProjectsArray.indexOf(projname) > -1){
$span.css('background-color', '#ddffdd');
}*/
// add to dom
$span.insertBefore($input);
$input.val('');
// submit the marks
} else {
$input.prop('disabled', true);
var str = '';
var summary = 'added projects ';
var TXTsummary = 'added projects ';
$('.gryllida-draft-rater').each(function(i){
var projN = $($('.gryllida-draft-rater')[i]).text();
str = str + '{' + '{WikiProject ' + projN + '}}\r\n';
summary = summary + '[[Wikipedia:WikiProject ' + projN + '|'+projN+']] ';
summary = summary + '([[Template:WikiProject ' + projN + '|template]]), ';
TXTsummary = TXTsummary + '[[Wikipedia:WikiProject ' + projN + '|'+projN+']], ';
});
var realsummary = summary + ' ([[User:Gryllida/drafts|how to join]])';
var wgNamespaceNumber=mw.config.get( 'wgNamespaceNumber' );
var wgTitle = mw.config.get('wgTitle');
api.postWithToken("edit", {
action: 'edit',
title: mw.config.get( 'wgFormattedNamespaces' )[wgNamespaceNumber+1] +':'+wgTitle,
text: str,
summary: realsummary
}).done(function (data){
var pageName = mw.config.get( 'wgPageName' ).replace(/\_/g, ' ');
// find who made the article
api.get( {
action: 'query',
titles: pageName,
prop: 'revisions',
rvprop: 'user',
rvlimit: 1,
'rvdir': 'newer'
}).done(function(data){
var u = data.query.pages[Object.keys(data.query.pages)].revisions[0].user;
//alert(u);
// action=query&
// format=json&
// prop=revisions&
// titles=User%20talk%3AGryllida&callback=&rvlimit=1&rvdir=older
api.get( {
action: 'query',
titles: 'User talk:'+u,
prop: 'revisions',
rvprop: 'content',
rvlimit: 1,
'rvdir': 'older'
}).done(function(data){
var text = "";
if(data.query.pages[Object.keys(data.query.pages)].revisions){
text = data.query.pages[Object.keys(data.query.pages)].revisions[0]['*'];
}
//alert(text);
//alert(text.indexOf('welcome') == -1);
var msg = "";
if(text.indexOf('welcome') == -1) {
msg = msg + '{{subst:Welcome}}{{subst:sisterprojects}}--~~~~';
}
msg = msg + '\r\n' + '== Re: [[' + pageName + ']]==\r\n';
msg = msg + 'Hello ' + u + '\r\nI have ' + TXTsummary + ' to your article.';
msg = msg + ' You may wish to join them, check their to-do, and meet new';
msg = msg + ' people with interest in these topics. ';
msg = msg + '( To reply click "edit" next to this section, and add your reply at the end. ) ';
msg = msg + 'Cheers, --~~~~';
api.postWithToken("edit", {
action: 'edit',
title: 'User talk:'+u,
section: 'new',
text: msg
}).done(function (data){
api.postWithToken("watch", {
action: 'watch',
title: 'User talk:'+u
}).done(function (data){
$('#gryllida-draft-rater-div').remove();
var $span = $('<span>', {html: 'Notified <a href="http://en.wikipedia.org/wiki/User talk:'+u+'">'+u+'</a>.'});
$span.insertAfter($('#ca-talk'));
$('#ca-talk').attr('class', '');
$('#ca-talk').attr('class', '');
// visit next draft
visitNextDraft();
});
});
});
});
});
}
}
});
var $form = $('<form>', {action: '#', id: 'gryllida-wpjs-form'});
$form.append($label);
$form.append($input);
$form.append($submitButton);
$form.submit(false);
$('#gryllida-draft-rater-div').append($form);
$input.attr('autocomplete');
$form.attr('autocomplete')
$input.focus();
} else{
//visitNextDraft();
}
});
// </nowiki>