-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathImprovedTitleMessage.user.js
More file actions
52 lines (47 loc) · 1.95 KB
/
ImprovedTitleMessage.user.js
File metadata and controls
52 lines (47 loc) · 1.95 KB
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
49
50
51
// ==UserScript==
// @name show chars left in a title
// @namespace https://meta.stackexchange.com/users/158100/rene
// @version 0.1
// @description add chars left / used on a title
// @author rene
// @match *://*.stackexchange.com/questions/ask
// @match *://stackoverflow.com/questions/ask
// @match *://superuser.com/questions/ask
// @match *://servefault.com/questions/ask
// @match *://stackapps.com/questions/ask
// @match *://mathoverflow.net/questions/ask
// @grant none
// ==/UserScript==
(function() {
'use strict';
// where is the title
var targetNode = document.getElementById('title');
// we observe changes, like adding the validation message to the DOM
var observer = new MutationObserver(function(elems, sender) {
var i, elem, splitmsg;
// lets see what we got
for( i = 0; i < elems.length; i = i + 1) {
elem = elems[i];
// we're only in for the validation message
if (elem.type === 'childList' &&
elem.target &&
elem.target.classList &&
elem.target.classList.contains('js-stacks-validation-message') && // the most important check
(elem.target.textContent||'').indexOf('Your title is') === -1 // let's not keep adding our own stuff
) {
// let's keep the first part
splitmsg = (elem.target.textContent || '.').split('.');
// add the title length at the end
elem.target.textContent = splitmsg[0] +
'. Your title is ' +
targetNode.value.length +
' characters';
}
}
});
// we're not interested in attribute changes
var config = { attributes:false, childList: true, subtree: true };
if (targetNode) {
observer.observe(targetNode.parentElement.parentElement, config);
}
})();