Last active
June 6, 2018 09:36
-
-
Save DaveRandom/f5f30606500fb014d734c103c6948fbe to your computer and use it in GitHub Desktop.
Tweet icon for Jeeves in SO chat
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==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