Skip to content

Instantly share code, notes, and snippets.

@guitarrapc
Last active Jan 15, 2019
Embed
What would you like to do?
private static void GetMixedContent(string[] urls)
{
// generate log file and header
WriteHeader();
// chrome option
var options = new ChromeOptions();
options.AddArgument("--headless");
options.AddArgument("--no-sandbox");
// set parallelism
// 1. not define -> set default value (10)
// 2. invalid value -> sequential
// 3. below 1 -> sequential
// 4. others -> parallel
var useParallel = Environment.GetEnvironmentVariable("COUNT") ?? "10";
var isSequential = !int.TryParse(useParallel, out var parallelism) || parallelism <= 1;
// run
if (isSequential)
{
using (var chrome = new ChromeDriver(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), options))
{
foreach (var url in urls)
{
RunDriver(chrome, url);
}
}
}
else
{
Parallel.ForEach(urls, new ParallelOptions { MaxDegreeOfParallelism = parallelism }, (url, state, i) =>
{
using (var chrome = new ChromeDriver(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), options))
{
RunDriver(chrome, url);
}
});
}
}
private static void RunDriver(ChromeDriver chrome, string url)
{
Console.WriteLine($"checking {url}");
var manager = chrome.Manage();
manager.Timeouts().ImplicitWait = TimeSpan.FromMinutes(2);
chrome.Url = url;
var logs = manager.Logs.GetLog("browser");
var mixedContentLogs = logs.Where(x => Regex.IsMatch(x.Message, ".*Mixed Content:.*")).ToArray();
if (mixedContentLogs.Any())
{
Console.ForegroundColor = ConsoleColor.Yellow;
foreach (var log in mixedContentLogs)
{
Console.WriteLine($" > {mixedContentLogs.Length} MixedContent found.");
Console.WriteLine($" * {log.Timestamp} ({log.Level}): {log.Message}");
WriteContent(url, log.Timestamp.ToString("s"), log.Level.ToString(), log.Message);
}
Console.ForegroundColor = orgColor;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment