secret
Last active

  • Download Gist
dw.tabs.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
// ==UserScript==
// @name dw.tabs
// @namespace http://nxn.io/
// @version 0.1
// @description enter something awful
// @match http://*.doomworld.com/vb/*
// @copyright 2012+, Nixon
// ==/UserScript==
 
(function() {
var root = window;
if (typeof unsafeWindow === 'object') {
root = unsafeWindow;
}
var m = root.document.querySelector('textarea[name=message]')
, f = root.document.querySelector('form[name=vbform]')
, tab = '      '
, tab_v = '[tab]'
 
if (!m) return;
m.addEventListener(
'keydown',
function(e) {
if (e.keyCode !== 9) return;
var cursorPos = this.selectionStart
, start = this.value.substring(0, cursorPos)
, end = this.value.substring(cursorPos);
this.value = start + tab_v + end;
this.setSelectionRange(cursorPos + tab_v.length, cursorPos + tab_v.length);
e.preventDefault();
}
);
m.value = m.value.replace(new RegExp(tab, 'gim'), tab_v);
 
if (!f) return;
f.addEventListener(
'submit',
function(e) {
m.value = m.value.replace(/\[tab\]/gim, tab)
}
);
})();

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.