Skip to content

Commit

Permalink
Refactor (#11) and fixbug on some website (#4)
Browse files Browse the repository at this point in the history
  • Loading branch information
kimkha committed Apr 10, 2015
1 parent c454e81 commit 127af69
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 117 deletions.
117 changes: 0 additions & 117 deletions src/scripts/avim.js
Original file line number Diff line number Diff line change
Expand Up @@ -1036,15 +1036,6 @@ function ifMoz(e) {
}
}

/*function FKeyPress() {
var obj = findFrame();
AVIMObj.sk = fcc(obj.event.keyCode);
if(checkCode(obj.event.keyCode) || (obj.event.ctrlKey && (obj.event.keyCode != 92) && (obj.event.keyCode != 126))) {
return;
}
start(obj, AVIMObj.sk);
}/**/

function checkCode(code) {
if(((AVIMGlobalConfig.onOff === 0) || ((code < 45) && (code != 42) && (code != 32) && (code != 39) && (code != 40) && (code != 43)) || (code == 145) || (code == 255))) {
return true;
Expand Down Expand Up @@ -1086,114 +1077,6 @@ function findIgnore(el) {
return false;
}

function findFrame() {
for(var i = 0; i < AVIMObj.fID.length; i++) {
if(findIgnore(AVIMObj.fID[i])) return;
AVIMObj.frame = AVIMObj.fID[i];
if(typeof(AVIMObj.frame) != "undefined") {
try {
if (AVIMObj.frame.contentWindow.document && AVIMObj.frame.contentWindow.event) {
return AVIMObj.frame.contentWindow;
}
} catch(e) {
if (AVIMObj.frame.document && AVIMObj.frame.event) {
return AVIMObj.frame;
}
}
}
}
}

function _keyPressHandler(e) {
if(!AVIMObj.support) {
return;
}

var el = e.target, code = e.which;
if(e.ctrlKey) {
return;
}
if(e.altKey && (code != 92) && (code != 126)) {
return;
}
if((el.type != 'textarea') && (el.type != 'text')) {
if (el.isContentEditable) {
ifMoz(e);
}
return;
}
if (checkCode(code)) {
return;
}
AVIMObj.sk = fcc(code);
if(findIgnore(el)) {
return;
}
start(el, e);
if(AVIMObj.changed) {
AVIMObj.changed = false;
e.preventDefault();
return false;
}
return;
}

function _keyUpHandler(evt) {
var code = evt.which;

// Press Ctrl twice to off/on AVIM
if (code == 17) {
if (AVIMObj.isPressCtrl) {
AVIMObj.isPressCtrl = false;
sendRequest({'turn_avim':'onOff'}, configAVIM);
} else {
AVIMObj.isPressCtrl = true;
// Must press twice in 300ms
setTimeout(function(){
AVIMObj.isPressCtrl = false;
}, 300);
}
} else {
AVIMObj.isPressCtrl = false;
}
}

function _keyDownHandler(evt) {
var key;
if(evt == "iframe") {
AVIMObj.frame = findFrame();
key = AVIMObj.frame.event.keyCode;
} else {
key = evt.which;
}
}

function keyUpHandler(evt) {
_keyUpHandler(evt);
console.log("keyUpHandler");
}

function keyDownHandler(evt) {
_keyDownHandler(evt);
console.log("keyDownHandler");
}

function keyPressHandler(evt) {
var success = _keyPressHandler(evt);
if (success === false) {
evt.preventDefault();
}
console.log("keyPressHandler");
}

function attachEvt(obj, evt, handle, capture) {
obj.addEventListener(evt, handle, capture);
}

function removeEvt(obj, evt, handle, capture) {
obj.removeEventListener(evt, handle, capture);
}

function AVIMInit(AVIM, isAttach) {
/*if(AVIM.support) {
AVIM.fID = document.getElementsByTagName("iframe");
Expand Down
110 changes: 110 additions & 0 deletions src/scripts/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,116 @@ var extension = chrome.extension;
var document = window.document;
var sendRequest = extension.sendMessage;

var inputTypes = ["textarea", "text", "search", "tel"];

function findFrame() {
for(var i = 0; i < AVIMObj.fID.length; i++) {
if(findIgnore(AVIMObj.fID[i])) return;
AVIMObj.frame = AVIMObj.fID[i];
if(typeof(AVIMObj.frame) != "undefined") {
try {
if (AVIMObj.frame.contentWindow.document && AVIMObj.frame.contentWindow.event) {
return AVIMObj.frame.contentWindow;
}
} catch(e) {
if (AVIMObj.frame.document && AVIMObj.frame.event) {
return AVIMObj.frame;
}
}
}
}
}

function _keyPressHandler(e) {
if(!AVIMObj.support) {
return;
}

var el = e.target, code = e.which;
if(e.ctrlKey) {
return;
}
if(e.altKey && (code != 92) && (code != 126)) {
return;
}
if(inputTypes.indexOf(el.type) < 0) {// Not contains in list of input types
if (el.isContentEditable) {
ifMoz(e);
}
return;
}
if (checkCode(code)) {
return;
}
AVIMObj.sk = fcc(code);
if(findIgnore(el)) {
return;
}
start(el, e);
if(AVIMObj.changed) {
AVIMObj.changed = false;
e.preventDefault();
return false;
}
return;
}

function _keyUpHandler(evt) {
var code = evt.which;

// Press Ctrl twice to off/on AVIM
if (code == 17) {
if (AVIMObj.isPressCtrl) {
AVIMObj.isPressCtrl = false;
sendRequest({'turn_avim':'onOff'}, configAVIM);
} else {
AVIMObj.isPressCtrl = true;
// Must press twice in 300ms
setTimeout(function(){
AVIMObj.isPressCtrl = false;
}, 300);
}
} else {
AVIMObj.isPressCtrl = false;
}
}

function _keyDownHandler(evt) {
var key;
if(evt == "iframe") {
AVIMObj.frame = findFrame();
key = AVIMObj.frame.event.keyCode;
} else {
key = evt.which;
}
}

function keyUpHandler(evt) {
_keyUpHandler(evt);
console.log("keyUpHandler");
}

function keyDownHandler(evt) {
_keyDownHandler(evt);
console.log("keyDownHandler");
}

function keyPressHandler(evt) {
var success = _keyPressHandler(evt);
if (success === false) {
evt.preventDefault();
}
console.log("keyPressHandler");
}

function attachEvt(obj, evt, handle, capture) {
obj.addEventListener(evt, handle, capture);
}

function removeEvt(obj, evt, handle, capture) {
obj.removeEventListener(evt, handle, capture);
}

function removeOldAVIM() {
removeEvt(document, "mouseup", AVIMAJAXFix, false);
removeEvt(document, "keydown", keyDownHandler, true);
Expand Down

0 comments on commit 127af69

Please sign in to comment.