Skip to content

Instantly share code, notes, and snippets.

@kevmo314
Created November 12, 2020 01:31
Show Gist options
  • Save kevmo314/39bcfe7942ffc0f7109d75aa3a735847 to your computer and use it in GitHub Desktop.
Save kevmo314/39bcfe7942ffc0f7109d75aa3a735847 to your computer and use it in GitHub Desktop.
Twitter Canned Replies
// ==UserScript==
// @name Twitter Canned Replies
// @namespace https://www.twitter.com/
// @version 1.0
// @description Adds ability to create/send canned replies to DMs
// @author Kevin Wang <kevmo314@gmail.com>
// @match https://www.tampermonkey.net/index.php?version=4.9.6095&ext=fire&updated=true
// @include https://twitter.com/messages/*
// @require https://ajax.googleapis.com/ajax/libs/jquery/2.1.0/jquery.min.js
// @grant GM.setValue
// @grant GM.getValue
// ==/UserScript==
setInterval(async function() {
const location = window.location.pathname;
if (!location.includes('-')) {
return;
}
const recipientId = location.split('-')[1];
if(new URLSearchParams(window.location.search).get('GMautosend')) {
$("[aria-label='Send']").click();
window.location.href = `https://twitter.com/messages/compose?recipient_id=${recipientId}`;
}
if ($("#canned-replies").length) {
return;
}
const target = $("aside[aria-label='Start a new message']");
if (!target.length) {
return;
}
target.prepend($("<div id='canned-replies' style='color: white; padding: 16px;'>Canned Replies:<div id='canned-reply-options'></div><button id='canned-reply-add'>add</button></div>"))
const value = await GM.getValue("canned-replies", "[]");
console.log(value);
const cans = JSON.parse(value);
for (const can of cans) {
const button = $("<button>" + can.name + "</button>")
$("#canned-reply-options").append(button);
button.click(function() {
const escaped = encodeURIComponent(can.content);
window.location.href = `https://twitter.com/messages/compose?recipient_id=${recipientId}&text=${escaped}&GMautosend=true`
});
}
$("#canned-reply-add").click(function() {
const name = prompt("Enter a name for this canned reply");
const content = $("[data-testid='dmComposerTextInput']").text();
GM.setValue("canned-replies", JSON.stringify([...cans, {name, content}]));
$("#canned-replies").remove();
});
}, 1000);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment