Skip to content

Instantly share code, notes, and snippets.

@Zro617
Last active January 24, 2016 21:52
Show Gist options
  • Save Zro617/f705f3c9e810b14b75c3 to your computer and use it in GitHub Desktop.
Save Zro617/f705f3c9e810b14b75c3 to your computer and use it in GitHub Desktop.
Directly message the author of a Scratch post
// ==UserScript==
// @name Scratch Messenger
// @namespace ScratchMessenger
// @author Zro617
// @description Lets you quickly write a message directly to the author of a forum post on Scratch
// @include https://scratch.mit.edu/discuss/topic/*
// @version 1.1.1
// @grant none
// ==/UserScript==
/*
VERSION HISTORY
1.0 - Released
1.1 - Lifted character maximum, split large message into chunks that attach to the parent chunk, added spam prevention
1.1.1 - Oops, reset character partition limit to 500
*/
function sendComment(user, csrf, comment, parentid, success, fail) {
var xhr = new XMLHttpRequest();
xhr.open('POST', 'https://scratch.mit.edu/site-api/comments/user/' + user + '/add/', true);
xhr.responseType = 'document';
xhr.setRequestHeader('X-CSRFToken', csrf),
xhr.setRequestHeader('X-Requested-With', 'XMLHttpRequest');
xhr.onload = function () {
this.status == 200 ? success(xhr) : fail(xhr);
};
xhr.send(JSON.stringify({
content: comment,
parent_id: parentid,
commentee_id: ''
}));
}
function getCSRF() {
return document.cookie.match(/scratchcsrftoken=([0-9a-zA-Z]+)/);
}
function getPosts() {
return document.getElementsByClassName('blockpost roweven firstpost');
}
function getUsers() {
return document.getElementsByClassName('black username');
}
function chunkify(comment, length) {
var commentChunks = [];
if (comment.length > length) {
for (var i = 0, chk; i < comment.length; ) {
if (i < comment.length - length) {
chk = comment.substring(i, i + length - 3) + '...';
i += length - 3;
} else {
chk = comment.substring(i);
i += length;
}
commentChunks.push(chk);
}
} else {
commentChunks.push(comment);
}
return commentChunks;
}
if (!getCSRF()) return;
var posts = getPosts(), users = getUsers();
if (!posts.length) return;
for (var p = 0, msgauthor, btn, user, ul; p < posts.length; p++) {
user = users[p].innerHTML;
msgauthor = document.createElement('li');
msgauthor.setAttribute('class', 'message-author');
btn = document.createElement('a');
btn.setAttribute('id', user);
//btn.setAttribute("id",posts[p].id.match(/\d+/)[0]);
btn.innerHTML = 'Message ' + user;
btn.onclick = function () {
if (this.innerHTML != 'Message '+this.id) return alert("Please wait 30 seconds before messaging again"); // avoid spamming
var clicked = this,
user = this.id,
csrf = getCSRF()[1],
parentid = "",
chunk = 0,
comment = prompt('Write your message to ' + user);
if (!comment) return;
comment = chunkify(comment, 500);
clicked.innerHTML = "Posting...";
function sendChunk() {
console.log('Sending chunk '+chunk+'...');
sendComment(user, csrf, comment[chunk], parentid, function(x) {
if (parentid == ""){
parentid = x.responseXML.getElementsByClassName('comment')[0].getAttribute('data-comment-id'); // can't user querySelector, seriously?!?!
console.log('Comment ID retrieved: '+parentid);
}
console.log('Chunk '+chunk+' of comment sent.');
if (chunk == comment.length-1) {
clicked.innerHTML = "Message sent!";
window.setTimeout(function(){clicked.innerHTML='Message '+user;},30000); // wait 30 seconds before messaging same user
alert("Message successfully sent!");
return;
}
chunk++;
window.setTimeout(sendChunk,3000); // avoid triggering spam filter
}, function(x) { alert("Oops! Something went wrong...") }
);
}
sendChunk();
};
msgauthor.appendChild(btn);
msgauthor.appendChild(document.createTextNode(' | '));
ul = posts[p].getElementsByClassName('postfootright') [0].getElementsByTagName('ul') [0];
ul.insertBefore(msgauthor, ul.firstChild.nextSibling);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment