User:Sam Sailor/Scripts/ReviewStatus.js

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>
// Forked from [[User:Novem Linguae/Scripts/ReviewStatus.js]] at [[Special:PermanentLink/1220711120]]
// Added id-tracking and guard clause to prevent double injection of icon.

class ReviewStatus {
    async execute() {
        if (!this.shouldRunOnThisPage()) {
            return;
        }
        
        // Check if the icon already exists
        if ($('#mw-review-status-icon').length) {
            return;
        }
        
        const pageID = mw.config.get('wgArticleId');
        const boolIsReviewed = await this.isReviewed(pageID);
        let htmlToInsert = '';
        
        // Add an ID to the image tag so we can track it
        if (boolIsReviewed) {
            htmlToInsert = ' <img id="mw-review-status-icon" src="http://wiki.nitrosworld.org/proxy-img/http%3A%2F%2Fen.wikipedia.org%2Fw%2Fextensions%2FPageTriage%2Fmodules%2Fext.pageTriage.toolbar%2Fimages%2FpageInfo%2Ficon_reviewed.png" title="Reviewed" />';
        } else {
            htmlToInsert = ' <img id="mw-review-status-icon" src="http://wiki.nitrosworld.org/proxy-img/http%3A%2F%2Fen.wikipedia.org%2Fw%2Fextensions%2FPageTriage%2Fmodules%2Fext.pageTriage.toolbar%2Fimages%2FpageInfo%2Ficon_not_reviewed.png" title="Not reviewed" />';
        }
        
        if (this.pageHasSections()) {
            $('#firstHeading .mw-editsection').first().before(htmlToInsert);
        } else {
            $('#firstHeading').append(htmlToInsert);
        }
    }
    
    /**
     * @param {number} pageID The page ID number. A positive number with no commas.
     */
    async isReviewed(pageID) {
        const api = new mw.Api();
        const response = await api.get({
            action: 'query',
            format: 'json',
            formatversion: '2',
            prop: 'isreviewed',
            pageids: pageID
        });
        return response.query.pages[0].isreviewed;
    }
    
    shouldRunOnThisPage() {
        // Don't run when not viewing articles
        const action = mw.config.get('wgAction');
        if (action !== 'view') {
            return false;
        }
        
        // Don't run when viewing diffs
        const isDiff = mw.config.get('wgDiffNewId');
        if (isDiff) {
            return false;
        }
        
        const isDeletedPage = (!mw.config.get('wgCurRevisionId'));
        if (isDeletedPage) {
            return false;
        }
        
        // Only run in mainspace
        const namespace = mw.config.get('wgNamespaceNumber');
        const isMainspaceOrDraftspace = ([0].includes(namespace));
        if (!isMainspaceOrDraftspace) {
            return false;
        }
        
        return true;
    }
    
    pageHasSections() {
        return $('#firstHeading .mw-editsection').length;
    }
}

$(async function() {
    await mw.loader.using(['mediawiki.api'], async function() {
        await (new ReviewStatus()).execute();
    });
});
// </nowiki>
// [[Category:Wikipedia scripts|ReviewStatus]]