将浏览器标准 JavaScript API 完整映射到 C#,让 .NET 开发人员能够使用熟悉的 C# 语法操作浏览器原生功能。
- 🔄 完整的 JS 互操作 - 使用 .NET 7+ 的
[JSImport]/[JSExport特性 - 🌐 纯 WebAssembly - 无需 Blazor 依赖,轻量级编译输出
- 📦 类型安全 - 强类型 C# API,覆盖主流浏览器对象
- 🛠️ 开箱即用 - 预置 Window、Console、Document、Storage 等核心 API
CSharpScript/
├── src/CSharpScript/
│ ├── CSharpScript.sln
│ └── CSharpScript/
│ ├── CSharpScript.csproj
│ ├── GlobalUsings.cs
│ └── Browser/
│ ├── Window.cs # window 对象
│ ├── Console.cs # console 对象
│ ├── Document.cs # document 对象
│ ├── Location.cs # location 对象
│ ├── Navigator.cs # navigator 对象
│ ├── History.cs # history 对象
│ └── Storage/
│ ├── LocalStorage.cs
│ └── SessionStorage.cs
| 模块 | 状态 | 描述 |
|---|---|---|
| Window | ✅ 已完成 | alert, confirm, prompt, setTimeout, scroll, resize 等 |
| Console | ✅ 已完成 | log, info, warn, error, debug, clear 等 |
| Document | 🔄 进行中 | querySelector, getElementById 等 |
| Location | ✅ 已完成 | href, pathname, search 等属性 |
| Navigator | ✅ 已完成 | userAgent, language, platform 等 |
| History | ✅ 已完成 | pushState, replaceState, back, forward 等 |
| LocalStorage | ✅ 已完成 | getItem, setItem, removeItem, clear 等 |
| SessionStorage | ✅ 已完成 | getItem, setItem, removeItem, clear 等 |
总体进度: 7/8 模块已完成 (87.5%)
| 技术 | 版本 |
|---|---|
| .NET SDK | 8.0 |
| 目标框架 | net8.0 |
| 运行时 | browser-wasm |
| 互操作 | JSImport / JSExport |
git clone https://github.com/your-username/CSharpScript.git
cd CSharpScriptcd src/CSharpScript
dotnet buildusing CSharpScript.Browser;
// 控制台输出
Console.Log("Hello from C#!");
Console.Info("This is an info message");
Console.Warn("Warning: something happened");
Console.Error("Error occurred!");
// 窗口操作
Window.Alert("Hello World!");
bool confirmed = Window.Confirm("Are you sure?");
string? input = Window.Prompt("Enter your name:", "Guest");
// 存储 API
LocalStorage.SetItem("user", "John");
string? user = LocalStorage.GetItem("user");
LocalStorage.RemoveItem("user");
// 页面导航
History.PushState(null, "", "/new-page");
History.Back();
// 获取窗口尺寸
int width = Window.GetInnerWidth();
int height = Window.GetInnerHeight();<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
</Project>// 使用 JSImport 导入 JavaScript 函数
[SupportedOSPlatform("browser")]
public static partial class Window
{
[JSImport("globalThis.window.alert")]
public static partial void Alert(string message);
[JSImport("globalThis.window.setTimeout")]
public static partial int SetTimeout(Action callback, int delay);
}欢迎提交 Issue 和 Pull Request!
- Fork 本仓库
- 创建特性分支 (
git checkout -b feature/AmazingFeature) - 提交更改 (
git commit -m 'Add some AmazingFeature') - 推送分支 (
git push origin feature/AmazingFeature) - 开启 Pull Request
暂无
Made with ❤️ using .NET 8 + WebAssembly