Пример #1
0
 internal ChromeSession(ChromeSessionInfo chromeSessionInfo, Chrome chrome)
 {
     this.Id         = chromeSessionInfo.Id;
     internalSession = new BaristaLabs.ChromeDevTools.Runtime.ChromeSession(chromeSessionInfo.WebSocketDebuggerUrl);
     this.chrome     = chrome;
     Task.WaitAll(InitializePage());
 }
Пример #2
0
        private static ChromeSession CreateChromeSession(string sessionInfoUrl, List <string> titleClues, out List <ChromeSessionInfo> otherWsUrls)
        {
            otherWsUrls = null;
            var qnSessions = GetWebSocketSessionInfos(sessionInfoUrl);

            otherWsUrls = qnSessions.Where(k => !string.IsNullOrEmpty(k.WebSocketDebuggerUrl)).ToList();
            ChromeSessionInfo webSocketSessionInfo = null;

            foreach (var title in titleClues)
            {
                webSocketSessionInfo = otherWsUrls.FirstOrDefault(k => k.Title == title);
                if (webSocketSessionInfo != null)
                {
                    otherWsUrls.Remove(webSocketSessionInfo);
                    break;
                }
            }
            ChromeSession chromeSession = null;

            if (webSocketSessionInfo != null)
            {
                chromeSession = new ChromeSessionFactory().Create(webSocketSessionInfo.WebSocketDebuggerUrl, webSocketSessionInfo.Title);
            }
            return(chromeSession);
        }
Пример #3
0
        private ChromeSession addSession(ChromeSessionInfo info)
        {
            var session = new ChromeSession(info, this);

            aliveSessions.Add(session.Id, session);
            return(session);
        }
Пример #4
0
        /// <summary>
        /// Для присоединения к хрому без запуска процесса
        /// </summary>
        /// <param name="port"></param>
        /// <returns></returns>
        async public Task <CallResult <IChromeSession> > GetChromeSession(int port)
        {
            CallResult <IChromeSession> chromeSessionResult = new CallResult <IChromeSession>();

            ChromeSessionInfo sessionInfo           = null;
            CallResult <ChromeSessionInfo[]> result = await GetSessionInfo(port);

            if (result.Success)
            {
                sessionInfo = result.Data[0];
            }
            else
            {
                chromeSessionResult.Error = new UsefulThings.Error(result.Error.Message);
                return(chromeSessionResult);
            }

            try
            {
                var chromeSessionFactory = new ChromeSessionFactory();
                chromeSession = chromeSessionFactory.Create(sessionInfo.WebSocketDebuggerUrl);
            }
            catch (Exception ex)
            {
                chromeSessionResult.Error = new UsefulThings.Error(ex.Message);
                return(chromeSessionResult);
            }

            chromeSession.Subscribe <RequestPausedEvent>(requestPausedEvent =>
            {
                RequestPausedEventHandler(requestPausedEvent);
            });

            chromeSession.Subscribe <AuthRequiredEvent>(authRequiredEvent =>
            {
                AuthRequiredEventHandler(authRequiredEvent);
            });

            chromeSession.Subscribe <FrameNavigatedEvent>(frameNavigatedEvent =>
            {
                FrameNavigatedEventHandler(frameNavigatedEvent);
            });

            chromeSession.Subscribe <LoadingFinishedEvent>(loadingFinishedEvent =>
            {
                LoadingFinishedEventHandler(loadingFinishedEvent);
            });

            //enable page
            var pageEnableResult = await chromeSession.SendAsync <Chrome.Page.EnableCommand>();

            //enable network
            var enableNetwork = await chromeSession.SendAsync(new Chrome.Network.EnableCommand());

            await SetUA(null);

            chromeSessionResult.Data = chromeSession;
            return(chromeSessionResult);
        }
        internal ChromeSession(ChromeSessionInfo chromeSessionInfo, Chrome chrome, int commandTimeout = 120)
        {
            Id              = chromeSessionInfo.Id;
            _chrome         = chrome;
            InternalSession = new BaristaLabs.ChromeDevTools.Runtime.ChromeSession(chromeSessionInfo.WebSocketDebuggerUrl)
            {
                //TODO: move to config
                CommandTimeout = commandTimeout * 1000
            };
            InternalNativeSession = chromeSessionInfo;

            Task.WaitAll(InitializePage());
        }
Пример #6
0
 public ChromeSession Create(ChromeSessionInfo sessionInfo)
 {
     return(Create(sessionInfo.WebSocketDebuggerUrl, string.Empty));
 }
Пример #7
0
        //Launch chrome with
        //"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --remote-debugging-port=9223

        static int Main(string[] args)
        {
            var cliArguments = Cli.Parse <CliArguments>(args);

            //Do an initial check to ensure that the Skrapr Definition exists.
            if (!File.Exists(cliArguments.SkraprDefinitionPath))
            {
                throw new FileNotFoundException($"The specified skrapr definition ({cliArguments.SkraprDefinitionPath}) could not be found. Please check that the skrapr definition exists.");
            }

            //Setup our DI
            var serviceProvider = new ServiceCollection()
                                  .AddLogging()
                                  .BuildServiceProvider();

            //Remove previous log file
            File.Delete("skraprlog.json");

            //Configure Serilog
            Log.Logger = new LoggerConfiguration()
                         .MinimumLevel.Verbose()
                         .Enrich.FromLogContext()
                         .WriteTo.File(new JsonFormatter(), "skraprlog.json")
                         .WriteTo.ColoredConsole(restrictedToMinimumLevel: LogEventLevel.Debug)
                         .CreateLogger();

            //Configure the logger.
            var logger = serviceProvider
                         .GetService <ILoggerFactory>()
                         .AddSerilog()
                         .CreateLogger <Program>();

            ChromeBrowser  browser  = null;
            SkraprDevTools devTools = null;
            SkraprWorker   worker   = null;

            try
            {
                if (cliArguments.Launch)
                {
                    browser = ChromeBrowser.Launch(cliArguments.RemoteDebuggingHost, cliArguments.RemoteDebuggingPort);
                }

                logger.LogInformation("Connecting to a Chrome session on {chromeHost}:{chromeRemoteDebuggingPort}...", cliArguments.RemoteDebuggingHost, cliArguments.RemoteDebuggingPort);

                ChromeSessionInfo session = null;
                try
                {
                    var sessions = ChromeBrowser.GetChromeSessions(cliArguments.RemoteDebuggingHost, cliArguments.RemoteDebuggingPort).GetAwaiter().GetResult();
                    session = sessions.FirstOrDefault(s => s.Type == "page" && !String.IsNullOrWhiteSpace(s.WebSocketDebuggerUrl));
                }
                catch (System.Net.Http.HttpRequestException)
                {
                    logger.LogWarning("Unable to connect to a Chrome session on {chromeHost}:{chromeRemoteDebuggingPort}.", cliArguments.RemoteDebuggingHost, cliArguments.RemoteDebuggingPort);
                    logger.LogWarning("Please ensure that a chrome session has been launched with the --remote-debugging-port={chromeRemoteDebuggingPort} command line argument", cliArguments.RemoteDebuggingPort);
                    logger.LogWarning("Or, launch SkraprConsoleHost with -l");
                    Debugger.Break();
                    return(-1);
                }

                //TODO: Create a new session if one doesn't exist.
                if (session == null)
                {
                    logger.LogWarning("Unable to locate a suitable session. Ensure that the Developer Tools window is closed on an existing session or create a new chrome instance with the --remote-debugging-port={chromeRemoteDebuggingPort) command line argument", cliArguments.RemoteDebuggingPort);
                    Debugger.Break();
                    return(-1);
                }

                devTools = SkraprDevTools.Connect(serviceProvider, session).GetAwaiter().GetResult();
                logger.LogInformation("Using session {sessionId}: {sessionTitle} - {webSocketDebuggerUrl}", session.Id, session.Title, session.WebSocketDebuggerUrl);

                worker = SkraprWorker.Create(serviceProvider, cliArguments.SkraprDefinitionPath, devTools.Session, devTools, debugMode: cliArguments.Debug);

                if (cliArguments.Debug)
                {
                    logger.LogInformation($"Operating in debug mode. Tasks may perform additional behavior or may skip themselves.");
                }

                if (cliArguments.Attach == true)
                {
                    var targetInfo        = devTools.Session.Target.GetTargetInfo(session.Id).GetAwaiter().GetResult();
                    var matchingRuleCount = worker.GetMatchingRules().GetAwaiter().GetResult().Count();
                    if (matchingRuleCount > 0)
                    {
                        logger.LogInformation($"Attach specified and {matchingRuleCount} rules match the current session's state; Continuing.", matchingRuleCount);
                        worker.Post(new NavigateTask
                        {
                            Url = targetInfo.Url
                        });
                    }
                    else
                    {
                        logger.LogInformation($"Attach specified but no rules matched the current session's state; Adding start tasks.");
                        worker.AddStartUrls();
                    }
                }
                else
                {
                    logger.LogInformation($"Adding start tasks.");
                    worker.AddStartUrls();
                }

                Console.TreatControlCAsInput = true;
                logger.LogInformation("Skrapr is currently processing. Press ENTER to exit...");

                var cancelKeyTokenSource = new CancellationTokenSource();

                var workerCompletion = worker.Completion
                                       .ContinueWith((t) => cancelKeyTokenSource.Cancel());

                var keyCompletion = ConsoleUtils.ReadKeyAsync(ConsoleKey.Enter, cancelKeyTokenSource.Token)
                                    .ContinueWith(async(t) =>
                {
                    if (!t.IsCanceled)
                    {
                        logger.LogWarning("Stop requested at the console, cancelling...");
                        worker.Cancel();
                        await worker.Completion;
                    }
                });

                Task.WaitAny(workerCompletion, keyCompletion);

                if (worker.Completion.IsFaulted)
                {
                    logger.LogError("Worker was faulted. Exiting with status code of -1");
                    if (Debugger.IsAttached)
                    {
                        throw worker.Completion.Exception.Flatten();
                    }
                    return(-1);
                }
            }
            catch (TaskCanceledException)
            {
                //Do Nothing
            }
            finally
            {
                //Cleanup.
                if (worker != null)
                {
                    worker.Dispose();
                    worker = null;
                }

                if (devTools != null)
                {
                    devTools.Dispose();
                    devTools = null;
                }

                if (browser != null)
                {
                    browser.Dispose();
                    browser = null;
                }
            }

            logger.LogInformation("Worker completed successfully. Status code 0");
            Debugger.Break();
            return(0);
        }
Пример #8
0
        } // End Sub NotMain

        public static async System.Threading.Tasks.Task runVote()
        {
            // synchronization
            System.Threading.ManualResetEventSlim screenshotDone = new System.Threading.ManualResetEventSlim();

            // STEP 1 - Run Chrome
            IChromeProcessFactory chromeProcessFactory = new ChromeProcessFactory(new StubbornDirectoryCleaner());

            using (IChromeProcess chromeProcess = chromeProcessFactory.Create(9222, true))
            {
                // STEP 2 - Create a debugging session
                ChromeSessionInfo[] sessionInfos = await chromeProcess.GetSessionInfo();

                ChromeSessionInfo sessionInfo = (sessionInfos != null && sessionInfos.Length > 0) ?
                                                sessionInfos[sessionInfos.Length - 1]
                    : new ChromeSessionInfo();

                IChromeSessionFactory chromeSessionFactory = new ChromeSessionFactory();
                IChromeSession        chromeSession        = chromeSessionFactory.Create(sessionInfo.WebSocketDebuggerUrl);



                CommandResponse <ClearBrowserCacheCommandResponse> clearCache = await chromeSession.SendAsync(new ClearBrowserCacheCommand());

                System.Console.WriteLine(clearCache.Result);


                CommandResponse <ClearBrowserCookiesCommandResponse> clearCookies = await chromeSession.SendAsync(new ClearBrowserCookiesCommand());

                System.Console.WriteLine(clearCookies.Result);

                // CommandResponse<ClearObjectStoreCommandResponse> clearObjectStorage = await chromeSession.SendAsync(new ClearObjectStoreCommand());
                // System.Console.WriteLine(clearObjectStorage.Result);


                // CommandResponse<ClearDataForOriginCommandResponse> clearStorage = await chromeSession.SendAsync(new ClearDataForOriginCommand() { Origin= "www.20min.ch", StorageTypes = "all" });
                // Whatever the correct command for clear everything is...
                await ClearData(chromeSession, "www.20min.ch", "all");
                await ClearData(chromeSession, "20min.ch", "all");
                await ClearData(chromeSession, "*", "all");
                await ClearData(chromeSession, "all", "all");



                // STEP 3 - Send a command
                //
                // Here we are sending a commands to tell chrome to set the viewport size
                // and navigate to the specified URL
                await chromeSession.SendAsync(new SetDeviceMetricsOverrideCommand
                {
                    Width  = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width,
                    Height = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Height,
                    Scale  = 1
                });


                CommandResponse <NavigateCommandResponse> navigateResponse = await chromeSession.SendAsync(new NavigateCommand
                {
                    // Url = "http://www.google.com"
                    // Url = "about:blank"
                    Url = "https://www.20min.ch/schweiz/news/story/GA-wird-teurer--kein-Studentenrabatt-mehr-27426069"
                });

                System.Console.WriteLine("NavigateResponse: " + navigateResponse.Id);



                // STEP 4 - Register for events (in this case, "Page" domain events)
                // send an command to tell chrome to send us all Page events
                // but we only subscribe to certain events in this session

                ICommandResponse pageEnableResult = await chromeSession.SendAsync <MasterDevs.ChromeDevTools.Protocol.Chrome.Page.EnableCommand>();

                System.Console.WriteLine("PageEnable: " + pageEnableResult.Id);

                chromeSession.Subscribe <LoadEventFiredEvent>(loadEventFired =>
                {
                    // we cannot block in event handler, hence the task
                    System.Threading.Tasks.Task2.Run(async() =>
                    {
                        System.Console.WriteLine("LoadEventFiredEvent: " + loadEventFired.Timestamp);

                        /*
                         * long documentNodeId = (await chromeSession.SendAsync(new GetDocumentCommand())).Result.Root.NodeId;
                         * long bodyNodeId =
                         *  (await chromeSession.SendAsync(new QuerySelectorCommand
                         *  {
                         *      NodeId = documentNodeId,
                         *      Selector = "body"
                         *  })).Result.NodeId;
                         *
                         * long height = (await chromeSession.SendAsync(new GetBoxModelCommand { NodeId = bodyNodeId })).Result.Model.Height;
                         */


                        CommandResponse <EvaluateCommandResponse> removePopOver = await chromeSession.SendAsync(new EvaluateCommand
                        {
                            Expression = @"
    window.setTimeout(function(){ 
        var one = document.getElementById('onesignal-popover-cancel-button');
            if(one != null)
        one.click();
    }, 2000);

/*
window.setTimeout(function(){ 
        window.close();
    }, 4000);
*/
    ",
                            // ContextId = 123
                        });



                        CommandResponse <EvaluateCommandResponse> closeWindow = await chromeSession.SendAsync(new EvaluateCommand
                        {
                            // <a href="javascript:window.close(self);">run</a>
                            Expression = @"
console.log('closing');
var a = document.createElement('a');
var linkText = document.createTextNode('T');
a.id = 'lolz';
a.appendChild(linkText);
a.href = 'javascript:window.close(self);';
document.body.appendChild(a);
document.getElementById('lolz').click();
// window.close();
// open(location, '_self').close();
"
                                         // ContextId = 123
                            ,
                            UserGesture = true
                        });
                        System.Console.WriteLine(closeWindow.Result);



                        System.Console.WriteLine("Closing page");
                        var closeTargetResponse = chromeSession.SendAsync(
                            new CloseTargetCommand()
                        {
                            TargetId = navigateResponse.Result.FrameId
                        }
                            );

                        /*
                         * MasterDevs.ChromeDevTools.CommandResponse<CloseTargetCommandResponse> closeTargetResponse =
                         *  await chromeSession.SendAsync(
                         *  new CloseTargetCommand()
                         *  {
                         *      TargetId = navigateResponse.Result.FrameId
                         *  }
                         * );
                         */
                        System.Console.WriteLine("Page closed");
                        System.Console.WriteLine(closeTargetResponse);



                        if (true)
                        {
                            // document.querySelector("#thread3367_msg3367 > div.rate_button > div.clickable.top").click()
                            // document.querySelector("#thread3367_msg3367 > div.rate_button > div.clickable.bottom").click()
                            string threadId      = "3399";
                            string msgId         = "3399";
                            string voteDirection = "bottom"; // top / bottom

                            string votingElement = "#thread" + threadId + "_msg" + msgId + @" > div.rate_button > div.clickable." + voteDirection;

                            string javaScriptToExecute = @"
    (function()
    {
        var elmnt = document.querySelector('" + votingElement + @"');
        if (elmnt != null)
        {
            elmnt.scrollIntoView();
            window.scrollBy(0, -70)
            elmnt.click();
            console.log('https://www.youtube.com/watch?v=h6mJw50OdZ4&t=163');
            console.log('The first honest vote ever in a rotten borough !');
            console.log('CopyLeft 2019 StS');
        }
    })();
    ";


                            CommandResponse <EvaluateCommandResponse> evr = await chromeSession.SendAsync(new EvaluateCommand
                            {
                                Expression = javaScriptToExecute,
                                // ContextId = 123
                            });

                            if (evr.Result.ExceptionDetails != null)
                            {
                                System.Console.WriteLine(evr.Result.ExceptionDetails);
                            }
                            else
                            {
                                System.Console.WriteLine("voted");
                            }
                        }

                        await System.Threading.Tasks.Task2.Delay(3000);

                        // tell the main thread we are done
                        screenshotDone.Set();
                    });
                }); // End Sub LoadEventFired

                // wait for screenshoting thread to (start and) finish
                screenshotDone.Wait();

                System.Console.WriteLine("Exiting ..");
            } // End Using chromeProcess
        }     // End Sub Main(string[] args)
 public IChromeSession Create(ChromeSessionInfo sessionInfo)
 {
     return(Create(sessionInfo.WebSocketDebuggerUrl));
 }
Пример #10
0
        } // End Sub KillHeadless

        private static void Main(string[] args)
        {
            KillHeadless();

            Task.Run(async() =>
            {
                // synchronization
                System.Threading.ManualResetEventSlim screenshotDone = new System.Threading.ManualResetEventSlim();

                // STEP 1 - Run Chrome
                IChromeProcessFactory chromeProcessFactory = new ChromeProcessFactory(new StubbornDirectoryCleaner());
                using (IChromeProcess chromeProcess = chromeProcessFactory.Create(9222, true))
                {
                    // STEP 2 - Create a debugging session
                    //ChromeSessionInfo sessionInfo = (await chromeProcess.GetSessionInfo()).LastOrDefault();
                    ChromeSessionInfo[] sessionInfos = await chromeProcess.GetSessionInfo();
                    ChromeSessionInfo sessionInfo    = (sessionInfos != null && sessionInfos.Length > 0) ?
                                                       sessionInfos[sessionInfos.Length - 1]
                        : new ChromeSessionInfo();

                    IChromeSessionFactory chromeSessionFactory = new ChromeSessionFactory();
                    IChromeSession chromeSession = chromeSessionFactory.Create(sessionInfo.WebSocketDebuggerUrl);

                    // STEP 3 - Send a command
                    //
                    // Here we are sending a commands to tell chrome to set the viewport size
                    // and navigate to the specified URL
                    await chromeSession.SendAsync(new SetDeviceMetricsOverrideCommand
                    {
                        Width  = ViewPortWidth,
                        Height = ViewPortHeight,
                        Scale  = 1
                    });

                    var navigateResponse = await chromeSession.SendAsync(new NavigateCommand
                    {
                        Url = "http://www.google.com"
                    });
                    System.Console.WriteLine("NavigateResponse: " + navigateResponse.Id);

                    // STEP 4 - Register for events (in this case, "Page" domain events)
                    // send an command to tell chrome to send us all Page events
                    // but we only subscribe to certain events in this session
                    ICommandResponse pageEnableResult = await chromeSession.SendAsync <Protocol.Chrome.Page.EnableCommand>();
                    System.Console.WriteLine("PageEnable: " + pageEnableResult.Id);

                    chromeSession.Subscribe <LoadEventFiredEvent>(loadEventFired =>
                    {
                        // we cannot block in event handler, hence the task
                        Task.Run(async() =>
                        {
                            System.Console.WriteLine("LoadEventFiredEvent: " + loadEventFired.Timestamp);

                            long documentNodeId = (await chromeSession.SendAsync(new GetDocumentCommand())).Result.Root.NodeId;
                            long bodyNodeId     =
                                (await chromeSession.SendAsync(new QuerySelectorCommand
                            {
                                NodeId = documentNodeId,
                                Selector = "body"
                            })).Result.NodeId;

                            long height = (await chromeSession.SendAsync(new GetBoxModelCommand {
                                NodeId = bodyNodeId
                            })).Result.Model.Height;

                            await chromeSession.SendAsync(new SetDeviceMetricsOverrideCommand
                            {
                                Width  = ViewPortWidth,
                                Height = height,
                                Scale  = 1
                            });

                            System.Console.WriteLine("Taking screenshot");
                            var screenshot = await chromeSession.SendAsync(new CaptureScreenshotCommand {
                                Format = "png"
                            });

                            byte[] data = System.Convert.FromBase64String(screenshot.Result.Data);
                            System.IO.File.WriteAllBytes("output.png", data);
                            System.Console.WriteLine("Screenshot stored");


                            PrintToPDFCommand printCommand = new PrintToPDFCommand()
                            {
                                Scale           = 1,
                                MarginTop       = 0,
                                MarginLeft      = 0,
                                MarginRight     = 0,
                                MarginBottom    = 0,
                                PrintBackground = true,
                                Landscape       = false,
                                PaperWidth      = cm2inch(21),
                                PaperHeight     = cm2inch(29.7)
                            };


                            System.Console.WriteLine("Printing PDF");
                            CommandResponse <PrintToPDFCommandResponse> pdf = await chromeSession.SendAsync(printCommand);
                            System.Console.WriteLine("PDF printed.");

                            byte[] pdfData = System.Convert.FromBase64String(pdf.Result.Data);
                            System.IO.File.WriteAllBytes("output.pdf", pdfData);
                            System.Console.WriteLine("PDF stored");


                            // tell the main thread we are done
                            screenshotDone.Set();
                        });
                    });

                    // wait for screenshoting thread to (start and) finish
                    System.Console.WriteLine("Exiting ..");
                    screenshotDone.Wait();
                }
            }).Wait();
        }