Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ==UserScript==
- // @name anon.fm AJAX posting
- // @namespace FM veshtschaniye
- // @version 0.0.1.6
- // @author AHOHNMYC
- // @match https://anon.fm/feedback*
- // @match https://alcxemuct.accountant/feedback*
- // @grant none
- // ==/UserScript==
- /* Клик на капчу обновляет её
- * Сообщение отправляется по Enter
- * R на поле ввода капчи обновляет её
- * */
- const img = document.getElementsByTagName('img')[0];
- const capchaInput = document.querySelector('[name=check]');
- /* Стили */
- img.style.cursor = 'pointer';
- img.style.filter = 'invert(.0666)';
- capchaInput.style = 'text-align: center; width: 6em';
- capchaInput.type = 'number';
- capchaInput.min = 0;
- capchaInput.max = 99999;
- document.head.appendChild(document.createElement`style`).textContent = 'input::-webkit-inner-spin-button {display: none}';
- document.querySelector('[type=submit]').style.display = 'none';
- document.getElementById('charsleft').tabIndex = -1;
- /* События */
- img.addEventListener('click', updateCapcha);
- document.forms[0].onsubmit = alternativeSend;
- function updateCapcha(focusOnInput=true) {
- if (focusOnInput) capchaInput.focus();
- img.removeAttribute('src');
- fetch('/feedback')
- .then( response=>response.text() )
- .then( text=>{
- const capchaID = text.match(/(\d+)\.gif/)[1];
- updateCapchaID(capchaID);
- });
- }
- function updateCapchaID(capchaID) {
- if (!capchaID) return;
- document.querySelector('[name=cid]').value = capchaID;
- img.src = '/feedback/'+capchaID+'.gif';
- capchaInput.value = '';
- }
- addEventListener('keypress', e=>{
- if (e.ctrlKey && 'Enter' === e.code)
- document.forms[0].dispatchEvent(new Event('submit'));
- if (document.activeElement.name === 'check' && ('KeyR' === e.code || 114 === e.keyCode || 'r' === e.key.toLowerCase() || 'к' === e.key.toLowerCase())) {
- e.preventDefault();
- updateCapcha();
- }
- });
- function alternativeSend(e) {
- e.preventDefault();
- if (!capchaInput.value) return capchaInput.focus();
- if (!te.value || !checkChmo()) return te.focus();
- const xhr = new XMLHttpRequest();
- xhr.open('POST', '/feedback', true);
- xhr.onreadystatechange = function() {
- if(xhr.readyState === XMLHttpRequest.DONE && xhr.status === 200) {
- let capchaFieldColor = '#dfd'; /* green */
- if ( xhr.responseText.match('Отправлено') ) {
- updateCapcha(false);
- te.value = '';
- te.focus();
- CountLeft(te);
- } else {
- capchaFieldColor = '#fbb'; /* red */
- const capchaID = xhr.responseText.match(/(\d+)\.gif/)[1];
- updateCapchaID(capchaID);
- }
- capchaInput.value = '';
- capchaInput.style.backgroundColor = capchaFieldColor;
- }
- };
- let text = '';
- ['cid', 'left', 'msg', 'check'].forEach(arg=>text+=(text?'&':'')+arg+'='+encodeURIComponent(document.querySelector(`[name=${arg}]`).value));
- xhr.send(text);
- }
Add Comment
Please, Sign In to add comment