Skip to content

Instantly share code, notes, and snippets.

@MegaApuTurkUltra
Forked from Zro617/scratch-messenger.user.js
Last active January 24, 2016 18:32
Show Gist options
  • Save MegaApuTurkUltra/d78fbc31974359c698f3 to your computer and use it in GitHub Desktop.
Save MegaApuTurkUltra/d78fbc31974359c698f3 to your computer and use it in GitHub Desktop.
Adds "Message" control on Scratch forums; allows arbitrary length messages
// ==UserScript==
// @name Scratch Messenger
// @namespace ScratchMessenger
// @author Zro617, MegaApuTurkUltra
// @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.0
// @grant none
// ==/UserScript==
var isloggedin = document.getElementsByTagName("textarea");
if(!isloggedin.length) return;
function chunkString(str, length) {
return str.match(new RegExp('.{1,' + length + '}', 'g'));
}
var posts = document.getElementsByClassName("blockpost roweven firstpost"),
users = document.getElementsByClassName("black username");
if (posts.length) {
for (var p=0,msgauthor,btn,id,ul;p<posts.length;p++){
msgauthor = document.createElement("li");
msgauthor.setAttribute("class","message-author");
btn = document.createElement("a");
btn.setAttribute("id",users[p].innerHTML);
//btn.setAttribute("id",posts[p].id.match(/\d+/)[0]);
btn.innerHTML = "Message " + users[p].innerHTML;
btn.onclick = function() {
var status = document.createElement("span");
btn.appendChild(status);
var csrf=document.cookie.match(/scratchcsrftoken=([0-9a-zA-Z]+)/);
if(!csrf)return;
csrf=csrf[1];
var user = this.id;
var comment = prompt("Write your comment to "+this.id+".");
if(comment && comment.length){
comment = chunkString(comment, 500);
var xhr = new XMLHttpRequest();
console.log("Posting part 0");
status.textContent = " (posting...)";
xhr.responseType = 'document';
xhr.open("POST","https://scratch.mit.edu/site-api/comments/user/" + user + "/add/",true);
xhr.setRequestHeader("X-CSRFToken", csrf);
xhr.setRequestHeader("X-Requested-With","XMLHttpRequest");
xhr.onload=function(){
if(this.status != 200){
alert("Sorry, your comment couldn't be posted");
status.parentNode.removeChild(status);
return;
}
var id = this.responseXML.getElementsByClassName("comment")[0].getAttribute("data-comment-id");
console.log("Got id: " + id);
var i = 1;
function next(){
if(i >= comment.length){
alert("Message posted!");
status.parentNode.removeChild(status);
return;
}
console.log("Posting part " + i);
status.textContent = " (posting " + i + "/" + comment.length + ")";
var req = new XMLHttpRequest();
req.open("POST","https://scratch.mit.edu/site-api/comments/user/" + user + "/add/",true);
req.setRequestHeader("X-CSRFToken", csrf);
req.setRequestHeader("X-Requested-With","XMLHttpRequest");
req.onload = function(){
if(this.status != 200){
alert("Sorry, your comment couldn't be posted");
status.parentNode.removeChild(status);
return;
}
if(this.responseText.indexOf("isFlood") > -1){
// uh oh, spam filter
// retry soon
status.textContent = " (" + i + "/" + comment.length + " | waiting for spam filter)";
setTimeout(next, 10000);
return;
}
i++;
setTimeout(next, 2000);
};
req.send(JSON.stringify({content:comment[i], parent_id:id, commentee_id:""}));
}
next();
};
xhr.send(JSON.stringify({content:comment[0], parent_id:"", commentee_id:""}));
}
};
msgauthor.appendChild(btn);
msgauthor.appendChild(document.createTextNode(" | "));
ul = posts[p].getElementsByClassName("postfootright")[0].getElementsByTagName("ul")[0];
ul.insertBefore(msgauthor,ul.firstChild.nextSibling);
}
}
@MegaApuTurkUltra
Copy link
Author

@Zro617 Lol I forgot 200 characters wasn't the limit. I should change it to 500

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