var targetText = document.getElementById("commandTarget");
//the target textField id is commandTarget
targetText.addEventListener("dblclick", function(event) { //add dblclick function
var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
.getService(Components.interfaces.nsIWindowMediator);
var contentWindow = wm.getMostRecentWindow('navigator:browser')
.getBrowser().contentWindow;
var contentDocument = contentWindow.document;
var highLightStyle = createStyleSheet(contentDocument,
".__highlight{border:1px solid red}");
var textDiv = contentDocument.createElement("div");
contentDocument.body.appendChild(textDiv);
textDiv.style.border = "1px solid blue";
textDiv.style.display = "none";
textDiv.style.backgroundColor = "#f1f1f1";
textDiv.style.position = "absolute";
textDiv.style.fontSize = "12px";
textDiv.style.height = "16px";
var currElem = null;
function mouseOverListener(event) {
event.preventDefault();
event.stopPropagation();
var elem = event.target;
removeClass(currElem, "__highlight");
addClass(elem, "__highlight");
currElem = elem;
textDiv.style.display = "";
textDiv.style.top = event.clientY + 5 + contentWindow.scrollY;
textDiv.style.left = event.clientX + 5 + contentWindow.scrollX;
var locator = Recorder.get(contentWindow).findLocator(elem);
textDiv.innerHTML = locator;
}
function keyPressListener(event) {
if (event.ctrlKey && event.charCode == 118) {
event.preventDefault();
event.stopPropagation();
targetText.value = textDiv.innerHTML;
clearListener();
}
}
function clearListener() {
contentDocument.body.removeEventListener("mouseover",
mouseOverListener, true);
targetText.removeEventListener("keypress", keyPressListener, true);
targetText.removeEventListener("blur", clearListener, true);
textDiv.parentNode.removeChild(textDiv);
removeClass(currElem, "__highlight");
highLightStyle.parentNode.removeChild(highLightStyle);
}
contentDocument.body.addEventListener("mouseover", mouseOverListener, true);
targetText.addEventListener("keypress", keyPressListener, true);
targetText.addEventListener("blur", clearListener, true);
}, true);
// create a style element use css code
function createStyleSheet(doc, cssText) {
var style = doc.createElement("style");
style.setAttribute("type", "text/css");
style.innerHTML = cssText;
doc.body.appendChild(style);
return style;
}
//add a class to a element
function addClass(elem, className) {
if (elem) {
if (elem.className) {
elem.className = elem.className + " " + className;
} else {
elem.className = className;
}
}
}
//remove a class to a element
function removeClass(elem, className) {
if (elem) {
if (elem.className) {
elem.className = (" " + elem.className + " ").replace(" "
+ className + " ", " ");
} else {
elem.className = className;
}
}
}