Skip to content

Instantly share code, notes, and snippets.

@DaveRandom
Last active Jun 6, 2018
Embed
What would you like to do?
Tweet icon for Jeeves in SO chat
// ==UserScript==
// @name Tweet icon for Jeeves in SO chat
// @namespace http://room11.org/
// @version 1.0
// @description @PeeHaa sucks
// @author @DaveRandom
// @match *://chat.stackoverflow.com/rooms/*
// @grant none
// ==/UserScript==
(() => {
'use strict';
const imgSrc = 'https://abs.twimg.com/icons/apple-touch-icon-192x192.png';
const imgStyles = 'width:13px;height:13px;display:inline-block;cursor:pointer';
const maxLength = 280;
const command = '!!tweet';
const input = document.getElementById('input');
const button = document.getElementById('sayit-button');
function processMutations(mutations)
{
mutations.forEach(processMutation);
}
function processMutation(mutation)
{
mutation.addedNodes.forEach((node) => {
if (node.tagName && node.tagName.toLowerCase() === 'li') {
processAddedStarredMessageNode(node);
}
});
}
function processAddedStarredMessageNode(starredMessageNode)
{
const starContainer = starredMessageNode.firstElementChild;
const tmp = document.createElement('div');
let node = starContainer.nextSibling;
while (!node.href || !node.classList || !node.classList.contains('permalink')) {
tmp.appendChild(node.cloneNode(true));
node = node.nextSibling;
}
const href = node.href;
const text = tmp.textContent
.replace(/^\s+/, '')
.replace(/\s*-\s*$/, '')
.replace(/\s+/, ' ');
if (text.length > maxLength) {
return;
}
const question = 'Are you sure you want to tweet this?\n\n' + text;
const img = document.createElement('img');
img.src = imgSrc;
img.setAttribute('style', imgStyles);
img.setAttribute('title', 'Tweet this message with ' + command);
img.addEventListener('click', () => {
if (!confirm(question)) {
return;
}
input.value = command + ' ' + href;
button.dispatchEvent(new MouseEvent('click', {view: window, bubbles: true, cancelable: true}));
});
starredMessageNode.insertBefore(img, starContainer);
}
(new MutationObserver(processMutations)).observe(
document.getElementById('starred-posts'),
{childList: true, subtree: true}
);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment