Last active
September 25, 2019 20:29
-
-
Save kms70847/38b97c1d81ddce8cec751e5e47531382 to your computer and use it in GitHub Desktop.
Emoticon Adder Userscript
This file contains hidden or 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 Emoticon Adder | |
// @version 1 | |
// @include http://chat.stackoverflow.com/rooms/* | |
// @include https://chat.stackoverflow.com/rooms/* | |
// @grant none | |
// ==/UserScript== | |
//note: emoticons with slash characters, for example ¯\_(ツ)_/¯, must be quadruple-escaped. Once for javascript, and again for markdown. | |
var choices = ["---", "(◕‿◕)", "(╯°□°)╯︵ ┻━┻", "( ͡° ͜ʖ ͡°)", "ಠ_ಠ", " ¯\\\\_(ツ)_/¯", "👍", "🐍"]; | |
var buttonArea = document.getElementById("chat-buttons"); | |
var select = document.createElement("select"); | |
buttonArea.appendChild(select) | |
for(var choice of choices){ | |
option = document.createElement("option"); | |
option.value = choice; | |
option.innerHTML = choice; | |
select.appendChild(option); | |
} | |
function selected(){ | |
if (select.selectedIndex <= 0){ | |
return; | |
} | |
var choice = choices[select.selectedIndex]; | |
select.selectedIndex = 0; | |
insert_text_at_cursor(choice); | |
} | |
function insert_text_at_cursor(text){ | |
var input = document.getElementById("input"); | |
var left = input.value.slice(0, input.selectionStart); | |
var middle = text; | |
var right = input.value.slice(input.selectionEnd); | |
input.value = left + middle + right; | |
input.setSelectionRange(left.length + middle.length, left.length + middle.length); | |
input.focus(); | |
} | |
select.onchange = selected; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment