public GeckoElement GetElementByJQuery(string jQuery)
        {
            using (var autoContext = new AutoJSContext(_window))
            {
                var jsValue = autoContext.EvaluateScript(jQuery, _window.DomWindow);

                if (!jsValue.IsObject)
                {
                    return(null);
                }

                var nativeComObject = jsValue.ToComObject(autoContext.ContextPointer);
                var element         = Xpcom.QueryInterface </* nsIDOMHTMLElement */ nsIDOMElement>(nativeComObject);
                if (element != null)
                {
                    return(GeckoHtmlElement.Create((mozIDOMWindowProxy)_window.DomWindow, element));
                }

                if (!SpiderMonkey.JS_HasProperty(autoContext.ContextPointer, jsValue.AsPtr, "length"))
                {
                    return(null);
                }

                var length = SpiderMonkey.JS_GetProperty(autoContext.ContextPointer, jsValue.AsPtr, "length").ToInteger();
                if (length == 0)
                {
                    return(null);
                }

                return(CreateHtmlElementFromDom(autoContext, jsValue, 0));
            }
        }
        private static GeckoElement CreateHtmlElementFromDom(AutoJSContext autoContext, JsVal jsValue, int elementIndex)
        {
            var elementIndexString = elementIndex.ToString(CultureInfo.InvariantCulture);
            var firstNativeDom     = SpiderMonkey.JS_GetProperty(autoContext.ContextPointer, jsValue.AsPtr, elementIndexString).ToComObject(autoContext.ContextPointer);

            var element = Xpcom.QueryInterface </* nsIDOMHTMLElement */ nsIDOMElement>(firstNativeDom);

            return(element == null ? null : GeckoHtmlElement.Create((mozIDOMWindowProxy)autoContext.Window, element));
        }
示例#3
0
        public GeckoElement GetElementByJQuery(string jQuery)
        {
            JsVal jsValue;

            using (var autoContext = new AutoJSContext())
            {
                autoContext.PushCompartmentScope((nsISupports)m_Window.Document.DomObject);
                jsValue = autoContext.EvaluateScript(jQuery);
                if (jsValue.IsObject)
                {
                    var nativeComObject = jsValue.ToComObject(autoContext.ContextPointer);
                    var element         = Xpcom.QueryInterface <nsIDOMHTMLElement>(nativeComObject);
                    if (element != null)
                    {
                        return(GeckoHtmlElement.Create(element));
                    }

                    if (!SpiderMonkey.JS_HasProperty(autoContext.ContextPointer, jsValue.AsPtr, "length"))
                    {
                        return(null);
                    }

                    var length = SpiderMonkey.JS_GetProperty(autoContext.ContextPointer, jsValue.AsPtr, "length").ToInteger();
                    if (length == 0)
                    {
                        return(null);
                    }

                    var firstNativeDom = SpiderMonkey.JS_GetProperty(autoContext.ContextPointer, jsValue.AsPtr, "0").ToComObject(autoContext.ContextPointer);
                    element = Xpcom.QueryInterface <nsIDOMHTMLElement>(firstNativeDom);
                    if (element != null)
                    {
                        return(GeckoHtmlElement.Create(element));
                    }
                }
            }
            return(null);
        }
示例#4
0
        public IEnumerable <GeckoElement> GetElementsByJQuery(string jQuery)
        {
            JsVal jsValue;
            var   elements = new List <GeckoElement>();

            using (var autoContext = new AutoJSContext())
            {
                autoContext.PushCompartmentScope((nsISupports)m_Window.Document.DomObject);
                jsValue = autoContext.EvaluateScript(jQuery);
                if (!jsValue.IsObject)
                {
                    return(elements);
                }

                if (!SpiderMonkey.JS_HasProperty(autoContext.ContextPointer, jsValue.AsPtr, "length"))
                {
                    return(null);
                }

                var length = SpiderMonkey.JS_GetProperty(autoContext.ContextPointer, jsValue.AsPtr, "length").ToInteger();
                if (length == 0)
                {
                    return(null);
                }

                for (var elementIndex = 0; elementIndex < length; elementIndex++)
                {
                    var firstNativeDom = SpiderMonkey.JS_GetProperty(autoContext.ContextPointer, jsValue.AsPtr, elementIndex.ToString(CultureInfo.InvariantCulture)).ToComObject(autoContext.ContextPointer);
                    var element        = Xpcom.QueryInterface <nsIDOMHTMLElement>(firstNativeDom);
                    if (element != null)
                    {
                        elements.Add(GeckoHtmlElement.Create(element));
                    }
                }
            }
            return(elements);
        }