Last active
January 15, 2019 00:32
-
-
Save guitarrapc/e9680e5ca078d11974fd566b2dece159 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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