Skip to content

Instantly share code, notes, and snippets.

@kms70847
Last active August 29, 2015 14:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kms70847/d41ca3432ae4e1bb43a3 to your computer and use it in GitHub Desktop.
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.
// ==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