I am developing a Firefox add-on. I need to highlight elements in a webpage using the element's XPath. I am able to highlight it. The problem is the elements are appearing in a distorted way. They are loosing their alignment. Please concentrate on How I am adding display property to parent element that is causing issue for me in the elementHover function.
Please find below is code that I have tried:
$(document).on('mouseenter', 'span[name=element-span]', function() {
var xpath = $(this).next('input').val();
$(this).closest("li").addClass("hovered");
browser.tabs.executeScript(tempTabId, {
code: "var css = '.highlistelement{" + "outline:2px solid #F70B0B !important;" + "border:2px solid red !important;" +
"-webkit-box-shadow:0 0 0 2px #F70B0B !important, 0 0 0 2px #F70B0B !important;" + "}'," +
"head = document.getElementsByTagName('head')[0],style = document.createElement('style');" + "style.type = 'text/css';" +
"if (style.styleSheet){style.styleSheet.cssText = css;} " + "else {style.appendChild(document.createTextNode(css));" + "}head.appendChild(style);"
});
browser.tabs.executeScript(tempTabId, {
code: "var css = '.block{" + "display:inline-block !important;" +"};"
});
browser.tabs.executeScript(tempTabId, {
code: "var css = '.displayBlock{" + "display:inline-block !important;" +"}',"+
"head = document.getElementsByTagName('head')[0],style = document.createElement('style');" + "style.type = 'text/css';" +
"if (style.styleSheet){style.styleSheet.cssText = css;} " + "else {style.appendChild(document.createTextNode(css));" + "}head.appendChild(style);"
});
browser.tabs.executeScript(tempTabId, {
code: elementHover(xpath, "highlistelement","displayBlock","block")
});
});
function elementHover(xpath, highlightClass,displayClass,blockClass) {
var str = 'var path="' + xpath + '";' +
'var iterator = document.evaluate(path, document, null, XPathResult.UNORDERED_NODE_ITERATOR_TYPE, null); ' +
'try {' +
'var thisNode = iterator.iterateNext();' +
'while (thisNode) {' +
'thisNode.classList.add("' + highlightClass + '");' +
'pnode=thisNode.parentNode;'+
' while(pnode){'+
'if(pnode.style.getPropertyValue("display")==="none")'+
'{'+
'pnode.classList.add("'+blockClass+'");' +
'}' +
'else if(pnode.style.getPropertyValue("display")==="block"){' +
'pnode.style.display="block"' +
'}'+
'else if(pnode.style.getPropertyValue("display")==="inline-block"){' +
'pnode.style.display="inline-block"' +
'}'+
'else if(pnode.style.getPropertyValue("display")==="flex"){' +
'pnode.style.display="flex"' +
'}'+
'else if(pnode.style.getPropertyValue("display")==="inline"){' +
'pnode.style.display="inline"' +
'}'+
'else if(pnode.style.getPropertyValue("display")==="inline-flex"){' +
'pnode.style.display="inline-flex"' +
'}'+
'else if(pnode.style.getPropertyValue("display")==="inline-table"){' +
'pnode.style.display="inline-table"' +
'}'+
'else if(pnode.style.getPropertyValue("display")==="list-item"){' +
'pnode.style.display="list-item"' +
'}'+
'else if(pnode.style.getPropertyValue("display")==="run-in"){' +
'pnode.style.display="run-in"' +
'}'+
'else if(pnode.style.getPropertyValue("display")==="table"){' +
'pnode.style.display="table"' +
'}'+
'else if(pnode.style.getPropertyValue("display")==="table-caption"){' +
'pnode.style.display="table-caption"' +
'}'+
'else if(pnode.style.getPropertyValue("display")==="table-column-group"){' +
'pnode.style.display="table-column-group"' +
'}'+
'else if(pnode.style.getPropertyValue("display")==="table-header-group"){' +
'pnode.style.display="table-header-group"' +
'}'+
'else if(pnode.style.getPropertyValue("display")==="table-footer-group"){' +
'pnode.style.display="table-header-group"' +
'}'+
'else if(pnode.style.getPropertyValue("display")==="table-row-group"){' +
'pnode.style.display="table-row-group"' +
'}'+
'else if(pnode.style.getPropertyValue("display")==="table-cell"){' +
'pnode.style.display="table-cell"' +
'}'+
'else if(pnode.style.getPropertyValue("display")==="table-column"){' +
'pnode.style.display="table-cell"' +
'}'+
'else if(pnode.style.getPropertyValue("display")==="table-row"){' +
'pnode.style.display="table-row"' +
'}'+
'else if(pnode.style.getPropertyValue("display")==="initial"){' +
'pnode.style.display="initial"' +
'}'+
'else if(pnode.style.getPropertyValue("display")==="inherit"){' +
'pnode.style.display="inherit"' +
'}'+
'else if(pnode.style.getPropertyValue("display")==="")' +
'{'+
'pnode.classList.add("'+displayClass+'");' +
'var evObj = document.createEvent("Events");'+
' evObj.initEvent("mouseover", true, false);'+
'pnode.dispatchEvent(evObj);'+
'}else if(pnode.style.getPropertyValue("visibility")==="hidden")'+
'{'+
'pnode.style.visibility = "visible";' +
'}' +
'thisNode.scrollIntoView(true);' +
'pnode=pnode.parentNode;'+
'}' +
'thisNode = iterator.iterateNext();' +
'}' +
'} catch (e) {' +
'}';
return str;
}
Please find the sample image below to understand the issue:
Please observe the image. When I highlight the "Gmail" text, the remaining two links are moved to next line.