Last active
July 30, 2016 15:44
-
-
Save ComFreek/9879093 to your computer and use it in GitHub Desktop.
Additional list toolbar button for StackOverflow editor windows (note: as an answer to the following question: http://softwarerecs.stackexchange.com/q/2833/583)
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 StackOverflow List Button | |
// @description Additional list toolbar button for StackOverflow editor windows | |
// @version 1.0 | |
// | |
// @match *://stackoverflow.com/* | |
// @match *://meta.stackoverflow.com/* | |
// @match *://pt.stackoverflow.com/* | |
// | |
// @match *://serverfault.com/* | |
// @match *://superuser.com/* | |
// @match *://askubuntu.com/* | |
// @match *://stackapps.com/* | |
// @match *://mathoverflow.net/* | |
// | |
// @match *://discuss.area51.stackexchange.com/* | |
// @match *://*.stackexchange.com/* | |
// | |
// @author ComFreek <http://stackoverflow.com/users/603003/comfreek> | |
// @license The same as all contributions on the StackExchange network: http://creativecommons.org/licenses/by-sa/3.0/ | |
// ==/UserScript== | |
(function () { | |
"use strict"; | |
function listify(textarea) { | |
var str = textarea.value; | |
var selection = str.substring(textarea.selectionStart, textarea.selectionEnd); | |
selection = "- " + selection.split("\n").join("\n- "); | |
textarea.value = str.substr(0, textarea.selectionStart) + selection + str.substr(textarea.selectionEnd); | |
} | |
function provideButton() { | |
var listifyBtn = document.createElement("button"); | |
listifyBtn.setAttribute("type", "button"); | |
listifyBtn.appendChild(document.createTextNode("Listify!")); | |
var clickEvtHandler = function () { | |
listify(this.parentNode.querySelector("textarea")); | |
}; | |
// insert our button before the toolbar | |
// inserting it as a toolbar button similar to the ones already provided | |
// by the editor would require too much work and time | |
var editorBtnBars = document.querySelectorAll(".wmd-button-bar"); | |
for (var i=0, len=editorBtnBars.length; i<len; i++) { | |
// Take the original listifyBtn only in the first round | |
var newElem = listifyBtn; | |
if (i > 0) { | |
var newElem = listifyBtn.cloneNode(true); | |
} | |
newElem.addEventListener("click", clickEvtHandler); | |
editorBtnBars[i].parentNode.insertBefore(newElem, editorBtnBars[i]); | |
} | |
} | |
provideButton(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment