Skip to content

Instantly share code, notes, and snippets.

@Fawers
Last active April 1, 2018 23:39
Show Gist options
  • Save Fawers/cbdf72d8b3576ae51a631f5767f7b6f0 to your computer and use it in GitHub Desktop.
Save Fawers/cbdf72d8b3576ae51a631f5767f7b6f0 to your computer and use it in GitHub Desktop.
WaniKani Rollback Answer
/**
* wkrollback - WaniKani Rollback Answer
* Inspired by Wanikani Override userscript on Greasy Fork,
* but aimed at mobiles, as an inline JavaScript bookmarklet.
* This is the unminified code; use the minified version on
* your browser.
*
* Author: Fabricio da Silva Werneck
* Date: 2018-04-01 19:24 -0300
*
* How it works:
* WaniKani ("WK") stores everything that happens in a review
* session in a browser-level database called jStorage, and this
* includes information about wrong items.
* When you answer an item wrongly, WK will store this information
* on a key inside jStorage. This key is a combination of the item
* type (radical / kanji / vocab) and its id. If you understand
* Regex語, it's easier to understand with a pattern: /([rkv])(\d+)/,
* being group 1 the item type identifier, and group 2 the item id.
* This key holds an object that can have up to 4 (four) keys:
* - mc
* - mi
* - rc
* - ri
* We're interested in 'mi' and 'ri', which stands for 'meaning incorrect'
* and 'reading incorrect', respectively. They serve as counters for how
* many times you answered wrongly the meaning and reading of an item
* (for obvious reasons, radicals only work with 'mi' and 'mc').
* This script injects a button in the row of buttons below the user input,
* and resets either 'mi' or 'ri' of the current item to 0, depending on the
* question type (meaning or reading) on press.
*
* NOTE: this script is intended for TYPOS only. While I can't control
* whether you're intentionally using this script to cheat, do keep in mind
* that I'm not the one being fooled with you misusing it.
* Peace!
*/
(function(stg) {
/**
* - buttonContainer is the row of buttons below the user input field.
* - rollbackButton is the button this script adds to the container.
* - fieldset is the entity that holds the css class responsible for
* colorizing the user input field.
*/
var buttonContainer = $('#additional-content ul'),
rollbackButton = $('<li><span title="Rollback">Rollback</span></li>'),
fieldset = $('#user-response').parent();
// css injection for visual feedback. because, you know. aesthetics.
document.head.innerHTML += (
'<style>\n\
fieldset.backrolled input {\n\
color: rgb(104, 104, 104);\n\
background-color: rgb(238, 202, 0);\n\
}\n\
#additional-content ul {\n\
display: flex;\n\
justify-content: space-between;\n\
}\n\
</style>'
);
function rollback() {
// if item has already been marked as backrolled, do nothing.
if (fieldset.hasClass('backrolled')) {
console.log('Rollback for current item has taken action already; ignoring.');
alert('Item is already marked as backrolled.');
return;
}
// if item is not marked as incorrect, do nothing.
else if (!fieldset.hasClass('incorrect')) {
console.log('Current item is not marked as incorrect; ignoring.');
alert('Item is not incorrect!');
return;
}
var currentItem = stg.get('currentItem'),
type = currentItem.rad ? 'r' : currentItem.kan ? 'k' : 'v',
id = currentItem.id;
var qType = stg.get('questionType')[0];
var item = type + id,
key = qType + 'i',
entry = stg.get(item);
entry[key] = 0;
stg.set('questionCount', stg.get('questionCount') - 1);
stg.set('wrongCount', stg.get('wrongCount') - 1);
stg.set(item, entry);
fieldset.removeClass('incorrect');
fieldset.addClass('backrolled');
}
rollbackButton.on('click', rollback);
document.onkeypress = function(e) {(e.code === 'Backquote' && fieldset.find('#user-response').is(':disabled')) && rollbackButton.click()};
buttonContainer.append(rollbackButton);
})($.jStorage);
javascript:!function(r){var e=$("#additional-content ul"),n=$('<li><span title="Rollback">Rollback</span></li>'),a=$("#user-response").parent();document.head.innerHTML+="<style>\n fieldset.backrolled input {\n color: rgb(104, 104, 104);\n background-color: rgb(238, 202, 0);\n }\n #additional-content ul {\n display: flex;\n justify-content: space-between;\n }\n </style>",n.on("click",function(){if(a.hasClass("backrolled"))return console.log("Rollback for current item has taken action already; ignoring."),void alert("Item is already marked as backrolled.");if(!a.hasClass("incorrect"))return console.log("Current item is not marked as incorrect; ignoring."),void alert("Item is not incorrect!");var e=r.get("currentItem"),n=(e.rad?"r":e.kan?"k":"v")+e.id,t=r.get("questionType")[0]+"i",o=r.get(n);o[t]=0,r.set("questionCount",r.get("questionCount")-1),r.set("wrongCount",r.get("wrongCount")-1),r.set(n,o),a.removeClass("incorrect"),a.addClass("backrolled")}),document.onkeypress=function(e){"Backquote"===e.code&&a.find("#user-response").is(":disabled")&&n.click()},e.append(n)}($.jStorage);
@Fawers
Copy link
Author

Fawers commented Apr 1, 2018

WaniKani Rollback Answer

"Installation" instructions

Open the raw, minified script on your browser, copy it all. Create a new bookmark on your browser, edit it, and paste the content on the URL field. Give this bookmark an easy name. (We're using wkrollback here.)

Usage

While on the review session page, click on the address bar and type in wkrollback. You should see your bookmark there. Click on it. A new button, labeled Rollback, should appear below the user input box. Click this button when you get an answer wrong, and the script will reset it for you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment