var targetValue = "";
var rv = false;
function passTargetValue(cmd){
try{
// getTarget() returns all available element Target with a " ; " delimiter
let targetStr = getTarget(cmd); // example targetstr = id=123;name=abc;link=ggle;css=#123;xpath=//html/body/div
let commandTargetStr = targetStr.split(";");
if(commandTargetStr.length === 1){
targetValue = commandTargetStr;
ctri_log.info("targetValue open: " + targetValue );
return;
}
//iterate through all available Targets (for ex: ID, NAME, Link, CSS, XPath)
for (var i = 0; i < commandTargetStr.length; i++) {
tv = commandTargetStr[i];
ctri_log.info("targetValue: " + tv );
// Passing Target to locate element present or not.
selectValidTarget(tv);
ctri_log.info("rv = " +rv);
if (rv){
targetValue = tv;
return;
}else{
passTargetValue(cmd);
}
}
}
catch(error) {
if(error.message == "Could not establish connection. Receiving end does not exist.") {
sendPreparedNextFrame(infos);
} else {
ctri_log.error("Unknown error");
}
}
}
function selectValidTarget(tv){
try{
browser.tabs.query({
active: true,
windowId: contentWindowId
}).then(function(tabs) {
if (tabs.length === 0) {
console.log("No match tabs");
} else {
browser.webNavigation.getAllFrames({tabId: tabs[0].id})
.then(function(framesInfo){
var frameIds = [];
for (let i = 0; i < framesInfo.length; i++) {
frameIds.push(framesInfo[i].frameId)
}
frameIds.sort();
var infos = {
"index": 0,
"tabId": tabs[0].id,
"frameIds": frameIds,
"targetValue": tv
};
sendShowElementMessage(infos);
});
}
});
} catch (e) {
console.error(e);
}
}
function validateTarget(infos){
browser.tabs.sendMessage(infos.tabId, {
showElement: true,
targetValue: infos.targetValue
}, {
frameId: infos.frameIds[infos.index]
}).then(function(response) {
if (response){
if (!response.result) {
sendPreparedNextFrame(infos);
} else {
let text = infos.index == 0 ? "top" : index.toString() + "(id)";
ctri_log.info("Element is found in " + text + " frame.");
rv = true;
}
}
}).catch(function(error) {
if(error.message == "Could not establish connection. Receiving end does not exist.") {
sendPreparedNextFrame(infos);
} else {
ctri_log.error("Unknown error");
}
});
}
function sendPreparedNextFrame(infos) {
if (infos.index == infos.frameIds.length) {
ctri_log.error("Element is not found.");
rv = false;
} else {
infos.index++;
validateTarget(infos);
}
}
function doCommand() {
Command = Command;
Target = passTargetValue(cmd);
Value = value;
// Executes the command line --> input --> (Command , Target , Value)
}