Jump to content

User:Zackmann08/scripts/updateEditCount.js

From Wikipedia, the free encyclopedia
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.
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"
    	);
    }
});