Documentation may be created at User:ITechieGamer/test.js/doc.
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.
// <nowiki>
$(document).ready(function() {
var $testGroup = $('<div class="portal collapsed" role="navigation" id="p-test" aria-labelledby="p-test-label"></div>').append($('<h3 id="p-test-label">Test scripts</h3>')),
$testBody = $('<div class="body"></div>'),
$testLink = $('<a id="t-test1" href="#">Add update video</a>');
$($testGroup.append($testBody.html($('<ul id="p-test-list"></ul>').append($('<li/>').html($testLink))))).insertBefore('#p-socialProfiles');
$('#t-test1').click(testFunction);
});
function testFunction() {
var editToken = mw.user.tokens.get('editToken'), output, pageName = mw.config.get('wgPageName'), ytVidId = prompt('YouTube video ID'), isSuccess = 0;
console.log('Working on the raw contents of this page');
$.ajax({
type: 'GET',
url: mw.util.wikiScript('index'),
data: {
title: pageName,
action: 'raw'
},
async: false,
success: function(response) {
output = response;
},
error: function(xhr, textStatus, error) {
console.log(xhr.statusText + '\n' + textStatus + '\n' + error);
alert('Error while getting raw contents of "' + pageName + '"');
return;
}
});
if(output.includes("== Trivia ==")) output = output.replace('== Trivia ==', '== Video ==\nVideo made by [[slicedlime]]:\n{{:' + pageName + '/Update Video}}\n\n== Trivia ==');
else if(output.includes("== References ==")) output = output.replace('== References ==', '== Video ==\nVideo made by [[slicedlime]]:\n{{:' + pageName + '/Update Video}}\n\n== References ==');
else {
console.log('§ Trivia or § References could not be found');
alert('§ Trivia or § References could not be found');
return;
}
console.log('Processing data');
$.ajax({
type: 'POST',
url: mw.util.wikiScript('api'),
data: {
format: 'json',
formatversion: 2,
action: 'edit',
title: pageName + '/Update Video',
text: '{{yt|' + ytVidId + '}}<noinclude>[[Category\x3aUpdate videos]]</noinclude>',
summary: 'Added update video',
bot: true,
minor: true,
createonly: true,
token: editToken
},
dataType: 'json',
async: false,
success: function() {
isSuccess++;
},
error: function(xhr, textStatus, error) {
console.log(xhr.statusText + '\n' + textStatus + '\n' + error);
alert('Error while editing pages');
return;
}
});
$.ajax({
type: 'POST',
url: mw.util.wikiScript('api'),
data: {
format: 'json',
formatversion: 2,
action: 'edit',
title: pageName,
text: output,
summary: 'Added update video',
bot: true,
minor: true,
token: editToken
},
dataType: 'json',
async: false,
success: function() {
isSuccess++;
},
error: function(xhr, textStatus, error) {
console.log(xhr.statusText + '\n' + textStatus + '\n' + error);
alert('Error while editing pages');
return;
}
});
console.log('Success: ' + isSuccess + ' of 2');
alert('Success: ' + isSuccess + ' of 2');
}
// </nowiki>