Skip to content

Instantly share code, notes, and snippets.

@cuylerstuwe
Last active October 2, 2018 20:12
Show Gist options
  • Select an option

  • Save cuylerstuwe/74a86289859d2ca93d3b61051ffa5b3d to your computer and use it in GitHub Desktop.

Select an option

Save cuylerstuwe/74a86289859d2ca93d3b61051ffa5b3d to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Web Panda Ham
// @namespace salembeats
// @version 1.4
// @description Single-GID ham cycler. Speed and low SLOC.
// @author Cuyler Stuwe (salembeats)
// @include https://worker.mturk.com/requesters/pandaHamHandler*
// @include https://worker.mturk.com/requesters/registerPandaHam
// @grant GM_xmlhttpRequest
// @connect worker.mturk.com
// @connect amazon.com
// @connect mturk.com
// ==/UserScript==
const INTERVAL_MS = 1000;
const MAX_MESSAGES = 10;
var gid;
var xhrParams;
var messages = [];
for(let i = 0; i < MAX_MESSAGES; i++) {
messages[i] = "&nbsp";
}
var gidSpan;
var logsDiv;
var responseSpan;
var pingIndex = 0;
var lastPingTime = 0;
function register() {
navigator.registerProtocolHandler(
"web+panda", // This will always be "web+panda".
"https://worker.mturk.com/requesters/pandaHamHandler?url=%s", // %s will contain a URL-encoded version of the entire web+panda://(stuff) URL, including the protocol.
" Web Panda Handler (Ham)"); // User-readable string for the protocol rule.
newMessage(`Look for a prompt or an icon, usually in or around the URL bar, then click "accept", "approve", "allow", or something along those lines.`);
newMessage("This script is trying to register itself as a protocol handler for web+panda:// links, but it needs your approval.");
renderMessages();
}
function ping() {
var now = Math.round(performance.now());
var pingTimeDifference = now - lastPingTime;
const pingMessage = `${pingIndex} Pinging. (${now}) [+${pingTimeDifference}]`;
newMessage(pingMessage);
lastPingTime = now;
renderMessages(logsDiv);
pingIndex++;
GM_xmlhttpRequest({
...xhrParams,
onload: response => {
for(let i = 0; i < messages.length; i++) {
if(messages[i] === pingMessage) {
messages[i] = messages[i] + " " + "<span style='color: gray;'>" + response.responseText + "</span>";
break;
}
}
responseSpan.innerText = response.responseText;
}
});
}
function handle() {
renderMessages(logsDiv);
var parsedLandingPage = new URL(window.location.href);
var parsedWebPandaLink = new URL(parsedLandingPage.searchParams.get("url"));
var gid = parsedWebPandaLink.pathname.replace("//", "");
gidSpan.innerText = gid;
xhrParams = {
method: "GET",
headers: {
Accept: "application/json"
},
url: `https://worker.mturk.com/projects/${gid}/tasks/accept_random`,
onload: function(response) {
responseSpan.innerText = response.responseText;
}
};
setInterval(ping, INTERVAL_MS);
}
function setLayout() {
document.head.innerHTML = "";
document.body.innerHTML = `
<div>GID: <span id="gid"></div>
<div id="logs"></div>
<div>Response <span id="response"></span></div>
<style>
div {
margin: 10px;
}
#gid {
color: green;
}
#response {
color: orange;
}
</style>
`;
gidSpan = document.getElementById("gid");
logsDiv = document.getElementById("logs");
responseSpan = document.getElementById("response");
}
function newMessage(message) {
messages.unshift(message);
while(messages.length > MAX_MESSAGES) {
messages.pop();
}
}
function renderMessages(element) {
if(!element) {element = document.body;}
var messagesHTML = "";
for(const message of messages) {
messagesHTML += `<div>${message}</div>`;
}
element.innerHTML = messagesHTML;
}
function main() {
setLayout();
if(window.location.pathname.includes("registerPandaHam")) {
register();
}
else {
handle();
}
}
main();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment