Created
January 7, 2019 04:37
-
-
Save kmxz/8700325d7c7bc60d930e9b02de7eb617 to your computer and use it in GitHub Desktop.
Script to add to gartic.io
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const assertEqual = (a, b, opt_msg) => { | |
if (a !== b) { | |
throw new RangeError((opt_msg || 'Assertion failed') + ' (expected: ' + b + '; actual: ' + a + ')'); | |
} | |
}; | |
const verifyWords = words => { | |
const lines = words.split('\n').filter(_ => _).map(_ => _.trim()); | |
assertEqual(lines.shift(), 'Word,Difficulty (1 - 3),Dirtyness (1 - 3)'); | |
const validValues = ['1', '2', '3']; | |
const rows = lines.map(l => l.split(',').map(_ => _.trim())); | |
const wordsSet = new Set(); | |
return rows.map(row => { | |
assertEqual(row.length, 3); | |
assertEqual(validValues.includes(row[1]), true); | |
assertEqual(validValues.includes(row[2]), true); | |
assertEqual(wordsSet.has(row[0]), false, row[0] + ' appeared'); | |
wordsSet.add(row[0]); | |
return { | |
word: row[0], | |
difficulty: parseInt(row[1]) - 1 | |
}; | |
}); | |
}; | |
const setUpDom = () => { | |
const $input = document.querySelector('#screens > div > div.content.bg.createTheme > div.globalSettings > div:nth-child(3) > form > div:nth-child(1) > label.text > input[type="text"]'); | |
assertEqual(!!$input, true); | |
assertEqual($input.getAttribute('maxlength'), '30'); | |
const $difficulties = ['easy', 'medium', 'hard'].map(id => document.getElementById(id)); | |
$difficulties.forEach(cb => { | |
assertEqual(cb.getAttribute('type'), 'radio'); | |
assertEqual(cb.getAttribute('name'), 'level'); | |
}); | |
const $btAdd = document.querySelector('#screens > div > div.content.bg.createTheme > div.globalSettings > div:nth-child(3) > form > div:nth-child(1) > label.btAdd > input'); | |
assertEqual(!!$btAdd, true); | |
assertEqual($btAdd.getAttribute('type'), 'submit'); | |
return { | |
$input, $difficulties, $btAdd | |
}; | |
}; | |
// https://stackoverflow.com/a/48890844/2098471 | |
const setNativeValue = (element, value) => { | |
const valueSetter = Object.getOwnPropertyDescriptor(element, 'value').set; | |
const prototype = Object.getPrototypeOf(element); | |
const prototypeValueSetter = Object.getOwnPropertyDescriptor(prototype, 'value').set; | |
if (valueSetter && valueSetter !== prototypeValueSetter) { | |
prototypeValueSetter.call(element, value); | |
} else { | |
valueSetter.call(element, value); | |
} | |
element.dispatchEvent(new Event('input', { bubbles: true })); | |
}; | |
const main = src => { | |
const words = verifyWords(src); | |
const { $input, $difficulties, $btAdd } = setUpDom(); | |
let i = 0; | |
const doSingle = () => { | |
if (i >= words.length) { window.alert('All done!'); return; } | |
const entry = words[i]; | |
setNativeValue($input, entry.word); | |
$difficulties[entry.difficulty].click(); | |
setTimeout(() => { | |
$btAdd.click(); | |
i++; | |
setTimeout(doSingle, 150); | |
}, 150); | |
}; | |
doSingle(); | |
}; | |
// Usage: call main(csvContent) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
how to install
__