Difference between revisions of "User:Vandalbane/common.js"
Vandalbane (talk | contribs) (Undo revision 283170 by Vandalbane (talk)) |
Vandalbane (talk | contribs) |
||
Line 1: | Line 1: | ||
− | + | // This script implements a very crude one-click undo. If you go onto a diff page with an undo link, this will change it to work in one single click rather than two. | |
+ | // It also adds a "revert" link to edits on contribution pages, that works the same way (one click). | ||
+ | // Known glitch: Does not work as intended if a user has edited the same page multiple times (to avoid this, I recommend ticking the "Only show edits that are latest revisions" box. | ||
+ | |||
+ | function rvt(undop, undoafterp, c, tl) { // Implements one-click undo | ||
+ | $.ajax({ | ||
+ | url: 'https://www.explainxkcd.com/wiki/api.php', | ||
+ | data: { | ||
+ | format: 'json', | ||
+ | action: 'edit', | ||
+ | title: atob(tl), | ||
+ | undo: undop, | ||
+ | undoafter: undoafterp, | ||
+ | summary: "Reverted vandalism with [[User:CRLF/OneClickUndo.js]]", | ||
+ | token: mw.user.tokens.get('csrfToken') | ||
+ | }, | ||
+ | dataType: 'json', | ||
+ | type: 'POST', | ||
+ | success: function () { if (c) { $('.mw-diff-undo > a').text("success"); $('.mw-diff-undo > a').removeAttr("onclick"); } else { $('#link' + undop).text("success"); $('.mw-diff-undo > a').removeAttr("onclick"); } } | ||
+ | }); | ||
+ | } | ||
+ | try { | ||
+ | var href = $('.mw-diff-undo > a').attr('href').split("="); | ||
+ | var undop = href[4]; | ||
+ | var undoafterp = href[3].substr(0, href[3].length - 5); | ||
+ | $('.mw-diff-undo > a').removeAttr('href'); | ||
+ | $('.mw-diff-undo > a').attr('onclick', 'rvt(' + undop + ',' + undoafterp + ',1,"'+btoa($("#firstHeading").text().substr(33, $("#firstHeading").text().length - 34))+'")'); | ||
+ | } | ||
+ | catch (e) { console.log(e); console.log("assumed not undoable"); } | ||
+ | |||
+ | |||
+ | function makeLinks(str, titles) { // Implements undo links on contribs page | ||
+ | $.ajax({ | ||
+ | url: 'https://www.explainxkcd.com/wiki/api.php', | ||
+ | type: 'GET', | ||
+ | dataType: 'json', | ||
+ | data: { format: 'json', action: 'query', prop: 'revisions', titles: str, rvprop: 'ids', formatversion: 2 }, | ||
+ | success: function (data) { | ||
+ | for (var i = 0; i < data["query"]["pages"].length; i++) { | ||
+ | ob = data["query"]["pages"][i] | ||
+ | pg = ob["title"]; | ||
+ | if (titles[pg] == "done") continue; | ||
+ | el = titles[pg]; | ||
+ | $('<span id="span' + ob["revisions"][0]["revid"] + '"></span>').insertAfter(el); | ||
+ | $("#span" + ob["revisions"][0]["revid"]).html(' <strong>[<a id="link' + ob["revisions"][0]["revid"] + '" onclick=\'rvt(' + ob["revisions"][0]["revid"] + ',' + ob["revisions"][0]["parentid"] + ',0,"'+btoa(pg)+'")\'>revert</a>]</strong>'); | ||
+ | titles[pg] = "done"; | ||
+ | } | ||
+ | } | ||
+ | }) | ||
+ | } | ||
+ | |||
+ | cs = $('.mw-contributions-title'); | ||
+ | titles = {}; | ||
+ | str = ""; | ||
+ | for (var i = 0; i < cs.length; i++) { | ||
+ | titles[cs[i].getAttribute("title")] = cs[i]; | ||
+ | str += cs[i].getAttribute("title") + '|' | ||
+ | makeLinks(str.slice(0, -1), titles); | ||
+ | } |
Revision as of 00:40, 30 May 2022
// This script implements a very crude one-click undo. If you go onto a diff page with an undo link, this will change it to work in one single click rather than two. // It also adds a "revert" link to edits on contribution pages, that works the same way (one click). // Known glitch: Does not work as intended if a user has edited the same page multiple times (to avoid this, I recommend ticking the "Only show edits that are latest revisions" box. function rvt(undop, undoafterp, c, tl) { // Implements one-click undo $.ajax({ url: 'https://www.explainxkcd.com/wiki/api.php', data: { format: 'json', action: 'edit', title: atob(tl), undo: undop, undoafter: undoafterp, summary: "Reverted vandalism with [[User:CRLF/OneClickUndo.js]]", token: mw.user.tokens.get('csrfToken') }, dataType: 'json', type: 'POST', success: function () { if (c) { $('.mw-diff-undo > a').text("success"); $('.mw-diff-undo > a').removeAttr("onclick"); } else { $('#link' + undop).text("success"); $('.mw-diff-undo > a').removeAttr("onclick"); } } }); } try { var href = $('.mw-diff-undo > a').attr('href').split("="); var undop = href[4]; var undoafterp = href[3].substr(0, href[3].length - 5); $('.mw-diff-undo > a').removeAttr('href'); $('.mw-diff-undo > a').attr('onclick', 'rvt(' + undop + ',' + undoafterp + ',1,"'+btoa($("#firstHeading").text().substr(33, $("#firstHeading").text().length - 34))+'")'); } catch (e) { console.log(e); console.log("assumed not undoable"); } function makeLinks(str, titles) { // Implements undo links on contribs page $.ajax({ url: 'https://www.explainxkcd.com/wiki/api.php', type: 'GET', dataType: 'json', data: { format: 'json', action: 'query', prop: 'revisions', titles: str, rvprop: 'ids', formatversion: 2 }, success: function (data) { for (var i = 0; i < data["query"]["pages"].length; i++) { ob = data["query"]["pages"][i] pg = ob["title"]; if (titles[pg] == "done") continue; el = titles[pg]; $('<span id="span' + ob["revisions"][0]["revid"] + '"></span>').insertAfter(el); $("#span" + ob["revisions"][0]["revid"]).html(' <strong>[<a id="link' + ob["revisions"][0]["revid"] + '" onclick=\'rvt(' + ob["revisions"][0]["revid"] + ',' + ob["revisions"][0]["parentid"] + ',0,"'+btoa(pg)+'")\'>revert</a>]</strong>'); titles[pg] = "done"; } } }) } cs = $('.mw-contributions-title'); titles = {}; str = ""; for (var i = 0; i < cs.length; i++) { titles[cs[i].getAttribute("title")] = cs[i]; str += cs[i].getAttribute("title") + '|' makeLinks(str.slice(0, -1), titles); }