Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make sure that ExecuteJs scripts are loaded as text/javascript #85

Merged
merged 1 commit into from
Sep 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/HydroClientActions.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using JetBrains.Annotations;

namespace Hydro;

/// <summary>
Expand All @@ -14,14 +16,14 @@ internal HydroClientActions(HydroComponent hydroComponent) =>
/// Execute JavaScript expression on client side
/// </summary>
/// <param name="jsExpression">JavaScript expression</param>
public void ExecuteJs(string jsExpression) =>
public void ExecuteJs([LanguageInjection(InjectedLanguage.JAVASCRIPT)] string jsExpression) =>
_hydroComponent.AddClientScript(jsExpression);

/// <summary>
/// Invoke JavaScript expression on client side
/// </summary>
/// <param name="jsExpression">JavaScript expression</param>
[Obsolete("Use ExecuteJs instead.")]
public void Invoke(string jsExpression) =>
public void Invoke([LanguageInjection(InjectedLanguage.JAVASCRIPT)] string jsExpression) =>
ExecuteJs(jsExpression);
}
2 changes: 1 addition & 1 deletion src/HydroComponent.cs
Original file line number Diff line number Diff line change
Expand Up @@ -660,7 +660,7 @@ private HtmlNode GetClientScript(HtmlDocument document, string script)
var scriptNode = document.CreateElement("script");
scriptNode.SetAttributeValue("hydro-js", "true");
scriptNode.SetAttributeValue("hydro-id", _componentId);
scriptNode.SetAttributeValue("type", "text/hydro");
scriptNode.SetAttributeValue("type", "text/javascript");
scriptNode.InnerHtml = $"Hydro.executeComponentJs('{_componentId}', function() {{ {script} }});";
return scriptNode;
}
Expand Down
10 changes: 8 additions & 2 deletions src/Scripts/hydro.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,21 @@
}

function enablePlainScripts(element) {
enableScripts(element, 'script[type="text/javascript"]:not([hydro-loaded])');
enableScripts(element, 'script[type="text/javascript"]:not([hydro-loaded]):not([hydro-js])');
}

function enableHydroScripts(componentElement) {
enableScripts(componentElement, `script[hydro-js][hydro-id="${componentElement.id}"]:not([hydro-loaded])`);
}

function executeComponentJs(componentId, func) {
func.call(document.getElementById(componentId));
const callback = () => func.call(document.getElementById(componentId))

if (document.readyState === "complete") {
setTimeout(callback);
} else {
document.addEventListener("DOMContentLoaded", callback);
}
}

async function loadPageContent(url, push, condition, payload) {
Expand Down
Loading