Difference between revisions of "MediaWiki:Gadget-AddArchiveCategory.js"

From Eugene Maker Space
Jump to navigation Jump to search
(Add a script to tag the current page with the "Archived" category.)
 
m (Fix to avoid categorizing *this* page as Archived.)
Line 3: Line 3:
   
 
function addCategory() {
 
function addCategory() {
var text = '[[Category:Archived]]';
+
var text = '[[Category:' + 'Archived]]';
 
api.edit(mw.config.get('wgPageName'), function (revision) {
 
api.edit(mw.config.get('wgPageName'), function (revision) {
 
return {
 
return {

Revision as of 07:24, 19 February 2025

mw.loader.using(['mediawiki.util', 'mediawiki.api'], function () {
    var api = new mw.Api();

    function addCategory() {
        var text = '[[Category:' + 'Archived]]';
        api.edit(mw.config.get('wgPageName'), function (revision) {
            return {
                text: revision.content + '\n' + text,
                summary: 'Adding category ' + categoryName,
                minor: true
            };
        }).then(function () {
            window.location.reload(); // Reload the page to see the new category
        }, function (e) {
            mw.notify('Failed to add category: ' + e);
        });
    }

    $(document).ready(function () {
        mw.util.addPortletLink(
            'p-tb', // sidebar section (portlet id).
            '#',    // href for the link (will trigger javascript instead)
            'Add Archived Category', // link text.
            'ca-add-archive-category', // unique identifier for the link.
            'Add a category to this page' // tooltip text.
        );
        $('#ca-add-archive-category').click(function (e) {
            e.preventDefault();
            addCategory();
        });
    });
});