Last active
August 29, 2015 14:08
-
-
Save kms70847/d41ca3432ae4e1bb43a3 to your computer and use it in GitHub Desktop.
User script which adds a "spoiler" button to the Stack Overflow chat room interface.
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 spoiler button adder (SO Chat) | |
// @namespace about:blank | |
// @include http://chat.stackoverflow.com/rooms/* | |
// @version 1 | |
// @grant none | |
// ==/UserScript== | |
//adds a spoiler to the input textarea. | |
//inserts it wherever your cursor is. | |
//any highlighted text goes inside the spoiler. | |
function add_spoiler(){ | |
var input = document.getElementById("input"); | |
var left = input.value.slice(0, input.selectionStart); | |
var middle = input.value.slice(input.selectionStart, input.selectionEnd); | |
var right = input.value.slice(input.selectionEnd); | |
if (middle.length == 0){ | |
middle = "insert spoiler text here"; | |
} | |
middle = "[hover for spoilers](http://www.example.com \"" + middle + "\")"; | |
input.value = left + middle + right; | |
input.setSelectionRange(left.length + middle.length, left.length + middle.length); | |
input.focus(); | |
} | |
//creates a button that goes next to the input text area. | |
//todo: add title text | |
function create_button(name, id, func){ | |
var buttonArea = document.getElementById("chat-buttons"); | |
var button = document.createElement("button"); | |
button.className = "button"; | |
button.id = id; | |
button.innerHTML = name; | |
button.onclick = func; | |
buttonArea.appendChild(button); | |
} | |
create_button("spoiler", "spoiler-button", add_spoiler); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment