Skip to content

Instantly share code, notes, and snippets.

@hyperobject
Created December 2, 2015 18:44
Show Gist options
  • Save hyperobject/5e60342fe7289388364a to your computer and use it in GitHub Desktop.
Save hyperobject/5e60342fe7289388364a to your computer and use it in GitHub Desktop.
A remixed TPE userscript
// ==UserScript==
// @name thisandagain Summoner
// @namespace thisandagainPlsExplainThisPost
// @version 1.01
// @description Adds a button to posts to magically summon thisandagain
// @author tb10
// @match https://scratch.mit.edu/discuss/*
// @grant none
// ==/UserScript==
// the only textarea is the markitup editor which is only shown if the user is logged in
var isloggedin = document.getElementsByTagName("textarea");
if (!isloggedin.length) return;
var posts = document.getElementsByClassName("blockpost roweven firstpost");
if (posts.length) {
for (var p=0,plsexplain,btn,id,ul;p<posts.length;p++){
plsexplain = document.createElement("li");
plsexplain.setAttribute("class","postexplain");
btn = document.createElement("a");
btn.setAttribute("class","plsexplain");
btn.setAttribute("onclick",
'if(confirm("Are you sure you want to summon thisandagain? Remember, please do not use this to spam him.")){'+
'var csrf=document.cookie.match(/scratchcsrftoken=([0-9a-zA-Z]+)/);'+
'if(!csrf)return alert("You need to be logged in.");'+
'csrf=csrf[1];var cmt="pls explain -> https://scratch.mit.edu/discuss/post/"+this.id+"/";'+
'var xhr = new XMLHttpRequest();'+
'xhr.open("POST","https://scratch.mit.edu/site-api/comments/user/thisandagain/add/",true);'+
'xhr.setRequestHeader("X-CSRFToken",csrf),xhr.setRequestHeader("X-Requested-With","XMLHttpRequest");'+
'xhr.onload=function(){alert(xhr.status==200?"thisandagain has been summoned!":"Oops! Cannot do that.");};'+
'xhr.send(JSON.stringify({content:cmt, parent_id:"", commentee_id:""}));}'
);
btn.innerHTML = "thisandagain pls explain";
id = posts[p].id.match(/\d+/)[0];
btn.setAttribute("id",id);
plsexplain.appendChild(btn);
plsexplain.appendChild(document.createTextNode(" | "));
ul = posts[p].getElementsByClassName("postfootright")[0].getElementsByTagName("ul")[0];
//ul.appendChild(plsexplain);
ul.insertBefore(plsexplain,ul.firstChild.nextSibling);
}
}
@jeandrek
Copy link

jeandrek commented Dec 3, 2015

why don't you fork?

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