Тема: JS: Совместная разработка - ищу единомышленников, знающих JScript
Всем привет!
Подскажите, присутствуют ли гуры JScript на форуме?
Имеются весьма базовые навыки по этому движку, и есть желание разработать класс-обертку для chromedriver.
Сам класс вроде как начал, но в процессе утыкаюсь в ограничение собственный знаний синткасиса, приемов, фишек...
Цель: на выходе получить скрипт используемый как самостоятельно, так и подключаемый в OLE->ScriptControl (главная фишка, ради которой весь замут))).
Есть желающие поучавствовать в совместной разработке (или просто дать подсказок)?
Вот что набросал (exe-шник драйвера покамест запускается вручную, но в дальнейшем планируется автомат):
// json2js:
this.JSON||(this.JSON={}),function(){function f(t){return t<10?"0"+t:t}"function"!=typeof Date.prototype.toJSON&&(Date.prototype.toJSON=function(t){return isFinite(this.valueOf())?this.getUTCFullYear()+"-"+f(this.getUTCMonth()+1)+"-"+f(this.getUTCDate())+"T"+f(this.getUTCHours())+":"+f(this.getUTCMinutes())+":"+f(this.getUTCSeconds())+"Z":null},String.prototype.toJSON=Number.prototype.toJSON=Boolean.prototype.toJSON=function(t){return this.valueOf()});var cx=/[\u0000\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,escapable=/[\\\"\x00-\x1f\x7f-\x9f\u00ad\u0600-\u0604\u070f\u17b4\u17b5\u200c-\u200f\u2028-\u202f\u2060-\u206f\ufeff\ufff0-\uffff]/g,gap,indent,meta={"\b":"\\b","\t":"\\t","\n":"\\n","\f":"\\f","\r":"\\r",'"':'\\"',"\\":"\\\\"},rep;function quote(t){return escapable.lastIndex=0,escapable.test(t)?'"'+t.replace(escapable,function(t){var e=meta[t];return"string"==typeof e?e:"\\u"+("0000"+t.charCodeAt(0).toString(16)).slice(-4)})+'"':'"'+t+'"'}function str(t,e){var n,r,f,o,u,i=gap,a=e[t];switch(a&&"object"==typeof a&&"function"==typeof a.toJSON&&(a=a.toJSON(t)),"function"==typeof rep&&(a=rep.call(e,t,a)),typeof a){case"string":return quote(a);case"number":return isFinite(a)?String(a):"null";case"boolean":case"null":return String(a);case"object":if(!a)return"null";if(gap+=indent,u=[],"[object Array]"===Object.prototype.toString.apply(a)){for(o=a.length,n=0;n<o;n+=1)u[n]=str(n,a)||"null";return f=0===u.length?"[]":gap?"[\n"+gap+u.join(",\n"+gap)+"\n"+i+"]":"["+u.join(",")+"]",gap=i,f}if(rep&&"object"==typeof rep)for(o=rep.length,n=0;n<o;n+=1)"string"==typeof(r=rep[n])&&(f=str(r,a))&&u.push(quote(r)+(gap?": ":":")+f);else for(r in a)Object.hasOwnProperty.call(a,r)&&(f=str(r,a))&&u.push(quote(r)+(gap?": ":":")+f);return f=0===u.length?"{}":gap?"{\n"+gap+u.join(",\n"+gap)+"\n"+i+"}":"{"+u.join(",")+"}",gap=i,f}}"function"!=typeof JSON.stringify&&(JSON.stringify=function(t,e,n){var r;if(indent=gap="","number"==typeof n)for(r=0;r<n;r+=1)indent+=" ";else"string"==typeof n&&(indent=n);if((rep=e)&&"function"!=typeof e&&("object"!=typeof e||"number"!=typeof e.length))throw new Error("JSON.stringify");return str("",{"":t})}),"function"!=typeof JSON.parse&&(JSON.parse=function(text,reviver){var j;function walk(t,e){var n,r,f=t[e];if(f&&"object"==typeof f)for(n in f)Object.hasOwnProperty.call(f,n)&&(void 0!==(r=walk(f,n))?f[n]=r:delete f[n]);return reviver.call(t,e,f)}if(text=String(text),cx.lastIndex=0,cx.test(text)&&(text=text.replace(cx,function(t){return"\\u"+("0000"+t.charCodeAt(0).toString(16)).slice(-4)})),/^[\],:{}\s]*$/.test(text.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g,"@").replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,"]").replace(/(?:^|:|,)(?:\s*\[)+/g,"")))return j=eval("("+text+")"),"function"==typeof reviver?walk({"":j},""):j;throw new SyntaxError("JSON.parse")})}();
////////////////////////////////////////////////////////////////////////////////////////////////////
function ChromeDriver(){
function HttpQueryA(method, url,postdata) {
var whr = new ActiveXObject('WinHttp.WinHttpRequest.5.1');
whr.Open(method,url,true);
whr.Send(postdata);
whr.WaitForResponse(10);
return whr.responseText;
}
port = '9515';
var session = eval('('+HttpQueryA('POST','http://localhost:'+port+'/session','{"capabilities":{}}')+')');
var sessionId = session.value.sessionId;
this.OpenUrl = function(url){
return eval('('+
HttpQueryA('POST','http://localhost:'+port+'/session/'+sessionId+'/url',
'{"url":"'+encodeURI(url)+'"}')+')').value; // null if ok
};
this.OpenUrl('data:text/html,<h1>Test</h>');
this.PageSource = function(){
return eval('('+HttpQueryA('GET','http://localhost:'+port+'/session/'+sessionId+'/source')+')').value;
};
this.FindElementsByXPath = function(XPValue){
var params = new Object();
params.using = 'xpath';
params.value = XPValue;
query = JSON.stringify(params);
response = HttpQueryA('POST','http://localhost:'+port+'/session/'+sessionId+'/element',query);
var obj = eval('('+response+')');
var a = new Array();
for (var key in obj.value) {
a.push(obj.value[key]);
}
return a;
}
this.FindElementByXPath = function(XPValue){
return this.FindElementsByXPath(XPValue)[0];
}
this.FindElementById = function(id){
return this.FindElementByXPath('//*[@id="'+id+'"]');
}
this.FindElementByName = function(name){
return this.FindElementByXPath('//*[@name="'+name+'"]');
}
this.FindElementByAttribute = function(attr, name){
Element = this.FindElementByXPath('//*[@'+attr+'="'+name+'"]');
Element.test = function(){return 'ok';};
return Element;
}
this.ExecuteScript = function(script){
return eval('('+
HttpQueryA('POST',
'http://localhost:'+port+'/session/'+sessionId+'/execute/sync',
'{"script":"'+script+'","args":[]}'
)+')')['value'];
};
this.Close = function(confirmMsg){
HttpQueryA('DELETE','http://localhost:'+port+'/session/'+sessionId+'/window');
};
this.ParentHandle = -1;
this.Parent = new Number();
this.Parent = -1;
return this;
};
//*
var v = new ChromeDriver();
v.OpenUrl('https://forum.mydataexpress.ru');
WScript.Echo(v.FindElementByAttribute('class','crumb'));
// WScript.Echo(v.FindElementByAttribute('class','crumb').test);
//*/
//v.Close();
Текущий вопрос, который сейчас волнует - как сделать вложенную функцию, которую можно вызвать внутри родительской способом:
Родитель(Параметры).Чилд(Параметры)... (а точнее - FindElementByAttribute('...').text, где "text", это отдельная функция с параметрами полученными из результата родительской)