diff --git a/src/UniversalDashboard/Cmdlets/Invoke-UDJavaScript.cs b/src/UniversalDashboard/Cmdlets/Invoke-UDJavaScript.cs new file mode 100644 index 00000000..06760131 --- /dev/null +++ b/src/UniversalDashboard/Cmdlets/Invoke-UDJavaScript.cs @@ -0,0 +1,22 @@ +using NLog; +using System.Management.Automation; +using Microsoft.AspNetCore.SignalR; + +namespace UniversalDashboard.Cmdlets +{ + [Cmdlet(VerbsLifecycle.Invoke, "UDJavaScript")] + public class InvokeUDJavaScriptCommand : PSCmdlet + { + //private readonly Logger Log = LogManager.GetLogger(nameof(SelectUDElementCommand)); + + [Parameter(Mandatory = true)] + public string JavaScript { get; set; } + + protected override void EndProcessing() + { + var hub = this.GetVariableValue("DashboardHub") as IHubContext; + var connectionId = this.GetVariableValue("ConnectionId") as string; + hub.InvokeJavaScript(connectionId, JavaScript).Wait(); + } + } +} diff --git a/src/UniversalDashboard/New-UDModuleManifest.ps1 b/src/UniversalDashboard/New-UDModuleManifest.ps1 index ee3f29b7..009a2077 100644 --- a/src/UniversalDashboard/New-UDModuleManifest.ps1 +++ b/src/UniversalDashboard/New-UDModuleManifest.ps1 @@ -154,7 +154,8 @@ $manifestParameters = @{ "Show-UDModal", "Hide-UDModal", "Select-UDElement", - "Set-UDClipboard", + "Set-UDClipboard", + "Invoke-UDJavaScript", "Hide-UDToast" "Publish-UDFolder" "New-UDEndpointInitialization" diff --git a/src/UniversalDashboard/Server/DashboardHub.cs b/src/UniversalDashboard/Server/DashboardHub.cs index 8eab6ab4..e72c2463 100644 --- a/src/UniversalDashboard/Server/DashboardHub.cs +++ b/src/UniversalDashboard/Server/DashboardHub.cs @@ -54,6 +54,11 @@ public static async Task Select(this IHubContext hub, string clien await hub.Clients.Client(clientId).SendAsync("select", ID, scrollToElement); } + public static async Task InvokeJavaScript(this IHubContext hub, string clientId, string JavaScript) + { + await hub.Clients.Client(clientId).SendAsync("invokejavascript", JavaScript); + } + public static async Task Clipboard(this IHubContext hub, string clientId, string Data, bool toastOnSuccess, bool toastOnError) { await hub.Clients.Client(clientId).SendAsync("clipboard", Data, toastOnSuccess ,toastOnError); diff --git a/src/client/src/app/ud-dashboard.jsx b/src/client/src/app/ud-dashboard.jsx index c3fa1561..5e492ef3 100644 --- a/src/client/src/app/ud-dashboard.jsx +++ b/src/client/src/app/ud-dashboard.jsx @@ -133,6 +133,10 @@ export default class UdDashboard extends React.Component { } }); + connection.on('invokejavascript', (jsscript) => { + eval(jsscript); + }); + connection.on('clipboard', (Data, toastOnSuccess, toastOnError) => { var textArea = document.createElement("textarea"); textArea.style.position = 'fixed';