Tumblr Multiline Asks

Allows you to send multiline asks on Tumblr.

  1. // ==UserScript==
  2. // @name Tumblr Multiline Asks
  3. // @namespace the.vindicar.scripts
  4. // @description Allows you to send multiline asks on Tumblr.
  5. // @version 1.1.1
  6. // @grant none
  7. // @include http://www.tumblr.com/ask_form/*
  8. // @include https://www.tumblr.com/ask_form/*
  9. // ==/UserScript==
  10.  
  11. (function(){
  12. //We're on ask page, where code is old and ugly. We have to get our hands dirty...
  13. // First of all, let's check if we have actual ask form here.
  14. var ask_form = document.getElementById('ask_form');
  15. if (!ask_form)
  16. //We're looking at some message, like "your question has been received" or "Wait for an hour before asking more questions". Nothing to do here
  17. return;
  18. //We have an actual ask form. Leeeeeeeets do eeeet!
  19. // Begone, foul event handler that eats up our line breaks!
  20. document.removeEventListener('keydown', linebreaks, false);
  21. //There is a setInterval'd script that strips line breaks from the text.
  22. // Little dirty hack: original script caches the references to used nodes. Thus, if we replace those with clones, the script will be checking unused nodes.
  23. function replaceNode(nodeid) {
  24. var original = document.getElementById(nodeid);
  25. if (original) {
  26. var clone = original.cloneNode(true);
  27. original.parentNode.replaceChild(clone, original);
  28. original.setAttribute('id', 'former_'+nodeid);
  29. return clone;
  30. } else {
  31. return null;
  32. }
  33. }
  34. var q = replaceNode('question');
  35. var ask_button = replaceNode('ask_button');
  36. var characters_remaining = replaceNode('characters_remaining');
  37. var character_counter = replaceNode('character_counter');
  38. // Sadly, this trick also breaks useful parts. We need to recreate those manually. I've copied the code from Tumblr original JS.
  39. // This script also does useful things, so I copied it from Tumblr JS, snipping out linebreak part.
  40. setInterval(function() {
  41. // Character limit.
  42. if (q.value.length > 500) q.value = q.value.substring(0, 500);
  43.  
  44. // Enable ask button.
  45. if(q.value.length > 0) {
  46. ask_button.disabled = false;
  47. } else {
  48. ask_button.disabled = true;
  49. }
  50. var remaining = 500 - q.value.length;
  51. characters_remaining.innerHTML = remaining;
  52.  
  53. // Update character counter.
  54. if (remaining <= 120) {
  55. character_counter.style.display = 'block';
  56. if(remaining > 100) {
  57. character_counter.style.color = '#444';
  58. } else if (remaining > 80 && remaining <= 100) {
  59. character_counter.style.color = '#5f3b3e';
  60. } else if (remaining > 60 && remaining <= 80) {
  61. character_counter.style.color = '#7b3239';
  62. } else if (remaining > 40 && remaining <= 60) {
  63. character_counter.style.color = '#962a33';
  64. } else if (remaining > 20 && remaining <= 40) {
  65. character_counter.style.color = '#b2212e';
  66. } else {
  67. character_counter.style.color = '#cd1828';
  68. }
  69. } else {
  70. character_counter.style.display = 'none';
  71. }
  72. }, 100);
  73. // old validation handler still remembers replaced nodes. We have to remove it first...
  74. ask_form.removeEventListener('submit', validate, false);
  75. //...then create our own, working with clones...
  76. validate = function (e) {
  77. if (!q.value) {
  78. alert('You need to enter a question!');
  79. stopEvent(e);
  80. return false;
  81. }
  82. if (
  83. q.value.match(/http:\/\//i) ||
  84. q.value.match(/www\./i) ||
  85. q.value.match(/[a-zA-Z0-9]+(\.arpa|\.root|\.aero|\.biz|\.cat|\.com|\.coop|\.edu|\.gov|\.info|\.int|\.jobs|\.mil|\.mobi|\.museum|\.name|\.net|\.org|\.pro|\.travel|TLD|\.ac|\.ad|\.ae|\.af|\.ag|\.ai|\.al|\.am|\.an|\.ao|\.aq|\.ar|\.as|\.at|\.au|\.aw|\.ax|\.az|\.ba|\.bb|\.bd|\.be|\.bf|\.bg|\.bh|\.bi|\.bj|\.bm|\.bn|\.bo|\.br|\.bs|\.bt|\.bv|\.bw|\.by|\.bz|\.ca|\.cc|\.cd|\.cf|\.cg|\.ch|\.ci|\.ck|\.cl|\.cm|\.cn|\.co|\.cr|\.cu|\.cv|\.cx|\.cy|\.cz|\.de|\.dj|\.dk|\.dm|\.do|\.dz|\.ec|\.ee|\.eg|\.er|\.es|\.et|\.eu|\.fi|\.fj|\.fk|\.fm|\.fo|\.fr|\.ga|\.gb|\.gd|\.ge|\.gf|\.gg|\.gh|\.gi|\.gl|\.gm|\.gn|\.gp|\.gq|\.gr|\.gs|\.gt|\.gu|\.gw|\.gy|\.hk|\.hm|\.hn|\.hr|\.ht|\.hu|\.id|\.ie|\.il|\.im|\.in|\.io|\.iq|\.ir|\.is|\.it|\.je|\.jm|\.jo|\.jp|\.ke|\.kg|\.kh|\.ki|\.km|\.kn|\.kr|\.kw|\.ky|\.kz|\.la|\.lb|\.lc|\.li|\.lk|\.lr|\.ls|\.lt|\.lu|\.lv|\.ly|\.ma|\.mc|\.md|\.mg|\.mh|\.mk|\.ml|\.mm|\.mn|\.mo|\.mp|\.mq|\.mr|\.ms|\.mt|\.mu|\.mv|\.mw|\.mx|\.my|\.mz|\.na|\.nc|\.ne|\.nf|\.ng|\.ni|\.nl|\.no|\.np|\.nr|\.nu|\.nz|\.om|\.pa|\.pe|\.pf|\.pg|\.ph|\.pk|\.pl|\.pm|\.pn|\.pr|\.ps|\.pt|\.pw|\.py|\.qa|\.re|\.ro|\.ru|\.rw|\.sa|\.sb|\.sc|\.sd|\.se|\.sg|\.sh|\.si|\.sj|\.sk|\.sl|\.sm|\.sn|\.so|\.sr|\.st|\.su|\.sv|\.sy|\.sz|\.tc|\.td|\.tf|\.tg|\.th|\.tj|\.tk|\.tl|\.tm|\.tn|\.to|\.tp|\.tr|\.tt|\.tv|\.tw|\.tz|\.ua|\.ug|\.uk|\.um|\.us|\.uy|\.uz|\.va|\.vc|\.ve|\.vg|\.vi|\.vn|\.vu|\.wf|\.ws|\.ye|\.yt|\.yu|\.za|\.zm|\.zw)/i)
  86. ) {
  87. alert('Sorry, but please don\'t include links in questions.');
  88. stopEvent(e);
  89. return false;
  90. }
  91. ask_button.disabled = true;
  92. }
  93. //...and set it in place
  94. ask_form.addEventListener('submit', validate, false);
  95. })();