-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.js
126 lines (107 loc) · 4.2 KB
/
functions.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
var timeout = null;
var pattern = /([\+\/\*\-]?\(?(-?\$?(?:\d*\.\d{1,2}|\d+))+\)?)+/g;
var timedcheck = function () {
var t = $(this);
clearTimeout(timeout);
timeout = setTimeout(function(){checkitout(t);},1000);
}
function checkitout(t) {
var fromval = true;
var content = t.val();
if (!$.trim(content)){ // think I'm checking for contentEditable here, should actually do that
fromval = false;
content = t.getPreText();
}
console.log(content);
var exps = content.match(pattern);
if (exps) {
console.log(exps);
for (var i=0; i<exps.length; i++){
var el = exps[i];
var dsig = (el.search(/\$/) > -1);
var el_esc = el.replace(/\$/g, "");
//console.log(el_esc);
var ans = math.round(math.eval(el_esc), 2);
if (dsig) {
if (ans < 0){
ans = ans.toString().replace("-","-$");
}else{
ans = "$" + ans;
}
}
content = content.replace(el, ans);
if (fromval){
t.val(content);
}else{
t.html(htmlForTextWithEmbeddedNewlines(content));
}
}
setEndOfContenteditable(t);
}
}
function smartify(){
var extid = chrome.i18n.getMessage("@@extension_id"); //-> background.js?
var classname = "smartbox_" + extid;
var boxes = jQuery("."+classname);
console.log("smartify() functions");
if (boxes.length == 0){
var iframe = jQuery('iframe');
try{
jQuery("textarea,input,[contenteditable]", iframe.contents())
.css('backgroundColor', '#F8F8F8')
.addClass(classname)
.on('keyup', timedcheck);
}catch(err){
console.log("err: "+err);
}
jQuery("textarea,input,[contenteditable]")
.css('backgroundColor', '#F8F8F8')
.addClass(classname)
.on('keyup', timedcheck);
}else{
boxes
.off('keyup', timedcheck)
.removeClass(classname)
.css('backgroundColor', '#FDFDFD');
}
}
//http://stackoverflow.com/questions/1125292
function setEndOfContenteditable($contentEditableElement)
{
if ($contentEditableElement.attr('contenteditable') !== undefined) {
contentEditableElement = $contentEditableElement.get(0);
var range,selection;
if(document.createRange)//Firefox, Chrome, Opera, Safari, IE 9+
{
range = document.createRange();//Create a range (a range is a like the selection but invisible)
range.selectNodeContents(contentEditableElement);//Select the entire contents of the element with the range
range.collapse(false);//collapse the range to the end point. false means collapse to end rather than the start
selection = window.getSelection();//get the selection object (allows you to change selection)
selection.removeAllRanges();//remove any selections already made
selection.addRange(range);//make the range you have just created the visible selection
}
else if(document.selection)//IE 8 and lower
{
range = document.body.createTextRange();//Create a range (a range is a like the selection but invisible)
range.moveToElementText(contentEditableElement);//Select the entire contents of the element with the range
range.collapse(false);//collapse the range to the end point. false means collapse to end rather than the start
range.select();//Select the range (make it the visible selection
}
}
}
//http://stackoverflow.com/questions/4535888
function htmlForTextWithEmbeddedNewlines(text) {
var htmls = [];
var lines = text.split(/\n/);
var tmpDiv = jQuery(document.createElement('div'));
for (var i = 0 ; i < lines.length ; i++) {
htmls.push(tmpDiv.text(lines[i]).html());
}
return htmls.join("<br>");
}
//http://stackoverflow.com/questions/3455931
$.fn.getPreText = function () {
var ce = $("<pre />").html(this.html());
ce.find("div").replaceWith(function() { return "\n" + this.innerHTML; });
return ce.text();
};