Skip to content

Instantly share code, notes, and snippets.

@nxn

nxn/dw.tabs.js Secret

Created December 19, 2012 03:25
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 nxn/2be49b49d5f527f870da to your computer and use it in GitHub Desktop.
Save nxn/2be49b49d5f527f870da to your computer and use it in GitHub Desktop.
// ==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)
}
);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment