// ==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(); });