public Point GetRandomSpotWithinBox(int border = 2)
        {
            if (border < 0)
            {
                border = Math.Abs(border);
            }

            if (Width <= border * 2 || Height <= border * 2)
            {
                throw new ArgumentOutOfRangeException(nameof(border));
            }

            if (border == 0)
            {
                return(new Point(RandomUtils.Random.NextDouble(Left, Right), RandomUtils.Random.NextDouble(Top, Bottom)));
            }

            var boundingBorder = new BoundingClientRect
            {
                Left   = Left + border,
                Right  = Right - border,
                Top    = Top + border,
                Bottom = Bottom - border,
                Width  = Width - (border * 2),
                Height = Height - (border * 2)
            };

            return(boundingBorder.GetRandomSpotWithinBox(border = 0));
        }
        public Tuple <double, double> GetDelta(BoundingClientRect targetRect)
        {
            double deltaX = 0, deltaY = 0;

            if (targetRect.Top < Top && targetRect.Bottom > Bottom)
            {
                deltaY = 0;
            }
            else
            {
                //Negative values scroll down.
                if (targetRect.Bottom < Bottom)
                {
                    deltaY = targetRect.Bottom - Bottom;

                    // Add a random amount between the target top and the top of the screen
                    deltaY += RandomUtils.Random.NextDouble(targetRect.Top - Bottom, 0);
                    deltaY  = Math.Ceiling(deltaY);
                }


                //Positive values scroll up.
                if (targetRect.Top > Top)
                {
                    deltaY = targetRect.Top - Top;

                    deltaY += RandomUtils.Random.NextDouble(0, targetRect.Bottom - Top);
                    deltaY  = Math.Floor(deltaY);
                }
            }

            if (targetRect.Left < Left && targetRect.Right > Right)
            {
                deltaX = 0;
            }
            else
            {
                //Negative values to scroll right.
                if (targetRect.Right < Right)
                {
                    deltaX = Right - targetRect.Right;

                    deltaX += RandomUtils.Random.NextDouble(targetRect.Left - Top, 0);
                    deltaX  = Math.Ceiling(deltaX);
                }

                //Positive values to scroll left.
                if (targetRect.Left > Left)
                {
                    deltaX = Left - targetRect.Left;

                    deltaX += RandomUtils.Random.NextDouble(0, targetRect.Right - Left);
                    deltaX  = Math.Floor(deltaX);
                }
            }

            return(new Tuple <double, double>(deltaX, deltaY));
        }
        public Tuple <double, double> GetOnscreenDelta(PageDimensions pageDimensions)
        {
            var currentViewPort = new BoundingClientRect
            {
                Top    = pageDimensions.ScrollY,
                Bottom = pageDimensions.ScrollY + pageDimensions.WindowHeight,
                Left   = pageDimensions.ScrollX,
                Right  = pageDimensions.ScrollX + pageDimensions.WindowWidth,
                Width  = pageDimensions.WindowWidth,
                Height = pageDimensions.WindowHeight
            };

            return(GetDelta(currentViewPort));
        }
Exemple #4
0
        public async Task <BoundingClientRect> GetBoundingClientRect(long nodeId)
        {
            var resolveNodeResponse = await Session.DOM.ResolveNode(new Dom.ResolveNodeCommand
            {
                NodeId      = nodeId,
                ObjectGroup = "Skrapr"
            });

            var boundingClientRectResponse = await m_session.Runtime.CallFunctionOn(new Runtime.CallFunctionOnCommand
            {
                ObjectId            = resolveNodeResponse.Object.ObjectId,
                FunctionDeclaration = "function() { return this.getBoundingClientRect(); }",
                Silent      = true,
                UserGesture = false
            });

            if (boundingClientRectResponse.Result.Subtype == "error")
            {
                return(null);
            }

            var propertiesResponse = await m_session.Runtime.GetProperties(new Runtime.GetPropertiesCommand
            {
                ObjectId = boundingClientRectResponse.Result.ObjectId
            });

            var properties = propertiesResponse.Result;
            var result     = new BoundingClientRect()
            {
                Top    = double.Parse(properties.First(p => p.Name == "top").Value.Description),
                Right  = double.Parse(properties.First(p => p.Name == "right").Value.Description),
                Bottom = double.Parse(properties.First(p => p.Name == "bottom").Value.Description),
                Left   = double.Parse(properties.First(p => p.Name == "left").Value.Description),
                Width  = double.Parse(properties.First(p => p.Name == "width").Value.Description),
                Height = double.Parse(properties.First(p => p.Name == "height").Value.Description)
            };

            //Cleanup.
            var releaseResponse = await m_session.Runtime.ReleaseObject(new Runtime.ReleaseObjectCommand
            {
                ObjectId = boundingClientRectResponse.Result.ObjectId
            });

            return(result);
        }
Exemple #5
0
        /// <summary>
        /// Retrieves the position of the element relative to the document.
        /// </summary>
        /// <param name="nodeId"></param>
        /// <returns></returns>
        public async Task <BoundingClientRect> GetOffset(long nodeId)
        {
            var resolveNodeResponse = await Session.DOM.ResolveNode(new Dom.ResolveNodeCommand
            {
                NodeId      = nodeId,
                ObjectGroup = "Skrapr"
            });

            var boundingClientRectResponse = await m_session.Runtime.CallFunctionOn(new Runtime.CallFunctionOnCommand
            {
                ObjectId            = resolveNodeResponse.Object.ObjectId,
                FunctionDeclaration = @"function() {
    var doc, docElem, rect, win, elem = this;

    if ( !elem.getClientRects().length ) {
	    return { top: 0, bottom: 0, left: 0, right: 0, height: 0, width: 0 };
    }

    rect = elem.getBoundingClientRect();

    doc = elem.ownerDocument;
    docElem = doc.documentElement;
    win = doc.defaultView;

    return {
	    top: rect.top + win.pageYOffset - docElem.clientTop,
        bottom: rect.bottom + win.pageYOffset - docElem.clientTop,
	    left: rect.left + win.pageXOffset - docElem.clientLeft,
        right: rect.right + win.pageXOffset - docElem.clientLeft,
        height: rect.height,
        width: rect.width
    };
}",
                Silent      = true,
                UserGesture = false
            });

            if (boundingClientRectResponse.Result.Subtype == "error")
            {
                return(null);
            }

            var propertiesResponse = await m_session.Runtime.GetProperties(new Runtime.GetPropertiesCommand
            {
                ObjectId = boundingClientRectResponse.Result.ObjectId
            });

            var properties = propertiesResponse.Result;
            var result     = new BoundingClientRect()
            {
                Top    = double.Parse(properties.First(p => p.Name == "top").Value.Description),
                Right  = double.Parse(properties.First(p => p.Name == "right").Value.Description),
                Bottom = double.Parse(properties.First(p => p.Name == "bottom").Value.Description),
                Left   = double.Parse(properties.First(p => p.Name == "left").Value.Description),
                Width  = double.Parse(properties.First(p => p.Name == "width").Value.Description),
                Height = double.Parse(properties.First(p => p.Name == "height").Value.Description)
            };

            //Cleanup.
            var releaseResponse = await m_session.Runtime.ReleaseObject(new Runtime.ReleaseObjectCommand
            {
                ObjectId = boundingClientRectResponse.Result.ObjectId
            });

            return(result);
        }