Skip to content

Commit e31727c

Browse files
authored
Make CodeFactor happy (#206)
1 parent de31886 commit e31727c

File tree

6 files changed

+79
-58
lines changed

6 files changed

+79
-58
lines changed

appveyor.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ configuration: Release
77
before_build:
88
- ps: >-
99
dotnet restore .\lib\PuppeteerSharp.sln
10-
10+
1111
New-SelfSignedCertificate -Subject "localhost" -FriendlyName "Puppeteer" -CertStoreLocation "cert:\CurrentUser\My"
1212
1313
Get-ChildItem -Path cert:\CurrentUSer\my | where { $_.friendlyname -eq "Puppeteer" } | Export-Certificate -FilePath C:\projects\puppeteer-sharp\lib\PuppeteerSharp.TestServer\testCert.cer

lib/PuppeteerSharp/Connection.cs

Lines changed: 44 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -173,51 +173,56 @@ await Task.WhenAny(
173173

174174
if (!string.IsNullOrEmpty(response))
175175
{
176-
dynamic obj = JsonConvert.DeserializeObject(response);
177-
var objAsJObject = obj as JObject;
176+
ProcessResponse(response);
177+
}
178+
}
179+
}
178180

179-
if (objAsJObject["id"] != null)
180-
{
181-
int id = (int)objAsJObject["id"];
181+
private void ProcessResponse(string response)
182+
{
183+
dynamic obj = JsonConvert.DeserializeObject(response);
184+
var objAsJObject = obj as JObject;
182185

183-
//If we get the object we are waiting for we return if
184-
//if not we add this to the list, sooner or later some one will come for it
185-
if (!_responses.ContainsKey(id))
186-
{
187-
QueueId(id);
188-
}
186+
if (objAsJObject["id"] != null)
187+
{
188+
int id = (int)objAsJObject["id"];
189+
190+
//If we get the object we are waiting for we return if
191+
//if not we add this to the list, sooner or later some one will come for it
192+
if (!_responses.ContainsKey(id))
193+
{
194+
QueueId(id);
195+
}
189196

190-
_responses[id].SetResult(obj.result);
197+
_responses[id].SetResult(obj.result);
198+
}
199+
else
200+
{
201+
if (obj.method == "Target.receivedMessageFromTarget")
202+
{
203+
var session = _sessions.GetValueOrDefault(objAsJObject["params"]["sessionId"].ToString());
204+
if (session != null)
205+
{
206+
session.OnMessage(objAsJObject["params"]["message"].ToString());
191207
}
192-
else
208+
}
209+
else if (obj.method == "Target.detachedFromTarget")
210+
{
211+
var session = _sessions.GetValueOrDefault(objAsJObject["params"]["sessionId"].ToString());
212+
if (session != null)
193213
{
194-
if (obj.method == "Target.receivedMessageFromTarget")
195-
{
196-
var session = _sessions.GetValueOrDefault(objAsJObject["params"]["sessionId"].ToString());
197-
if (session != null)
198-
{
199-
session.OnMessage(objAsJObject["params"]["message"].ToString());
200-
}
201-
}
202-
else if (obj.method == "Target.detachedFromTarget")
203-
{
204-
var session = _sessions.GetValueOrDefault(objAsJObject["params"]["sessionId"].ToString());
205-
if (session != null)
206-
{
207-
session.Close();
208-
}
209-
210-
_sessions.Remove(objAsJObject["params"]["sessionId"].ToString());
211-
}
212-
else
213-
{
214-
MessageReceived?.Invoke(this, new MessageEventArgs
215-
{
216-
MessageID = obj.method,
217-
MessageData = objAsJObject["params"] as dynamic
218-
});
219-
}
214+
session.Close();
220215
}
216+
217+
_sessions.Remove(objAsJObject["params"]["sessionId"].ToString());
218+
}
219+
else
220+
{
221+
MessageReceived?.Invoke(this, new MessageEventArgs
222+
{
223+
MessageID = obj.method,
224+
MessageData = objAsJObject["params"] as dynamic
225+
});
221226
}
222227
}
223228
}

lib/PuppeteerSharp/ExecutionContext.cs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -142,21 +142,25 @@ private object FormatArgument(object arg)
142142
switch (arg)
143143
{
144144
case double d:
145-
// no such thing as -0 in C# :)
146-
if (double.IsPositiveInfinity(d)) return new { unserializableValue = "Infinity" };
147-
if (double.IsNegativeInfinity(d)) return new { unserializableValue = "-Infinity" };
148-
if (double.IsNaN(d)) return new { unserializableValue = "NaN" };
145+
if (double.IsPositiveInfinity(d))
146+
{
147+
return new { unserializableValue = "Infinity" };
148+
}
149+
150+
if (double.IsNegativeInfinity(d))
151+
{
152+
return new { unserializableValue = "-Infinity" };
153+
}
154+
155+
if (double.IsNaN(d))
156+
{
157+
return new { unserializableValue = "NaN" };
158+
}
159+
149160
break;
161+
150162
case JSHandle objectHandle:
151-
if (objectHandle.ExecutionContext != this)
152-
throw new PuppeteerException("JSHandles can be evaluated only in the context they were created!");
153-
if (objectHandle.Disposed)
154-
throw new PuppeteerException("JSHandle is disposed!");
155-
if (objectHandle.RemoteObject.unserializableValue != null)
156-
return new { objectHandle.RemoteObject.unserializableValue };
157-
if (objectHandle.RemoteObject.objectId == null)
158-
return new { objectHandle.RemoteObject.value };
159-
return new { objectHandle.RemoteObject.objectId };
163+
return objectHandle.FormatArgument(this);
160164
}
161165
return new { value = arg };
162166
}

lib/PuppeteerSharp/Frame.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,6 @@ internal Task SetContentAsync(string html)
279279
document.close();
280280
}", html);
281281

282-
283282
internal Task<string> GetTitleAsync() => EvaluateExpressionAsync<string>("document.title");
284283

285284
internal void OnLifecycleEvent(string loaderId, string name)

lib/PuppeteerSharp/JSHandle.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using Newtonsoft.Json.Linq;
2+
using System;
23
using System.Collections.Generic;
34
using System.Threading.Tasks;
45

@@ -33,7 +34,7 @@ public async Task<Dictionary<string, JSHandle>> GetPropertiesAsync()
3334
ownProperties = true
3435
});
3536
var result = new Dictionary<string, JSHandle>();
36-
foreach(var property in response.result)
37+
foreach (var property in response.result)
3738
{
3839
if (property.enumerable == null)
3940
continue;
@@ -84,5 +85,18 @@ public override string ToString()
8485

8586
return Helper.ValueFromRemoteObject<object>(RemoteObject)?.ToString();
8687
}
88+
89+
internal object FormatArgument(ExecutionContext context)
90+
{
91+
if (ExecutionContext != context)
92+
throw new PuppeteerException("JSHandles can be evaluated only in the context they were created!");
93+
if (Disposed)
94+
throw new PuppeteerException("JSHandle is disposed!");
95+
if (RemoteObject.unserializableValue != null)
96+
return new { RemoteObject.unserializableValue };
97+
if (RemoteObject.objectId == null)
98+
return new { RemoteObject.value };
99+
return new { RemoteObject.objectId };
100+
}
87101
}
88102
}

lib/PuppeteerSharp/Page.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ await Task.WhenAll(
480480
client.SendAsync("Security.enable", null),
481481
client.SendAsync("Performance.enable", null)
482482
);
483-
483+
484484
if (ignoreHTTPSErrors)
485485
{
486486
await client.SendAsync("Security.setOverrideCertificateErrors", new Dictionary<string, object>
@@ -517,7 +517,7 @@ public async Task<Response> GoToAsync(string url, NavigationOptions options = nu
517517
requests.Add(e.Request.Url, e.Request);
518518
}
519519
};
520-
520+
521521
_networkManager.RequestCreated += createRequestEventListener;
522522

523523
var mainFrame = _frameManager.MainFrame;
@@ -1134,7 +1134,6 @@ private async Task OnCertificateError(MessageEventArgs e)
11341134
{"eventId", e.MessageData.eventId },
11351135
{"action", "continue"}
11361136
});
1137-
11381137
}
11391138
}
11401139

0 commit comments

Comments
 (0)