Skip to content

Instantly share code, notes, and snippets.

@cuylerstuwe
Last active December 18, 2019 06:01
Show Gist options
  • Select an option

  • Save cuylerstuwe/9976062779584f4e6768f9e7caeaba9d to your computer and use it in GitHub Desktop.

Select an option

Save cuylerstuwe/9976062779584f4e6768f9e7caeaba9d to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name mTurk Dashboard - Last 7 Days' Earnings
// @namespace salembeats
// @version 1.3
// @description UPDATE: Weekly report generation.
// @author Cuyler Stuwe (salembeats)
// @match https://worker.mturk.com/dashboard*
// @grant none
// ==/UserScript==
const weeklyReportTemplate = (
`Greetings from Amazon Mechanical Turk,
The following is a summary of activity for your Mechanical Turk account for the week ending (Mon, Day Year).
Your HIT activity for this week:
- Number of HITs submitted: (NUMBER_OF_HITS_SUBMITTED_WITH_GROUPING)
Approvals and payments that occurred this week:
- Number of HITs approved: (NUMBER_OF_HITS_APPROVED_WITH_GROUPING)
- Number of HITs rejected: (NUMBER_OF_HITS_REJECTED_WITH_GROUPING)
- Total Amount earned this week: (TOTAL_EARNINGS_WITH_FORMATTING)`
);
const sanitizedCellValueText = cell => cell.innerHTML.replace(/<span.+<\/span>/, "").replace(/[^0-9.]/g, "");
const groupedNumber = num => num.toLocaleString("en-US", {useGrouping: true});
const sevenDayHitStatuses = [...document.querySelectorAll(".daily_hit_statuses")]
.slice(0,7);
const sevenDayTotalEarnings = sevenDayHitStatuses
.map(tr => +sanitizedCellValueText(tr.lastElementChild))
.reduce((acc, val) => acc + val);
const sevenDayTotalEarningsFormatted = sevenDayTotalEarnings.toLocaleString("en-US", {style: "currency", currency: "USD"});
const [sevenDayTotalSubmitted, sevenDayTotalApproved, sevenDayTotalRejected] = [1,2,3].map(tdIndex => {
return (
sevenDayHitStatuses
.map(tr => +(sanitizedCellValueText(tr.querySelectorAll("td")[tdIndex])))
.reduce((acc, val) => acc + val)
);
});
const [formattedApprovalPercentage, formattedRejectionPercentage] = [sevenDayTotalApproved, sevenDayTotalRejected].map(numerator => (
[...((numerator/sevenDayTotalSubmitted)*100).toString()].slice(0, 5).join("") + "%"
));
const weeklyReportText = (
weeklyReportTemplate
.replace("(Mon, Day Year)", new Date().toLocaleString("en-US", {month: "short", day: "numeric", year: "numeric"}))
.replace("(NUMBER_OF_HITS_SUBMITTED_WITH_GROUPING)", groupedNumber(sevenDayTotalSubmitted))
.replace("(NUMBER_OF_HITS_APPROVED_WITH_GROUPING)", groupedNumber(sevenDayTotalApproved))
.replace("(NUMBER_OF_HITS_REJECTED_WITH_GROUPING)", groupedNumber(sevenDayTotalRejected))
.replace("(TOTAL_EARNINGS_WITH_FORMATTING)", sevenDayTotalEarningsFormatted)
);
console.log(weeklyReportText);
document.querySelector(".dashboard-hit-stauses-overview")
.insertAdjacentHTML("beforebegin",
`<div style="margin-bottom: 1.0713rem !important">
<strong>7-Day Total Earnings:</strong>
${sevenDayTotalEarningsFormatted}.
<strong>(</strong> ${formattedApprovalPercentage} approved. ${formattedRejectionPercentage} rejected. <strong>)</strong>
<button id="copyWeeklyReport">Copy Weekly Report</button>
</div>`);
document.querySelector("#copyWeeklyReport").addEventListener("click", e => {
navigator.clipboard.writeText(weeklyReportText).then(() => alert("Weekly report copied to clipboard!"));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment