Skip to content

Instantly share code, notes, and snippets.

@mog86uk
Last active January 18, 2019 22:35
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mog86uk/df95c986b63970226e86 to your computer and use it in GitHub Desktop.
Save mog86uk/df95c986b63970226e86 to your computer and use it in GitHub Desktop.
japaneseclass.jp Practice - userscript to make the site remember all your Practice settings. ^^
// ==UserScript==
// @name JCJP Practice Settings Save
// @namespace mog86uk-jcjp-practice-settings-save
// @version 2.20
// @description JCJP Practice Settings Save! - Stores your Practice settings offline and automatically loads them each new session.
// @author mog86uk
// @match https://japaneseclass.jp/practice
// @grant none
// @noframes
// ==/UserScript==
(function() {
var jQuery = window.jQuery;
jQuery(document).ready(function($) {
var $settingsBox = '',
$saveButton = '',
$settingsButton = '',
$panicButton = '',
panicButtonClicked = false,
practiceSettings = [],
savedSettingsStr = '',
savedSettings = [],
i = 0;
$settingsBox = $('div#settings_box');
$saveButton = $('button#save_settings_button');
$saveButton.click(function() {
if (panicButtonClicked) {
panicButtonClicked = false;
return;
}
var storedValStr = '',
storedVal = [],
practiceSettingChecked = 0;
for (i = 0; i < practiceSettings.length; i++) {
if ($('input#' + practiceSettings[i]).length > 0) {
practiceSettingChecked = $('input#' + practiceSettings[i]).attr('checked');
storedVal[i] = practiceSettingChecked ? 1 : 0;
}
else {
storedVal[i] = 2;
}
}
storedValStr = storedVal.join('');
localStorage.setItem('Mog_SavedPracticeSettings', storedValStr);
});
$settingsButton = $('button#settings_button');
$settingsButton.after('<button id="mog_panic_button" class="header-btn pull-right">パニック</button>');
$panicButton = $('button#mog_panic_button');
$panicButton.click(function() {
panicButtonClicked = true;
// Fix for display issue that occurs sometimes when the user clicks the Settings button.
$settingsBox.css('display', 'block');
$saveButton.click();
});
practiceSettings = $('div#test_settings_box > input').map(function() {
return $(this).attr('id');
}).get();
savedSettingsStr = localStorage.getItem('Mog_SavedPracticeSettings');
if (savedSettingsStr !== null) {
if (savedSettingsStr.length == practiceSettings.length) {
savedSettings = savedSettingsStr.split('');
for (i = 0; i < savedSettings.length; i++) {
if (savedSettings[i] === '0') {
$('input#' + practiceSettings[i]).removeAttr('checked');
$('a#fake' + practiceSettings[i]).attr('class', 'fakecheck');
}
}
}
$saveButton.click();
// Fix for display issue that occurs sometimes when the user clicks the Settings button.
$settingsBox.css('display', 'block');
}
});
})();
@mog86uk
Copy link
Author

mog86uk commented Jan 17, 2019

2019-01-17
v2.00 — Rewrite. Now uses browser localStorage instead of cookies. Improved the code.
v2.10 — Future proofing. Should automatically work for levels 9 and upwards now, if/when Beeant adds more levels. ^^

@mog86uk
Copy link
Author

mog86uk commented Jan 18, 2019

2019-01-18
v2.20 — Bug Fix. Important.

In previous versions, if after saving the settings you happen to unlock a new level, this can confuse it when it comes to loading the settings again because the saved settings contain less levels than you now possess. I've changed it to instead restore all the settings back to their defaults in this scenario, so users will need to reselect which settings they want save each time they level up.

Another thing I fixed is what happens if you press the パニック button while the settings dialog is still open. Before, if you changed a setting but didn't save and instead just left the dialog open, if you then clicked the パニック button while the settings dialog was still left open it would save that setting you changed. If the user really had wanted to save their changes to the settings, they would've clicked the 'save settings' button themselves. This might have been saving unintentional settings changes. I've now changed it to instead just close the settings dialog without applying any unsaved changes.

I hope to improve at least the first of these two things soon. Depends how lazy I get though. I already know what I would need to do to make it carry over your settings correctly after levelling up. (Basically: change it from storing the settings as an array to storing them as an object; also, give a visible notice to the user informing them they have a new level in their settings to leave enabled or to disable.) ^^

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