I'm working on a new developer working on a Chrome extension to work as an internal CR tool.
The concept is simple, on the keyboard, the extension receives the word next to the carriage, checks for compliance with the pattern, and if the match is true, the word replaces the canned answer.
For this, I mainly used a modified version of this answer .
I ended up at the checkpoint because it works for the active element, but it doesn't seem to work for things like the create window in Chrome or sequentially through other services (Salesforce also doesn't seem to be like, for example). I thought a little that this could be a problem with iFrames, so I worked a little and changed this world of code:
function getActiveElement(document){ document = document || window.document; if( document.body === document.activeElement || document.activeElement.tagName == 'IFRAME' ){// Check if the active element is in the main web or iframe var iframes = document.getElementsByTagName('iframe');// Get iframes for(var i = 0; i<iframes.length; i++ ){ var focused = getActiveElement( iframes[i].contentWindow.document );// Recall if( focused !== false ){ return focused; // The focused } } } else return document.activeElement; };
(which I originally received from another SO message that I can no longer find). It seems that I was not lucky, like no dice.
Is there an easy way to always activate an active element with an active carriage on every page, even for a Gmail window and similar services, or am I going to get stuck in my own code for a growing list of services that my code cannot take a carriage?
My complete code is here. This is rude, while I'm just trying to get this to work, so I understand there the messy parts that need to be removed:
function AlertPrevWord() { //var text = document.activeElement; //Fetch the active element on the page, cause that where the cursor is. var text = getActiveElement(); console.log(text); var caretPos = text.selectionStart;//get the position of the cursor in the element. var word = ReturnWord(text.value, caretPos);//Get the word before the cursor. if (word != null) {//If it not blank return word //send it back. } } function ReturnWord(text, caretPos) { var index = text.indexOf(caretPos);//get the index of the cursor var preText = text.substring(0, caretPos);//get all the text between the start of the element and the cursor. if (preText.indexOf(" ") > 0) {//if there more then one space character var words = preText.split(" ");//split the words by space return words[words.length - 1]; //return last word } else {//Otherwise, if there no space character return preText;//return the word } } function getActiveElement(document){ document = document || window.document; if( document.body === document.activeElement || document.activeElement.tagName == 'IFRAME' ){// Check if the active element is in the main web or iframe var iframes = document.getElementsByTagName('iframe');// Get iframes for(var i = 0; i<iframes.length; i++ ){ var focused = getActiveElement( iframes[i].contentWindow.document );// Recall if( focused !== false ){ return focused; // The focused } } } else return document.activeElement; };