Skip to content

Instantly share code, notes, and snippets.

@kmxz
Created January 7, 2019 04:37
Show Gist options
  • Save kmxz/8700325d7c7bc60d930e9b02de7eb617 to your computer and use it in GitHub Desktop.
Save kmxz/8700325d7c7bc60d930e9b02de7eb617 to your computer and use it in GitHub Desktop.
Script to add to gartic.io
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)
@Fake0010
Copy link

We need script anti kicked.

@richardfat7
Copy link

works well, thanks

@dwqefqwfwef
Copy link

how to install

__

@CJNJCJJUVBBVBVBVN
Copy link

i need script anti kick

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