-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
86 lines (73 loc) · 2.68 KB
/
content.js
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
String.prototype.lpad = function(padString, length) {
var str = this;
while (str.length < length)
str = padString + str;
return str;
};
String.prototype.rpad = function(padString, length) {
var str = this;
while (str.length < length)
str = str + padString;
return str;
};
var id = chrome.i18n.getMessage('@@extension_id') + '-monobar';
/* Capture all context menu events and grab the target */
var editable = null;
document.addEventListener("contextmenu", function(e) {
editable = e.target;
}, true);
/* When the monobar context menu item is selected, get the last context menu
* target and toggle the font face.
*/
chrome.extension.onMessage.addListener(function(message, sender, response) {
if (message == 'monobar-changefont') {
var style = getComputedStyle(editable, '');
var font = style.getPropertyValue('font-family');
if (font && font.search("monospace") >= 0) {
editable.style.setProperty('font-family', 'inherit', 'important');
} else {
editable.style.setProperty('font-family', 'monospace, ' + id, 'important');
}
}
});
function getCaretPosition(node) {
var linenum = 1;
var linestart = -1;
var el = node.get(0);
var content = node.val();
var caret = (el.selectionStart || el.selectionStart == '0')
? el.selectionStart : 0;
var cnt = caret;
while (cnt-- > 0) {
if (content.charAt(cnt) == '\n') {
linenum++;
if (linestart < 0) linestart = cnt;
}
}
var x = node.position().left;
var y = node.outerHeight(true) + node.position().top;
if ($('#' + id).length < 1) {
node.after($('<div></div>')
.attr('id', id)
.css({position: 'absolute',
top: y + 'px',
left: x + 'px',
padding: '0 .5em',
fontFamily: 'monospace',
backgroundColor: 'white',
border: '1px solid gray',
whiteSpace: 'pre'
}));
}
var colnumLabel= ('Col: ' + (caret - linestart)).rpad(' ', 10);
var linenumLabel = ('Ln: ' + linenum).rpad(' ', 10);
var charcountLabel = ('Len: ' + content.length).lpad(' ', 10);
$('#' + id)
.html(linenumLabel + ' ' + colnumLabel + ' ' + charcountLabel)
.css({top: y + 'px', left: x + 'px'})
.show();
}
$('textarea').live('keyup', function() { getCaretPosition($(this)); });
$('textarea').live('click', function() { getCaretPosition($(this)); });
$('textarea')
.live('blur', function() { $('#' + id).remove(); });