/// <summary> /// 获取CSS Selector(当包含上下文的时候调用此方法) /// http://stackoverflow.com/questions/3390396/how-to-check-for-undefined-in-javascript /// </summary> /// <param name="element"></param> public string GetContextSelector(mshtml.IHTMLElement context, mshtml.IHTMLElement element) { SetContextSelector(context); var originalId = element.id; if (string.IsNullOrEmpty(originalId)) { var id = "__" + IEUtils.IEVariableNameHelper.CreateVariableName(); element.id = id; } mshtml.IHTMLWindow2 parentWindow = ((IHTMLDocument2)(element.document)).parentWindow as mshtml.IHTMLWindow2; IEUtils.RunScript(ScriptLoader.GetJquerySelector(""), parentWindow); StringBuilder sb = new StringBuilder(); sb.Append(string.Format("var el = $jq('#{0}');", element.id)); sb.AppendLine(); sb.Append("if(typeof __context == 'undefined'){"); sb.AppendLine(); sb.Append("__context = '';"); sb.AppendLine(); sb.Append("}"); sb.AppendLine(); sb.Append(" var selector = el.getCssPath(__context);"); sb.AppendLine(); sb.Append("__context = 'undefined';"); sb.Append("return selector"); object result = JS.FunEval(AppContext.Current.Window, sb.ToString()); element.id = originalId; return(result == null ? "" : result.ToString()); }
/// <summary> /// 获取CSS Selector(当包含上下文的时候调用此方法) /// http://stackoverflow.com/questions/3390396/how-to-check-for-undefined-in-javascript /// </summary> /// <param name="element"></param> public void SetSelector(mshtml.IHTMLElement element) { var originalId = element.id; if (string.IsNullOrEmpty(originalId)) { var id = "__" + IEUtils.IEVariableNameHelper.CreateVariableName(); element.id = id; } mshtml.IHTMLWindow2 parentWindow = ((IHTMLDocument2)(element.document)).parentWindow as mshtml.IHTMLWindow2; IEUtils.RunScript(ScriptLoader.GetJquerySelector(""), parentWindow); StringBuilder sb = new StringBuilder(); sb.Append(string.Format("var el = $jq('#{0}');", element.id)); sb.AppendLine(); sb.Append("if(typeof __context == 'undefined'){"); sb.AppendLine(); sb.Append("__context = '';"); sb.AppendLine(); sb.Append("}"); sb.AppendLine(); sb.Append(" var selector = el.getCssPath(__context);"); sb.AppendLine(); sb.Append("el.attr('__selector', selector);"); //sb.Append("alert(el.attr('__selector'));"); //sb.AppendLine(); IEUtils.RunScript(sb.ToString(), parentWindow); element.id = originalId; }
public FrmMap(FrmMain aParent) { InitializeComponent(); // 添加本实例指定的JS代码 this.wbMap.JsCode = MapHelper.GetMainMap(); mParent = aParent; IEUtils.SetWebBrowserFeatures(); }
/// <summary> /// 运行脚本 /// </summary> /// <param name="script"></param> /// <param name="isInjectJquery">是否需要注入jquery</param> public void RunScript(string script, bool isInjectJquery) { var el = this.GetIHTMLElement(); mshtml.IHTMLWindow2 parentWindow = ((IHTMLDocument2)(el.document)).parentWindow as mshtml.IHTMLWindow2; if (isInjectJquery) { IEUtils.RunScript(ScriptLoader.GetJqueryInstallScript(), parentWindow); } IEUtils.RunScript(script, parentWindow); }
/// <summary> /// 还原当前页面SESSION /// </summary> static public void RestorePageSession(this Browser browser) { RequireJQueryInstall(browser); string fileName = browser.Uri.Host; fileName = Path.Combine(Environment.GetFolderPath( Environment.SpecialFolder.ApplicationData), string.Format("{0}.session", fileName)); StreamReader sw = null; string session = ""; if (!File.Exists(fileName)) { return; } try { sw = new StreamReader(fileName); session = sw.ReadToEnd(); } finally { if (sw != null) { sw.Close(); } } string code = @" var obj = " + session + @"; return obj.url; "; object url = JS.FunEval(browser, code); if (url != null) { //先打开原始页面 browser.GoTo(url.ToString()); //回复FORM内容 code = @" var obj =" + session + @"; $jq('form').deserialize(obj.form); "; IEUtils.RunScript(code, GetWindows(browser)); browser.AttachToIE(); } }
/// <summary> /// 设置上下文变量(__context) /// </summary> /// <param name="element"></param> private void SetContextSelector(mshtml.IHTMLElement element) { var originalId = element.id; if (string.IsNullOrEmpty(originalId)) { var id = "__" + IEUtils.IEVariableNameHelper.CreateVariableName(); element.id = id; } mshtml.IHTMLWindow2 parentWindow = ((IHTMLDocument2)(element.document)).parentWindow as mshtml.IHTMLWindow2; StringBuilder sb = new StringBuilder(); sb.Append(string.Format("__context = $jq('#{0}');", element.id)); IEUtils.RunScript(ScriptLoader.GetJqueryInstallScript(), parentWindow); IEUtils.RunScript(sb.ToString(), parentWindow); element.id = originalId; }
public override INativeDocument OnGetNativeDocument() { return(new IEDocument(IEUtils.IEDOMFromhWnd(hwnd))); }
static public void RequireJQueryInstall(this Browser browser) { mshtml.IHTMLDocument2 doc = ((IEDocument)(browser.DomContainer.NativeDocument)).HtmlDocument; IEUtils.RunScript(ScriptLoader.GetJqueryInstallScript(), doc.parentWindow); }