Created
May 16, 2017 22:13
-
-
Save ngotchac/1857e330b3215add6b64854387bbeb9f to your computer and use it in GitHub Desktop.
Verify every person in a Riot channel
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
(function () { | |
const waitUntil = (query, spent = 0) => { | |
if (spent > 2500) { | |
return Promise.reject(new Error(`Didn't find ${query} after 5s.`)) | |
} | |
const has = document.querySelectorAll(query).length > 0; | |
if (has) { | |
return Promise.resolve(); | |
} | |
return new Promise((resolve, reject) => { | |
setTimeout(() => { | |
return waitUntil(query, spent + 100) | |
.then(resolve) | |
.catch(reject); | |
}, 100); | |
}); | |
}; | |
const waitFor = (time) => { | |
return new Promise((resolve) => { | |
setTimeout(() => { | |
resolve(); | |
}, time); | |
}); | |
}; | |
const verifyUser = () => { | |
let promise = Promise.resolve(); | |
const verifyButtons = Array.slice | |
.call(Array, document.querySelectorAll('.mx_DeviceVerifyButtons button')) | |
.filter((e) => /^verify/i.test(e.innerText)); | |
verifyButtons.forEach((button) => { | |
promise = promise | |
.then(() => { | |
button.click(); | |
return waitUntil('.mx_QuestionDialog'); | |
}) | |
.then(() => { | |
document.querySelector('.mx_Dialog_primary').click(); | |
return waitFor(500); | |
}); | |
}); | |
return promise; | |
}; | |
const users = Array.slice | |
.call(Array, document.querySelectorAll('.mx_EntityTile')); | |
console.warn('users', users); | |
let promise = Promise.resolve(); | |
users.forEach((_, index) => { | |
promise = promise | |
.then(() => { | |
const user = Array.slice | |
.call(Array, document.querySelectorAll('.mx_EntityTile')) | |
[index]; | |
const username = user.querySelector('.mx_EntityTile_name').innerText; | |
console.warn('verifying', username); | |
user.click(); | |
return waitUntil('.mx_DeviceVerifyButtons button') | |
}) | |
.then(() => verifyUser()) | |
.then(() => { | |
document.querySelector('.mx_MemberInfo_cancel').click(); | |
return waitFor(350); | |
}); | |
}); | |
promise | |
.then(() => console.warn('done')) | |
.catch((error) => console.error(error)); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment