forked from singularityhacker/FiddlePlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
110 lines (105 loc) · 3.31 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
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
var zenFunctionality = function(){
var textarea, zen;
};
zenFunctionality.prototype.createTextArea = function(id){
var textArea = document.createElement("textarea");
textArea.id = 'zenText';
textArea.style.display = 'none';
}
zenFunctionality.prototype.updateTextArea = function(txtArea, string){
txtArea.value = string;
}
zenFunctionality.prototype.getCaretPosition = function(element, zenSet) {
var range= parent.window.$('iframe.html')[0].contentDocument.getSelection().getRangeAt(0);
alert('Current position: '+range.startOffset+' inside '+range.startContainer);
return range.startOffset;
}
function showCaretPos() {
var el = document.getElementById("test");
var caretPosEl = document.getElementById("caretPos");
caretPosEl.innerHTML = "Caret position: " + getCaretCharacterOffsetWithin(el);
}
zenFunctionality.prototype.returnWord = function(text, caretPos) {
var index = text.indexOf(caretPos);
var preText = text.substring(0, caretPos);
if (preText.indexOf(" ") > 0) {
var words = preText.split(" ");
return words[words.length - 1]; //return last word
}
else {
return preText;
}
}
zenFunctionality.prototype.alertPrevWord = function(e) {
var editableDiv = e.target;
var caretPos = this.getCaretPosition(editableDiv);
var word = this.returnWord(editableDiv.innerText, caretPos);
if (word != null) {
alert(word);
}
}
$(function() {
// Adds script to body. Can be seen via inspector
/*var script = document.createElement("script");
script.type = "text/javascript";
script.src = "zen_textarea.min.js";
//document.body.appendChild(script);
*/
var htmlEdit = new zenFunctionality();
console.log(htmlEdit);
var baseExtensionURL = chrome.extension.getURL("js/");
var HTMLIFrame = $("iframe.html")[0].contentDocument;
var CSSIFrame = $("iframe.css")[0].contentDocument;
var JSIFrame = $("iframe.js")[0].contentDocument;
var RESULTIFrame = $("iframe[name='result']")[0].contentDocument;
var isCtrl = false;
$('head').append('');
$(HTMLIFrame.body).keyup(function (e) {
debugger;
e.preventDefault();
if(e.which == 17) isCtrl=false;
}).keydown(function (e) {
debugger;
if(e.which == 17) isCtrl=true;
if(e.which == 69 && isCtrl == true) {
var zenSet="html";
htmlEdit.alertPrevWord(e, zenSet);
isCtrl = false;
e.preventDefault();
return false;
}
});;
$(CSSIFrame.body).keyup(function (e) {
e.preventDefault();
if(e.which == 17) isCtrl=false;
}).keydown(function (e) {
var zenSet = 'css';
e.preventDefault();
htmlEdit.alertPrevWord(e, zenSet);
if(e.which == 17) isCtrl=true;
if(e.which == 69 && isCtrl == true) {
var zenSet="css";
htmlEdit.alertPrevWord(e, zenSet);
isCtrl = false;
e.preventDefault();
return false;
}
});
$(JSIFrame.body).keyup(function (e) {
e.preventDefault();
if(e.which == 17) isCtrl=false;
}).keydown(function (e) {
e.preventDefault();
if(e.which == 17) isCtrl=true;
if(e.which == 69 && isCtrl == true) {
alert('js');
return false;
}
});;
$(RESULTIFrame.body).append('herro');
console.log(HTMLIFrame);
console.log(CSSIFrame);
console.log(JSIFrame);
console.log(RESULTIFrame);
console.log(baseExtensionURL);
});