public
Last active

Rename users (especially ones with 'default' names)

  • Download Gist
RenameUser.user.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
// ==UserScript==
// @name RenameUser
// @namespace benjol
// @description Rename users (especially ones with 'default' names)
// @include http://stackoverflow.com/*
// @include http://meta.stackoverflow.com/*
// @version 1.0.0
// ==/UserScript==
 
function with_jquery(f) {
var script = document.createElement("script");
script.type = "text/javascript";
script.textContent = "(" + f.toString() + ")(jQuery)";
document.body.appendChild(script);
};
 
with_jquery(function ($) {
 
//Wrap local storage access so that we avoid collisions with other scripts
var prefix = "RenameSOUser-"
function GetStorage(key) { return localStorage[prefix + key]; }
function SetStorage(key, val) { localStorage[prefix + key] = val; }
 
if(GetStorage("RenamesList") == null) {
SetStorage("RenamesList", JSON.stringify({}));
}
 
var list = JSON.parse(GetStorage("RenamesList"));
 
function ReplaceOn(selector, url, name, oldname) {
var el = $(selector);
if(name == oldname) //resetting to old name, no icons
el.text(name);
else if(el.attr("href") == url) //user has same name as when rename defined, tick icon
el.html(name + " ✔");
else //user has changed name, cross icon
el.html(name + " ✘");
}
 
function ReplaceOne(url, name, oldname) {
var urlprefix = url.replace(/(\/\d+\/).*/, "$1");
var usercard = '.user-details > a[href^="' + urlprefix + '"]';
ReplaceOn(usercard, url, name, oldname);
var commmentOwner = 'a.comment-user[href^="' + urlprefix + '"]';
ReplaceOn(commmentOwner, url, name, oldname);
}
 
function ReplaceAll() {
for(var key in list) {
ReplaceOne(list[key].url, list[key].newname, list[key].oldname);
}
}
 
$('.post-signature:has(.user-details > a[href^="/users"])').hover(
function () {
$(this).addClass("bounty");
},
function () {
$(this).removeClass("bounty");
}
);
 
$('.post-signature:has(.user-details > a[href^="/users"])').on("click", function (e) {
if(e.target.nodeName == "A" || e.target.nodeName == "IMG") return; //don't show prompt for existing links!
var url = $(this).find(".user-details > a").attr("href");
var urlprefix = url.replace(/(\/\d+\/).*/, "$1");
var curname = list[urlprefix] != null ? list[urlprefix].newname : $(this).find(".user-details > a").text();
var oldname = list[urlprefix] != null ? list[urlprefix].oldname : curname;
var newname = prompt("New name for " + oldname + " (or leave empty to reset)", curname)
if(newname === null) return; //cancel: do nothing
if(newname.length == 0) { //empty: remove rename
ReplaceOne(url, oldname, oldname); //bug here: if user has changed name, need to refresh page to properly restore
delete list[urlprefix];
}
else {
ReplaceOne(url, newname, oldname);
list[urlprefix] = { url: url, newname: newname, oldname: oldname };
}
SetStorage("RenamesList", JSON.stringify(list));
});
 
ReplaceAll();
});

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.