User:Tholly/Rollback.js

From Simple English Wikipedia, the free encyclopedia

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
//
//  **The script below is an adaption of one from [[w:User:Gracenotes/rollback.js]]**
//           **Please read that page for more details on the script.**
//
//  The Differences:
//  -Instead of just being able to:  $user --> username
//  -Now there is:  $user --> [[User:username|username]] - (linked)
//  -Also: $talk     --> [[User talk:username|talk]] - (linked)
//  -Also: $contribs --> [[Special:Contributions/username|contributions]] - (linked)
//  -Also: $ip       --> [[Special:Contributions/username|username]] - (linked). Used especially for IPs with no userpage.
//  -Also: $rvuser   --> [[User:User to revert to|user to revert to]] - (linked).
//  -Also: $rvip     --> [[Special:Contributions/user to revert to|user to revert to]] - (linked). Used for IPs with no userpage.
//
//  -Another change is that there is already text in the summary box, defaulting to: 
//   "Reverted edits by $ip ($talk) to last version by $rvuser."
//  -Finally, the link to click on next to rollback is "summary", not "sum"
//   and there are spaces between "rollback", "|" and "summary" to match other formatting.
//
 
 
function addSumLink() {
    var ntitle2 = document.getElementById("mw-diff-ntitle2")
    if (!ntitle2) return;
    var rbnode = gdocument.getElementById("mw-diff-ntitle2").querySelectorAll("span.mw-rollback-link");
    if (rbnode.length != 0)
        addRollbackSummaryLink(rbnode[0]);
}
 
function confirmRollback() {
    var url = this.href;
    var user = url.match(/[?&]from=([^&]*)/);
    if (!user) return;
    var user = decodeURIComponent(user[1].replace("+", " "));
    var summary = prompt("Enter a summary to use for rollback. Leave blank to use the default.\n\n- '$user' will be replaced with the link to " + user + "'s userpage,\n- '$talk' to their talk page,\n- '$contribs' to their contributions page, and\n- '$ip' to their contributions page with the link text as '" + user + "'.\n\n- $rvuser links to userpage of user reverting back to, and \n- $rvip to the contribs page of user reverting back to (link text of username).", "Reverted edits by $ip ($talk) to last version by $rvuser.")
    if (summary == undefined)
        return false;
    else if (summary == "")
        return true;
    var startsum = summary.replace(/\$user/g, "[[User:" + user + "|" + user + "]]");
    var worksum1 = startsum.replace(/\$ip/g, "[[Special:Contributions/" + user + "|" + user + "]]");
    var worksum2 = worksum1.replace(/\$rvuser/g, "[[User:$1|$1]]");
    var worksum3 = worksum2.replace(/\$rvip/g, "[[Special:Contributions/$1|$1]]");
    var finsum = worksum3.replace(/\$contribs/g, "[[Special:Contributions/" + user + "|contributions]]");
    this.href += "&summary=" + encodeURIComponent(finsum.replace(/\$talk/g, "[[User talk:" + user + "|talk]]"));
 
    return true;
};
 
function addRollbackSummaryLink(rbnode) {
    var rblink = rbnode.getElementsByTagName("a")[0]
    var alink = rblink.cloneNode(true);
    alink.className = "";
    alink.firstChild.nodeValue = "summary";
    alink.onclick = confirmRollback;
    rbnode.insertBefore(alink, rblink.nextSibling);
    rbnode.insertBefore(document.createTextNode(" | "), alink);
}
 
$(addSumLink);

//  End of script