-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathWidgetOperations.cs
93 lines (76 loc) · 3.26 KB
/
WidgetOperations.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
using Microsoft.VisualBasic;
using Microsoft.Web.WebView2.WinForms;
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Policy;
using System.Text;
namespace sigmanuts_webview2
{
public class WidgetOperations
{
public static string CacheFolderPath => Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Sigmanuts");
public string WidgetsFolder = Path.Combine(CacheFolderPath, @".\localserver\widgets");
public static async void CreateWidget(string widgetName, Microsoft.Web.WebView2.Wpf.WebView2 appView)
{
CreateWidgetFolder(widgetName);
string PathToHTML = Path.Combine(CacheFolderPath, @$".\localserver\widgets\{widgetName}\src\html.html");
string PathToCSS = Path.Combine(CacheFolderPath, @$".\localserver\widgets\{widgetName}\src\css.css");
string PathToJS = Path.Combine(CacheFolderPath, @$".\localserver\widgets\{widgetName}\src\js.js");
string EssentialJS = File.ReadAllText(Path.Combine(CacheFolderPath, @$".\localserver\widgets\{widgetName}\js.html"));
string HTML = "";
if (File.Exists(PathToHTML))
{
HTML = File.ReadAllText(PathToHTML);
}
string CSS = "";
if (File.Exists(PathToHTML))
{
CSS = File.ReadAllText(PathToCSS);
}
string JS = "";
if (File.Exists(PathToHTML))
{
JS = File.ReadAllText(PathToJS);
}
string widgetHTML = $"<html>" +
$"<head>" +
$"<meta http-equiv=\"Cache-control\" content=\"no-cache\">" +
$"<style>" +
$"{CSS}" +
$"</style>" +
$"</head>" +
$"<body>" +
$"{HTML}" +
$"{EssentialJS}" +
$"<script type=\"text/javascript\">" +
$"{JS}" +
$"</script>" +
$"</body>" +
$"</html>";
string[] lines =
{
widgetHTML
};
await File.WriteAllLinesAsync(Path.Combine(CacheFolderPath, @$".\localserver\widgets\{widgetName}\widget.html"), lines);
await appView.CoreWebView2.ExecuteScriptAsync($"retrieveData().then(updateUI()); $('iframe').attr('src', `widgets/{widgetName}/widget.html`)");
}
public static string CreateWidgetFolder(string widgetName)
{
string fileToCopy = Path.Combine(CacheFolderPath, @$".\localserver\js.html");
string widgetDirectory = Path.Combine(CacheFolderPath, @$".\localserver\widgets\{widgetName}");
string srcDirectory = Path.Combine(widgetDirectory, "src");
Directory.CreateDirectory(widgetDirectory);
Directory.CreateDirectory(srcDirectory);
if (File.Exists(Path.Combine(widgetDirectory, "js.html")))
{
File.Delete(Path.Combine(widgetDirectory, "js.html"));
}
File.Copy(fileToCopy, Path.Combine(widgetDirectory, "js.html"));
return srcDirectory;
}
public void DeleteWidget(string widgetName)
{
}
}
}