user1010101 user1010101 - 5 months ago 10
Javascript Question

Parsing an HTML response body string to extract parts

I am receiving a response body via an Ajax get request and in that body I am interested in a particular section which looks like this:

if (w2.isHidden() == false) {
var counterValue = 0;
try {
var url = site + '/user.widget?page=just_data&query=Select IFNULL( time(MAX(longestWaitingSeconds), "unixepoch") , time(0, "unixepoch")) From AvayaCMSAT_skill';
var loader = dhtmlxAjax.getSync(url);
var text = loader.xmlDoc.responseText;
var dataCells = text.split("<cell>");
if (dataCells.length >= 2) {
counterValue = dataCells[1];
counterValue = counterValue.substring(0, counterValue.indexOf("<"));
}
} catch (err) {}
var elem = document.getElementById("counter2_Value");
elem.innerHTML = counterValue;
}

if (w5.isHidden() == false) {
var counterValue = 0;
try {
var url = site + '/user.widget?page=just_data&query=Select SUM(callsWaiting) From AvayaCMSAT_skill';
var loader = dhtmlxAjax.getSync(url);
var text = loader.xmlDoc.responseText;
var dataCells = text.split("<cell>");
if (dataCells.length >= 2) {
counterValue = dataCells[1];
counterValue = counterValue.substring(0, counterValue.indexOf("<"));
}
} catch (err) {}
var elem = document.getElementById("counter5_Value");
elem.innerHTML = counterValue;
}

if (w10.isHidden() == false) {
var counterValue = 0;
try {
var url = site + '/user.widget?page=just_data&query=Select SUM(callsWaiting) From AvayaCMSAT_vdn';
var loader = dhtmlxAjax.getSync(url);
var text = loader.xmlDoc.responseText;
var dataCells = text.split("<cell>");
if (dataCells.length >= 2) {
counterValue = dataCells[1];
counterValue = counterValue.substring(0, counterValue.indexOf("<"));
}
} catch (err) {}
var elem = document.getElementById("counter10_Value");
elem.innerHTML = counterValue;
}

if (w11.isHidden() == false) {
var counterValue = 0;
try {
var url = site + '/user.widget?page=just_data&query=Select IFNULL( time(MAX(longestWaitingSeconds), "unixepoch") , time(0, "unixepoch")) From AvayaCMSAT_vdn';
var loader = dhtmlxAjax.getSync(url);
var text = loader.xmlDoc.responseText;
var dataCells = text.split("<cell>");
if (dataCells.length >= 2) {
counterValue = dataCells[1];
counterValue = counterValue.substring(0, counterValue.indexOf("<"));
}
} catch (err) {}
var elem = document.getElementById("counter11_Value");
elem.innerHTML = counterValue;
}

if (w1.isHidden() == false) {
try {
var windowDims = getWindowStatus(1, 'grid');
var url = site + '/user.widget?page=just_data&query=Select groupName as "Group Name ", groupNumber as "Group Number", callsWaiting as " Calls Waiting ", longestWaiting as "Longest Waiting Call ", acceptableServiceLevel as "Acceptable Service Level", percentInServiceLevel as " Percent Within Service Level", staffed as "Staffed Agents ", available as " Agents Available" From AvayaCMSAT_skill&windowWidth=' + windowDims[2];
var loader = dhtmlxAjax.getSync(url);
if (loader.xmlDoc.responseText.length > 0) {
grid1.parse(loader.xmlDoc.responseText, 'xml');
grid1.loadSortingFromCookie('grid1');
grid1.loadSizeFromCookie('grid1');
}
} catch (err) {}
}

if (w6.isHidden() == false) {
try {
var url = site + '/user.widget?page=just_chart_data&query=SELECT SUM(available) as "Staffed Agents", SUM(staffed - available) as "Unavailable Agents" from AvayaCMSAT_skill&widgetType=pie_medium';
var loader = dhtmlxAjax.getSync(url);
pie_medium6.clearAll();
pie_medium6.parse(loader.xmlDoc.responseText, 'xml');
pie_medium6.refresh();
} catch (err) {}
}

if (w7.isHidden() == false) {
try {
var windowDims = getWindowStatus(7, 'grid');
var url = site + '/user.widget?page=just_data&query=Select agentName as "Agent Name", loginId as "LOGIN ID", agentExt as "Agent Extension", groupName as "Group Name", groupNumber as "Group Number", state as "Agent State", IFNULL( time((statusDuration), "unixepoch") , time(0, "unixepoch")) as"Status Duration" From AvayaCMSAT_agent&windowWidth=' + windowDims[2];
var loader = dhtmlxAjax.getSync(url);
if (loader.xmlDoc.responseText.length > 0) {
grid7.parse(loader.xmlDoc.responseText, 'xml');
grid7.loadSortingFromCookie('grid7');
grid7.loadSizeFromCookie('grid7');
}
}
catch (err) {}
}

if (w8.isHidden() == false) {
try {
var windowDims = getWindowStatus(8, 'grid');
var url = site + '/user.widget?page=just_data&query=Select groupName as "Group Name", groupNumber as "Group Number", SUM(acdCalls) as " Acd Calls ", SUM(extInCalls) as " Extension In Calls ", SUM(extOutCalls) as " Extension Out Calls" From AvayaCMSAT_agent GROUP BY groupName, groupNumber&windowWidth=' + windowDims[2];
var loader = dhtmlxAjax.getSync(url);
if (loader.xmlDoc.responseText.length > 0) {
grid8.parse(loader.xmlDoc.responseText, 'xml');
grid8.loadSortingFromCookie('grid8');
grid8.loadSizeFromCookie('grid8');
}
}
catch (err) {}
}

if (w9.isHidden() == false) {
try {
var windowDims = getWindowStatus(9, 'grid');
var url = site + '/user.widget?page=just_data&query=Select agentName as "Agent Name", loginId as "LOGIN ID", groupName as "Group Name", groupNumber as "Group Number", totalAcdCalls as "Total ACD Calls", avgTalkTime as "Average Talk Time", totalAfterCall as "Total After Call", totalAvailTime as "Total Available Time", totalAuxOthers as"Total Aux Others", extnCalls as "Extension Calls", avgExtnTime as "Average Extension Time", totalTimedStaff as "Total Time Staffed", totalHoldTime as "Total Hold Time" From AvayaCMSAT_agent&windowWidth=' + windowDims[2];
var loader = dhtmlxAjax.getSync(url);
if (loader.xmlDoc.responseText.length > 0) {
grid9.parse(loader.xmlDoc.responseText, 'xml');
grid9.loadSortingFromCookie('grid9');
grid9.loadSizeFromCookie('grid9');
}
}
catch (err) {}
}

if (w3.isHidden() == false) {
try {
var windowDims = getWindowStatus(3, 'grid');
var url = site + '/user.widget?page=just_data&query=Select groupName as "Group Name", groupNumber as "Group Number", percentInServiceLevel as"Percent Within Service Level", totalAcdCalls as "Total Acd Calls", avgSpeedAnswer as "Average Speed Answer", abandonCalls as "Abandoned Calls", avgAbandonTalkTime as "Average Abandon Talk Time", avgTalkTime as "Average Talk Time", totalAfterCall as "Total After Call", flowIn as "Flow In", flowOut as "Flow Out", totalAuxOther as " Total Aux/Other", avgStaff as" Average Staff" From AvayaCMSAT_skill&windowWidth=' + windowDims[2];
var loader = dhtmlxAjax.getSync(url);
if (loader.xmlDoc.responseText.length > 0) {
grid3.parse(loader.xmlDoc.responseText, 'xml');
grid3.loadSortingFromCookie('grid3');
grid3.loadSizeFromCookie('grid3');
}
}
catch (err) {}
}

if (w4.isHidden() == false) {
try {
var url = site + '/user.widget?page=just_data&query=Select 0 AS"Min", ROUND((((SUM(totalAcdCalls * percentInServiceLevel)/100)*1.0)/SUM(totalAcdCalls))*100,2) AS"Cur",100 AS "Max" From AvayaCMSAT_skill';
var loader = dhtmlxAjax.getSync(url);
var text = loader.xmlDoc.responseText;
var dataCells = text.split("<cell>");
if (dataCells.length >= 3) {
var speedoValue = dataCells[2];
speedoValue = speedoValue.substring(0, speedoValue.indexOf("<"));
$('#speedometer4_div').speedometer({
percentage: speedoValue || 0
});
}
}
catch (err) {}
}

if (w12.isHidden() == false) {
try {
var windowDims = getWindowStatus(12, 'grid');
var url = site + '/user.widget?page=just_data&query=Select vdnName as "Vdn Name", extSkill as "Skill/Extension", num as " Vector Number", callsWaiting as "Calls Waiting", longestWaitingCall as "Longest Waiting Call", acdCalls as "Acd Calls", avgSpeedTalk as"Average Speed Talk", abandonedCalls as "Abandoned Calls", avgAbandonTime as "Average Abandon Time", avgTalkHold as "Average Talk Hold", connCalls as "Conference Calls", flowOut as "Flow Out", callBusyDisc as "Call Busy Disconnect", percentInServLevel as "Percent In Service Level" From AvayaCMSAT_vdn WHERE meas !="none"&windowWidth=' + windowDims[2];
var loader = dhtmlxAjax.getSync(url);
if (loader.xmlDoc.responseText.length > 0) {
grid12.parse(loader.xmlDoc.responseText, 'xml');
grid12.loadSortingFromCookie('grid12');
grid12.loadSizeFromCookie('grid12');
}
}
catch (err) {}
}

if (w13.isHidden() == false) {
try {
var windowDims = getWindowStatus(13, 'grid');
var url = site + '/user.widget?page=just_data&query=Select vdnName as "Vdn Name", extSkill as "Skill/Extension", num as " Vector Number", callsOffered as "Calls Offered", totalCalls as "Total Acd Calls", avgSpeedAnswered as "Avg Speed Answered", totalAbandonedCalls as"Aboanded Calls", dayAvgAbandonTalk as "AVG Abandon Talk Time", dayAvgTalkHold as "AVG Talk Hold", totalConnCalls as "Conn Calls", totalFlowOut as "Flow Out", totalCallBusyDisc as "Call Busy Disc", prctInServLevel as "Percent In Service Level" From AvayaCMSAT_vdn WHERE meas !="none"&windowWidth=' + windowDims[2];
var loader = dhtmlxAjax.getSync(url);
if (loader.xmlDoc.responseText.length > 0) {
grid13.parse(loader.xmlDoc.responseText, 'xml');
grid13.loadSortingFromCookie('grid13');
grid13.loadSizeFromCookie('grid13');
}
}
catch (err) {}
}


If you notice in the string above there is a repeating pattern always begining with the following:

if (w[number].isHidden() == false){


I am interested in the stuff that comes after that and before the final closing bracket. So for example in this case:

if (w2.isHidden() == false) {

//PART TO EXTRACT
var counterValue = 0;
try {
var url = site + '/user.widget?page=just_data&query=Select IFNULL(time(MAX(longestWaitingSeconds), "unixepoch") , time(0, "unixepoch"))From vayaCMSAT_skill';
var loader = dhtmlxAjax.getSync(url);
var text = loader.xmlDoc.responseText;
var dataCells = text.split("<cell>");
if (dataCells.length >= 2) {
counterValue = dataCells[1];
counterValue = counterValue.substring(0, counterValue.indexOf("<"));
}
}
catch (err) {}
var elem = document.getElementById("counter2_Value");
elem.innerHTML = counterValue;
//END PART TO EXTRACT
}


As highlighted above I am intrested in extracting the string between the opening and closing {} and putting it in an array. I want to be able to do that for all if statements shown in orignal response body string.

I have looked at using regular expressions and indexOf()/substring(), e.g.:

var start = html.indexOf("false) {", 0) + 8;
var end = html.indexOf("if (w", start);
alert(html.substring(start, end));


But I have not managed to get the string between opening and closing curly brackets.

Answer
var tempWidgetContent = html.match(/w\d+\.isHidden(.*)\(\) == false\)[\s\S]*?catch\(err\)\{ \}/gm);

for (var i = 0; i < tempWidgetContent.length; i++) {
    var widgetContent = tempWidgetContent[i].substring(tempWidgetContent[i].indexOf('{') + 1);
}

This did the trick.