Skip to content

Commit

Permalink
feat: ExportAsTxt
Browse files Browse the repository at this point in the history
  • Loading branch information
Pinguin2001 committed Jan 4, 2025
1 parent df2650e commit d835f56
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 62 deletions.
4 changes: 0 additions & 4 deletions src/Bluebird.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@
<Compile Include="MainPage.xaml.cs">
<DependentUpon>MainPage.xaml</DependentUpon>
</Compile>
<Compile Include="Modules\ForceDark\ForceDarkHelper.cs" />
<Compile Include="Modules\Readability\ReadabilityHelper.cs" />
<Compile Include="Pages\LoginPage.xaml.cs">
<DependentUpon>LoginPage.xaml</DependentUpon>
Expand Down Expand Up @@ -256,9 +255,6 @@
<Content Include="Assets\AppIcons\Wide310x150Logo.scale-125.png" />
<Content Include="Assets\AppIcons\Wide310x150Logo.scale-150.png" />
<Content Include="Assets\AppIcons\Wide310x150Logo.scale-400.png" />
<Content Include="Modules\ForceDark\Assets\forcedark.min.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
<Content Include="Modules\Readability\Assets\readability.js">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Expand Down
40 changes: 40 additions & 0 deletions src/Core/FileHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,46 @@ public static async Task SaveBytesAsFileAsync(string fileName, byte[] buffer, st

}

public static async Task SaveStringAsFileAsync(string fileName, string fileContent, string filetypefriendlyname, string filetype)
{
// Create a file picker
FileSavePicker savePicker = new()
{
// Set options for your file picker
SuggestedStartLocation = PickerLocationId.DocumentsLibrary
};

// Dropdown of file types the user can save the file as
savePicker.FileTypeChoices.Add(filetypefriendlyname, new List<string>() { filetype });
// Default file name if the user does not type one in or select a file to replace
savePicker.SuggestedFileName = fileName;

// Open the picker for the user to pick a file
StorageFile file = await savePicker.PickSaveFileAsync();
if (file != null)
{
// Prevent updates to the remote version of the file until we finish making changes and call CompleteUpdatesAsync.
CachedFileManager.DeferUpdates(file);

// write to file
// Another way to write a string to the file is to use this instead:
await FileIO.WriteTextAsync(file, fileContent);

// Let Windows know that we're finished changing the file so the other app can update the remote version of the file.
// Completing updates may require Windows to ask for user input.
FileUpdateStatus status = await CachedFileManager.CompleteUpdatesAsync(file);
if (status == FileUpdateStatus.Complete)
{
NotificationHelper.NotifyUser("Success", "File " + file.Name + " was saved to\n" + file.Path);
}
else
{
await UI.ShowDialog("Error", "File " + file.Name + " couldn't be saved.");
}
}

}

public static async Task DeleteLocalFile(string fileName)
{
var file = await ApplicationData.Current.LocalFolder.TryGetItemAsync(fileName);
Expand Down
14 changes: 13 additions & 1 deletion src/Pages/WebViewPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,19 @@
<MenuFlyoutItem
Click="MenuFlyoutItem_Click"
Tag="ExportAsPdf"
Text="Export as pdf" />
Text="Export as pdf">
<MenuFlyoutItem.Icon>
<FontIcon Glyph="&#xEA90;" />
</MenuFlyoutItem.Icon>
</MenuFlyoutItem>
<MenuFlyoutItem
Click="MenuFlyoutItem_Click"
Tag="ExportAsTxt"
Text="Export as txt">
<MenuFlyoutItem.Icon>
<SymbolIcon Symbol="Document" />
</MenuFlyoutItem.Icon>
</MenuFlyoutItem>
</MenuFlyout>
</AppBarButton.Flyout>
</AppBarButton>
Expand Down
111 changes: 54 additions & 57 deletions src/Pages/WebViewPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ private async void WebViewControl_Loaded(object sender, RoutedEventArgs e)
}
}

private void WebViewControl_CoreWebView2Initialized(muxc.WebView2 sender, muxc.CoreWebView2InitializedEventArgs args)
private async void WebViewControl_CoreWebView2Initialized(muxc.WebView2 sender, muxc.CoreWebView2InitializedEventArgs args)
{
// WebViewEvents
sender.CoreWebView2.NavigationStarting += CoreWebView2_NavigationStarting;
Expand All @@ -60,9 +60,11 @@ private void WebViewControl_CoreWebView2Initialized(muxc.WebView2 sender, muxc.C
sender.CoreWebView2.LaunchingExternalUriScheme += CoreWebView2_LaunchingExternalUriScheme;
sender.CoreWebView2.IsDocumentPlayingAudioChanged += CoreWebView2_IsDocumentPlayingAudioChanged;
sender.CoreWebView2.IsMutedChanged += CoreWebView2_IsMutedChanged;
sender.WebMessageReceived += Sender_WebMessageReceived;
sender.WebMessageReceived += CoreWebView2_WebMessageReceived;
// Apply WebView2 settings
ApplyWebView2Settings(sender);
string mainscript = "document.addEventListener('keydown', function(event) { if (event.ctrlKey && event.key === 't') { event.preventDefault(); window.chrome.webview.postMessage('ControlT'); } });";
await sender.CoreWebView2.AddScriptToExecuteOnDocumentCreatedAsync(mainscript);
if (launchurl != null)
{
NavigateToUrl(launchurl);
Expand All @@ -74,44 +76,6 @@ private void WebViewControl_CoreWebView2Initialized(muxc.WebView2 sender, muxc.C
}
}

private void Sender_WebMessageReceived(muxc.WebView2 sender, CoreWebView2WebMessageReceivedEventArgs args)
{
// this input has been treated as VERY unsecure input
// DO NOT add anything which could be slighly insecure
if (args.TryGetWebMessageAsString() == "ControlT")
{
MainPageContent.CreateTab("New tab", "\uEC6C", typeof(NewTabPage));
}
}

private void CoreWebView2_IsMutedChanged(CoreWebView2 sender, object args)
{
if (sender.IsMuted)
{
MuteBtn.Content = new FontIcon { FontFamily = new FontFamily("Segoe MDL2 Assets"), Glyph = "\uE74F" };
return;
}
if (!sender.IsMuted)
{
MuteBtn.Content = new FontIcon { FontFamily = new FontFamily("Segoe MDL2 Assets"), Glyph = "\uE767" };
}
}

private void CoreWebView2_IsDocumentPlayingAudioChanged(CoreWebView2 sender, object args)
{
if (sender.IsDocumentPlayingAudio)
{
MuteBtn.Visibility = Visibility.Visible;
return;
}

if (!sender.IsDocumentPlayingAudio)
{
MuteBtn.Visibility = Visibility.Collapsed;
return;
}
}

private void ApplyWebView2Settings(muxc.WebView2 sender)
{
sender.CoreWebView2.Settings.AreDefaultScriptDialogsEnabled = false;
Expand All @@ -124,23 +88,15 @@ private void CoreWebView2_NavigationStarting(CoreWebView2 sender, CoreWebView2Na
LoadingBar.Visibility = Visibility.Visible;
}

private async void CoreWebView2_NavigationCompleted(CoreWebView2 sender, CoreWebView2NavigationCompletedEventArgs args)
private void CoreWebView2_NavigationCompleted(CoreWebView2 sender, CoreWebView2NavigationCompletedEventArgs args)
{
if (SettingsViewModel.SettingsVM.IsForceDarkEnabled)
{
string jscript = await Modules.ForceDark.ForceDarkHelper.GetForceDarkScriptAsync();
await sender.ExecuteScriptAsync(jscript);
}
string mainscript = "document.addEventListener('keydown', function(event) { if (event.ctrlKey && event.key === 't') { event.preventDefault(); window.chrome.webview.postMessage('ControlT'); } });";
await sender.ExecuteScriptAsync(mainscript);
LoadingBar.Visibility = Visibility.Collapsed;
LoadingBar.IsIndeterminate = false;
}

private void CoreWebView2_SourceChanged(CoreWebView2 sender, CoreWebView2SourceChangedEventArgs args)
{
string uri = sender.Source;
UrlBox.Text = uri;
UrlBox.Text = sender.Source;
}

private void CoreWebView2_FaviconChanged(CoreWebView2 sender, object args)
Expand Down Expand Up @@ -223,6 +179,44 @@ private async void CoreWebView2_LaunchingExternalUriScheme(CoreWebView2 sender,
await Launcher.LaunchUriAsync(new Uri(args.Uri));
}

private void CoreWebView2_IsDocumentPlayingAudioChanged(CoreWebView2 sender, object args)
{
if (sender.IsDocumentPlayingAudio)
{
MuteBtn.Visibility = Visibility.Visible;
return;
}

if (!sender.IsDocumentPlayingAudio)
{
MuteBtn.Visibility = Visibility.Collapsed;
return;
}
}

private void CoreWebView2_IsMutedChanged(CoreWebView2 sender, object args)
{
if (sender.IsMuted)
{
MuteBtn.Content = new FontIcon { FontFamily = new FontFamily("Segoe MDL2 Assets"), Glyph = "\uE74F" };
return;
}
if (!sender.IsMuted)
{
MuteBtn.Content = new FontIcon { FontFamily = new FontFamily("Segoe MDL2 Assets"), Glyph = "\uE767" };
}
}

private void CoreWebView2_WebMessageReceived(muxc.WebView2 sender, CoreWebView2WebMessageReceivedEventArgs args)
{
// this input has been treated as VERY unsecure input
// DO NOT add anything which could be slighly insecure
if (args.TryGetWebMessageAsString() == "ControlT")
{
MainPageContent.CreateTab("New tab", "\uEC6C", typeof(NewTabPage));
}
}

private async void AppBarButton_Click(object sender, RoutedEventArgs e)
{
switch ((sender as AppBarButton).Tag)
Expand Down Expand Up @@ -280,18 +274,22 @@ private async void MenuFlyoutItem_Click(object sender, RoutedEventArgs e)
switch ((sender as MenuFlyoutItem).Tag)
{
case "ExportAsPdf":
string websitetitle = WebViewControl.CoreWebView2.DocumentTitle;
using (IRandomAccessStream fileStream = await WebViewControl.CoreWebView2.PrintToPdfStreamAsync(null))
{
using (var reader = new DataReader(fileStream.GetInputStreamAt(0)))
{
await reader.LoadAsync((uint)fileStream.Size);
var buffer = new byte[(int)fileStream.Size];
reader.ReadBytes(buffer);
await FileHelper.SaveBytesAsFileAsync($"{websitetitle}.pdf", buffer, "Portable Document File", ".pdf");
await FileHelper.SaveBytesAsFileAsync($"{WebViewControl.CoreWebView2.DocumentTitle}.pdf", buffer, "Portable Document File", ".pdf");
}
}
break;
case "ExportAsTxt":
string script = "document.body.innerText";
string textContent = await WebViewControl.CoreWebView2.ExecuteScriptAsync(script);
await FileHelper.SaveStringAsFileAsync($"{WebViewControl.CoreWebView2.DocumentTitle}", textContent, "Text file", ".txt");
break;
case "ViewSource":
MainPageContent.CreateWebTab("view-source:" + WebViewControl.Source);
break;
Expand All @@ -310,6 +308,9 @@ private void AddFavoriteButton_Click(object sender, RoutedEventArgs e)
AddFavoriteFlyout.Hide();
}

#region "UrlBox event handlers"
private void UrlBox_GotFocus(object sender, RoutedEventArgs e) => (sender as TextBox).SelectAll();

private void UrlBox_KeyDown(object sender, KeyRoutedEventArgs e)
{
if (e.Key == VirtualKey.Enter)
Expand All @@ -336,11 +337,7 @@ private void AISearchButton_Click(object sender, RoutedEventArgs e)
NavigateToUrl(AISearchUrl + query);
}
}

private void UrlBox_GotFocus(object sender, RoutedEventArgs e)
{
UrlBox.SelectAll();
}
#endregion

private void NavigateToUrl(string uri)
{
Expand Down

0 comments on commit d835f56

Please sign in to comment.