{"id":13466856,"url":"https://github.com/jameschch/Blazor.DynamicJavascriptRuntime.Evaluator","last_synced_at":"2025-03-26T00:31:35.361Z","repository":{"id":116204815,"uuid":"198990604","full_name":"jameschch/Blazor.DynamicJavascriptRuntime.Evaluator","owner":"jameschch","description":"Execute dynamic object expressions as Javascript in Blazor","archived":false,"fork":false,"pushed_at":"2023-01-31T16:54:56.000Z","size":61,"stargazers_count":19,"open_issues_count":0,"forks_count":4,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-20T21:39:02.995Z","etag":null,"topics":["blazor","dynamic","javascript","jsinterop","razor","wasm","webassembly"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jameschch.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2019-07-26T09:41:55.000Z","updated_at":"2025-03-19T21:48:17.000Z","dependencies_parsed_at":"2024-01-07T10:25:08.819Z","dependency_job_id":"4296523a-08b7-47d0-97b5-35e23da622ed","html_url":"https://github.com/jameschch/Blazor.DynamicJavascriptRuntime.Evaluator","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jameschch%2FBlazor.DynamicJavascriptRuntime.Evaluator","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jameschch%2FBlazor.DynamicJavascriptRuntime.Evaluator/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jameschch%2FBlazor.DynamicJavascriptRuntime.Evaluator/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jameschch%2FBlazor.DynamicJavascriptRuntime.Evaluator/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jameschch","download_url":"https://codeload.github.com/jameschch/Blazor.DynamicJavascriptRuntime.Evaluator/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245566098,"owners_count":20636390,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["blazor","dynamic","javascript","jsinterop","razor","wasm","webassembly"],"created_at":"2024-07-31T15:00:50.768Z","updated_at":"2025-03-26T00:31:34.950Z","avatar_url":"https://github.com/jameschch.png","language":"C#","funding_links":[],"categories":["Libraries \u0026 Extensions"],"sub_categories":["Others"],"readme":"# Blazor.DynamicJavascriptRuntime.Evaluator (A.K.A. Gasmask)\n\nWouldn't it be really useful if you could run a line or two of Javascript in your Blazor C# app?\nWouldn't it be handy if you could execute arbitrary Javascript at runtime without strings of script?\nWouldn't it be a big plus if the Javascript code was declared as a dynamic object expression and could be exposed to unit tests?\nWouldn't it be nice if you could consume Javascript library API's without creating interop wrappers?\n\nCalling Javascript dynamically from C# couldn't be easier:\n\n```csharp\nvar context = (new EvalContext(JSRuntimeInstance) as dynamic).window.location = \"www.github.com\";\nawait (context as EvalContext).InvokeAsync\u003cstring\u003e();\n//window.location = \"www.github.com\";\n```\n\n...or with alternate syntax:\n\n```csharp\nusing (dynamic context = new EvalContext(JSRuntimeInstance))\n{\n\t(context as EvalContext).Expression = () =\u003e context.window.location = \"www.github.com\";\n\t//window.location = \"www.github.com\";\n}\n```\n\nWhen it comes to executing code with arguments, the C# parser will support this:\n\n```csharp\nusing (dynamic context = new EvalContext(JSRuntimeInstance))\n{\n\tdynamic arg = new EvalContext(JSRuntimeInstance);\n\tdynamic arg2 = new EvalContext(JSRuntimeInstance);\n\t(context as EvalContext).Expression = () =\u003e context.Chart.config.data.datasets[arg.i].data.push(arg2.config.data.datasets[arg.i].data);\n\t//Chart.config.data.datasets[i].data.push(config.data.datasets[i].data);\n}\n```\n\nNeed to call some script that returns a value? No problem:\n\n```csharp\ndynamic context = new EvalContext(runtime.Object);\n(context as EvalContext).Expression = () =\u003e context.document.cookie;           \nvar cookie = await (context as EvalContext).InvokeAsync\u003cstring\u003e();\n//document.cookie\n```\n\nIn order to satisfy the C# parser, an underscore (\"_\") can stand in for a space character in Javascript. This is disabled by default and you can configure your space placeholder:\n\n```csharp\nusing (dynamic context = new EvalContext(JSRuntimeInstance))\n{\n\tvar settings = new EvalContextSettings { EnableSpaceCharacterPlaceholderReplacement = true, SpaceCharacterPlaceholder = \"_\" };\n\tdynamic arg = new EvalContext(JSRuntimeInstance, settings);\n\t(context as EvalContext).Expression = () =\u003e context.var_instance = arg.new_object();\n\t//var instance = new object();\n}\n```\n\nThe dynamic expression is eagerly evaluated. This means decimal arithmetic will not be mangled by Javascript:\n\n```csharp\nusing (dynamic context = new EvalContext(JSRuntimeInstance))\n{\n\t(context as EvalContext).Expression = () =\u003e context.sum = 0.1M + 0.2M * 0.5M / 0.5M;\n\t//sum = 0.3;\n}\n```\n\nMaybe you feel like a bit of JQuery?\n\n```csharp\nusing (dynamic context = new EvalContext(JsRuntime))\n{\n    (context as EvalContext).Expression = () =\u003e context.jQuery(\"body\").css(\"overflow-y\", \"hidden\");\n\t//jQuery(\"body\").css(\"overflow-y\", \"hidden\")\n}\n```\nSorry, no $ allowed.\n\nHow about passing complex types as arguments? We've got you covered for anonymous types:\n\n```csharp\nusing (dynamic context = new EvalContext(JsRuntime))\n{\n    var arg = new { Property = \"Value\", Field = 123, Child = new { Member = new DateTime(2001, 1, 1) } };\n    (context as EvalContext).Expression = () =\u003e context.myScript.set(arg);\n\t//myScript.set({\"property\":\"Value\",\"field\":123,\"child\":{\"member\":\"2001-01-01T00:00:00\"}})\n}\n```\n\nPassing user-defined types takes more effort, but not too much:\n\n```csharp\nvar settings = new EvalContextSettings();\nsettings.SerializableTypes.Add(typeof(Specified));\nusing (dynamic context = new EvalContext(JsRuntime, settings))\n{\n    var arg = new Specified { Member = \"abc\" };\n    (context as EvalContext).Expression = () =\u003e context.myScript.setSpecified(arg);\n\t//myScript.setSpecified({\"member\":\"abc\"})\n}\n```\n\nThe execution of Javascript is performed with the eval() function, so it's imperative to sanitize user input that's passed into the Javascript runtime. You have been warned.\n\n## Setup\n\nFirst, install from nuget:\n\n```\nInstall-Package DynamicJavascriptRuntime.Blazor.Evaluator\n```\n\n[https://www.nuget.org/packages/DynamicJavascriptRuntime.Blazor.Evaluator/](https://www.nuget.org/packages/DynamicJavascriptRuntime.Blazor.Evaluator/)\n\nYou then need to add a script include to your index.htm:\n\n```html\n    \u003cscript src=\"_content/DynamicJavascriptRuntime.Blazor.Evaluator/BlazorDynamicJavascriptRuntime.js\"\u003e\u003c/script\u003e\n```\n\n## Syntax\n\nThe are a few different syntax options for dynamic expressions:\n\n- The ```using``` blocks wrapping the EvalContext are optional but prevent forgotten calls to Invoke.\n- Setting the ```Expression``` property is optional. You can chain an expression directly on the EvalContext e.g.:\n```csharp\ndynamic context = new EvalContext();\ncontext.alert(\"Gasmask\");\nawait (context as EvalContext).InvokeVoidAsync()\n```\n- You can chain multiple javascript calls on separate lines with a single EvalContext. This makes sense for some \"fluent\" libraries like jQuery.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjameschch%2FBlazor.DynamicJavascriptRuntime.Evaluator","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjameschch%2FBlazor.DynamicJavascriptRuntime.Evaluator","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjameschch%2FBlazor.DynamicJavascriptRuntime.Evaluator/lists"}