Skip to main content
1 of 2

A version that replaces tags, too!

Here's my version of the "update page" script that will:

  • Delete tags defined in tagsThatShouldJustBeRemoved
  • Replace tags defined in tagsToReplace
  • Adds a * to the page title to indicate that the tags were updated

.

// ==UserScript==
// @name       Burn/replace tags
// @version    1.0
// @description  Replace/Delete tags
// @match      https://stackoverflow.com/questions/*
// ==/UserScript==

// Only run on pages with questions (question number in URL)

if ('0123456789'.indexOf(window.location.pathname.charAt(11)) === -1) return;

function exec(fn) {
    var script = document.createElement('script');
    script.setAttribute("type", "application/javascript");
    script.textContent = '(' + fn + ')();';
    document.body.appendChild(script); // run the script
    document.body.removeChild(script); // clean up
}

window.addEventListener("load", function () {

    // script injection
    exec(function () {

    /* add other tags that should get REMOVED but don't indicate the question is deletable */
    var tagsThatShouldJustBeRemoved = ["amazon"];
    var tagsToReplace = {'aws-athena':'amazon-athena'};
    var tags = $(".post-taglist .post-tag").map(function () { return $(this).text(); }).get();
    var toSave = [];

    $.each(tags,
           function (index)
           {
               if (tags[index] in tagsToReplace)
                   toSave.push(tagsToReplace[tags[index]]);
               else if ($.inArray(tags[index], tagsThatShouldJustBeRemoved) === -1)
                   toSave.push(tags[index]); // save it
           });

    // No change? Exit!
    if (toSave === tags) return;

    // No tags? Tell user.
    if (toSave.length === 0){
        alert('This question has nothing but bad tags on it.  Either figure out a valid tag or flag and ask a mod to delete it if it is hopeless.');
        return;
    }

    var id = document.location.href.match(/\d+/);

    // Saves new tags
    $.ajax({
        type: "POST",
        url: "https://stackoverflow.com/posts/" + id + "/edit-tags",
        data: { fkey: StackExchange.options.user.fkey, tagnames: toSave.join(" ") },
        success: function (x, status, error)
        {
            // Indicate update by adding a * to title
            document.title = '*' + document.title;
        },
        error: function (x, status, error)
        {
            alert(status + "\\n" + error);
        },
    });

    });
}, false);