User:Zackmann08/scripts/updateEditCount.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 code will be executed when previewing this page.
Documentation for this user script can be added at User:Zackmann08/scripts/updateEditCount.
async function updateEditCount(){
var url = "https://en.wikipedia.org/wiki/Special:Contributions/Zackmann08";
mw.notify('Getting Contributions', {type: 'notice'});
var edit_count;
try {
var response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
// Get the response body as plain text (HTML string)
const htmlText = await response.text();
edit_count = htmlText.match(/A user with ([\d\,]+) edits/)[1];
} catch (error) {
console.error('Fetch error:', error);
mw.notify('Something went wrong. Check log for errors.', {type: 'error'});
}
mw.loader.using("mediawiki.api", function () {
var api = new mw.Api();
api.get({
prop: 'revisions',
rvprop: 'content',
rvlimit: 1,
titles: 'User:Zackmann08/count/edit'
}).done( function ( data ) {
if ( !data.query || !data.query.pages ) return;
var pageid = Object.getOwnPropertyNames( data.query.pages )[0],
text = data.query.pages[pageid].revisions[0]["*"];
new_text = edit_count;
api.postWithEditToken( {
action: "edit",
title: 'User:Zackmann08/count/edit',
summary: 'updating edit count using [[User:Zackmann08/scripts/updateEditCount.js]]',
text: new_text
}).done ( function ( data ) {
if ( data && data.edit && data.edit.result && data.edit.result == 'Success' ) {
mw.notify( "Count updated to " + edit_count, {type: 'success'} );
}
});
});
});
url = 'https://en.wikipedia.org/wiki/Wikipedia:List_of_Wikipedians_by_number_of_edits/1%E2%80%931000';
mw.notify('Getting Rank', {type: 'notice'});
var rank;
try {
var response = await fetch(url);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const htmlText = await response.text();
const parser = new DOMParser();
const doc = parser.parseFromString(htmlText, "text/html");
const tdZackmann08 = Array.from(doc.querySelectorAll('td')).find(td => {
const a = td.querySelector('a');
return a && a.textContent.trim() === 'Zackmann08';
});
rank = tdZackmann08.previousElementSibling.textContent
} catch (error) {
console.error('Fetch error:', error);
mw.notify('Something went wrong. Check log for errors.');
}
mw.loader.using("mediawiki.api", function () {
var api = new mw.Api();
api.get({
prop: 'revisions',
rvprop: 'content',
rvlimit: 1,
titles: 'User:Zackmann08/count/rank'
}).done( function ( data ) {
if ( !data.query || !data.query.pages ) return;
var pageid = Object.getOwnPropertyNames( data.query.pages )[0],
text = data.query.pages[pageid].revisions[0]["*"];
new_text = rank;
api.postWithEditToken( {
action: "edit",
title: 'User:Zackmann08/count/rank',
summary: 'updating rank using [[User:Zackmann08/scripts/updateEditCount.js]]',
text: new_text
}).done ( function ( data ) {
if ( data && data.edit && data.edit.result && data.edit.result == 'Success' ) {
if (text != new_text) {
mw.notify( "Rank updated to " + new_text, {type: 'success'} );
}
window.setTimeout( function () {
document.location.reload( true );
}, 2000 );
}
});
});
});
}
jQuery(document).ready(function($) {
if (mw.config.get('wgPageName') == 'User:Zackmann08'){
mw.util.addPortletLink(
"p-cactions",
"javascript:updateEditCount()",
"Update EC"
);
}
});