Skip to content

Instantly share code, notes, and snippets.

@jeandrek
Forked from hyperobject/tpe-alt.user.js
Last active December 6, 2015 22:18
Show Gist options
  • Save jeandrek/9ae12a6b9d85d32ee596 to your computer and use it in GitHub Desktop.
Save jeandrek/9ae12a6b9d85d32ee596 to your computer and use it in GitHub Desktop.
A remixed remixed TPE userscript
// ==UserScript==
// @name thisandagain Summoner
// @namespace thisandagainPlsExplainThisPost
// @version 1.06
// @description Adds a button to posts to magically summon thisandagain or thisandbatman
// @author Jonathan50
// @match https://scratch.mit.edu/discuss/*
// @grant none
// ==/UserScript==
function tpe_confirm(id) {
var win = open('', '', 'resizeable=0,location=0,width=533,height=241');
win.document.open();
win.document.write('<!DOCTYPE html><html><head><title>Confirm</title><style>body{font-family:sans-serif;color:#FFFFFF;background-image:url(http://i67.tinypic.com/5jw4lk.png);background-size:100% auto;}' +
'</style></head><body><h2>Are you sure you want to summon thisandagain?</h2><p>Remember, please do not use this to spam him.</p>' +
'<p>By clicking OK or Summon thisandbatman, you agree to be responsible for using this.</p><p><button onclick="opener.tpe_send(\'thisandagain\','+id+');window.close()">OK</button> ' +
'<button onclick="opener.tpe_send(\'thisandbatman\', '+id+');window.close()">Summon thisandbatman</button> <button onclick="window.close()">Cancel</button></p></body></html>');
win.document.close();
}
window.tpe_send = function(user, id) {
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/" + id + "/";
var xhr = new XMLHttpRequest();
xhr.open("POST","https://scratch.mit.edu/site-api/comments/user/" + user + "/add/", true);
xhr.setRequestHeader("X-CSRFToken",csrf);
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:""}));
};
(function() {
// 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.class = "plzexplain";
btn.onclick = function() {
tpe_confirm(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.insertBefore(plsexplain,ul.firstChild.nextSibling);
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment