Fix threading issues with Google Translate parsing

This commit is contained in:
~lucidiot 2021-09-17 20:21:06 +02:00
parent 8b8a7d2bbb
commit f3427b91e2
2 changed files with 8 additions and 11 deletions

View File

@ -1,12 +1,15 @@
using System;
using System.Collections.Specialized;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace SpectacleTransformer {
public class GoogleTranslationRequest : HttpTranslationRequest {
public static Uri GoogleTranslateUrl = new Uri("https://translate.google.com/m");
// <3 https://stackoverflow.com/questions/1732348/#1732454
private static Regex ResultRegex = new Regex("<div class=\"result-container\">(?<result>[^<]*)</div>");
public GoogleTranslationRequest(Uri InstanceUri, Language Source, Language Target, string Text) : base(InstanceUri, Source, Target, Text) { }
public static new bool MatchesInstance(Uri InstanceUri) {
@ -24,13 +27,8 @@ namespace SpectacleTransformer {
}
public override string DecodeResponse(string response) {
WebBrowser wb = new WebBrowser();
wb.DocumentText = response;
foreach (HtmlElement element in wb.Document.GetElementsByTagName("div")) {
if (element.GetAttribute("class") == "result-container") {
return element.InnerText.Trim();
}
}
Match m = ResultRegex.Match(response);
if (m.Success && m.Groups["result"].Success) return m.Groups["result"].Value;
return null;
}

View File

@ -78,7 +78,6 @@
<DependentUpon>MainForm.cs</DependentUpon>
</Compile>
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="TranslationResult.cs" />
<Compile Include="Utils.cs" />
<Compile Include="TranslationRequest.cs" />
<EmbeddedResource Include="MainForm.resx">
@ -133,4 +132,4 @@
<Target Name="AfterBuild">
</Target>
-->
</Project>
</Project>