fasdfsa commited on
Commit
121e5f3
·
1 Parent(s): 6667126

fix mdx 页面内词条跳转

Browse files
csharp/mdict/App.xaml.cs CHANGED
@@ -5,6 +5,7 @@ using System.Reflection;
5
  using System.Windows;
6
  using CefSharp;
7
  using CefSharp.Wpf;
 
8
 
9
  namespace mdict
10
  {
@@ -23,6 +24,16 @@ namespace mdict
23
  {
24
  var settings = new CefSettings();
25
 
 
 
 
 
 
 
 
 
 
 
26
  // Set cache path to a writable directory
27
  string exePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? string.Empty;
28
  string cachePath = Path.Combine(exePath, "Cache");
 
5
  using System.Windows;
6
  using CefSharp;
7
  using CefSharp.Wpf;
8
+ using mdict.Services;
9
 
10
  namespace mdict
11
  {
 
24
  {
25
  var settings = new CefSettings();
26
 
27
+ // Register mdx scheme
28
+ settings.RegisterScheme(new CefCustomScheme
29
+ {
30
+ SchemeName = "mdx",
31
+ SchemeHandlerFactory = new MdxSchemeHandlerFactory(),
32
+ IsStandard = true,
33
+ IsSecure = true,
34
+ IsCorsEnabled = false
35
+ });
36
+
37
  // Set cache path to a writable directory
38
  string exePath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) ?? string.Empty;
39
  string cachePath = Path.Combine(exePath, "Cache");
csharp/mdict/Services/LibraryManager.cs CHANGED
@@ -91,6 +91,40 @@ namespace mdict.Services
91
  return new List<string>();
92
  }
93
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94
  public void Dispose()
95
  {
96
  foreach (var dict in LoadedDictionaries)
 
91
  return new List<string>();
92
  }
93
 
94
+ public string? GetHtml(string key)
95
+ {
96
+ foreach (var dict in LoadedDictionaries)
97
+ {
98
+ try
99
+ {
100
+ var index = dict.Reader.Lookup(key);
101
+ if (index != null)
102
+ {
103
+ return dict.Reader.GetHtml(index);
104
+ }
105
+ }
106
+ catch { }
107
+ }
108
+ return null;
109
+ }
110
+
111
+ public byte[]? GetResource(string key)
112
+ {
113
+ foreach (var dict in LoadedDictionaries)
114
+ {
115
+ try
116
+ {
117
+ var data = dict.Reader.GetResource(key);
118
+ if (data != null)
119
+ {
120
+ return data;
121
+ }
122
+ }
123
+ catch { }
124
+ }
125
+ return null;
126
+ }
127
+
128
  public void Dispose()
129
  {
130
  foreach (var dict in LoadedDictionaries)
csharp/mdict/Services/MdxSchemeHandler.cs ADDED
@@ -0,0 +1,84 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ using System;
2
+ using System.IO;
3
+ using System.Text;
4
+ using System.Web;
5
+ using CefSharp;
6
+ using mdict.Services;
7
+
8
+ namespace mdict.Services
9
+ {
10
+ public class MdxSchemeHandlerFactory : ISchemeHandlerFactory
11
+ {
12
+ public IResourceHandler Create(IBrowser browser, IFrame frame, string schemeName, IRequest request)
13
+ {
14
+ var uri = new Uri(request.Url);
15
+ var path = uri.AbsolutePath.TrimStart('/');
16
+ var query = HttpUtility.ParseQueryString(uri.Query);
17
+
18
+ // Path format: mdict.cn/service/{action}
19
+ // uri.AbsolutePath will be /mdict.cn/service/{action} if host is empty or treated as path
20
+ // But usually mdx://host/path.
21
+ // User URL: mdx://mdict.cn/service/entry?profile_id=0&key=indices
22
+ // Host: mdict.cn
23
+ // Path: /service/entry
24
+
25
+ string action = "";
26
+ if (uri.Segments.Length > 0)
27
+ {
28
+ action = uri.Segments[uri.Segments.Length - 1]; // "entry"
29
+ }
30
+
31
+ if (string.Equals(action, "entry", StringComparison.OrdinalIgnoreCase))
32
+ {
33
+ string key = query["key"];
34
+ if (!string.IsNullOrEmpty(key))
35
+ {
36
+ string html = LibraryManager.Instance.GetHtml(key);
37
+ if (html != null)
38
+ {
39
+ return ResourceHandler.FromString(html, Encoding.UTF8);
40
+ }
41
+ }
42
+ }
43
+ else if (string.Equals(action, "mdd", StringComparison.OrdinalIgnoreCase) ||
44
+ string.Equals(action, "sound", StringComparison.OrdinalIgnoreCase))
45
+ {
46
+ string key = query["key"];
47
+ if (!string.IsNullOrEmpty(key))
48
+ {
49
+ byte[] data = LibraryManager.Instance.GetResource(key);
50
+ if (data != null)
51
+ {
52
+ string mimeType = GetMimeType(key);
53
+ return ResourceHandler.FromStream(new MemoryStream(data), mimeType);
54
+ }
55
+ }
56
+ }
57
+
58
+ // 404
59
+ return ResourceHandler.ForErrorMessage("Resource not found", System.Net.HttpStatusCode.NotFound);
60
+ }
61
+
62
+ private string GetMimeType(string fileName)
63
+ {
64
+ string ext = Path.GetExtension(fileName).ToLower();
65
+ return ext switch
66
+ {
67
+ ".css" => "text/css",
68
+ ".js" => "text/javascript",
69
+ ".png" => "image/png",
70
+ ".jpg" => "image/jpeg",
71
+ ".jpeg" => "image/jpeg",
72
+ ".gif" => "image/gif",
73
+ ".bmp" => "image/bmp",
74
+ ".webp" => "image/webp",
75
+ ".svg" => "image/svg+xml",
76
+ ".mp3" => "audio/mpeg",
77
+ ".wav" => "audio/wav",
78
+ ".ogg" => "audio/ogg",
79
+ ".spx" => "audio/ogg", // Chrome might handle it?
80
+ _ => "application/octet-stream"
81
+ };
82
+ }
83
+ }
84
+ }
readme.txt CHANGED
@@ -162,6 +162,40 @@ ContentBrowser.LoadHtml(content, "http://mdict/" + item.Key);
162
 
163
 
164
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
165
 
166
 
167
  比较有用的提示词:
 
162
 
163
 
164
 
165
+ `e:\huggingface\WpfMdict\rust\mdict` 参考这个项目, `e:\huggingface\WpfMdict\csharp\mdict` 修复这个项目查询词典显示词条后,浏览器内部链接跳转失败的问题:无法访问此网站
166
+ 网址为 mdx://mdict.cn/service/entry?profile_id=0&key=indices 的网页可能暂时无法连接,或者它已永久性地移动到了新网址。
167
+ ERR_UNKNOWN_URL_SCHEME
168
+
169
+
170
+ 已通过注册自定义 Scheme Handler 修复了浏览器无法处理 mdx:// 协议的问题。
171
+
172
+ 主要修改:
173
+ 实现 MdxSchemeHandler: 在 e:\huggingface\WpfMdict\csharp\mdict\Services\MdxSchemeHandler.cs 中实现了 ISchemeHandlerFactory,用于处理 mdx:// 协议的请求。
174
+
175
+ Action entry: 提取 key 参数,调用 LibraryManager 查找并返回词条 HTML。
176
+ Action mdd / sound: 提取 key 参数,调用 LibraryManager 查找并返回资源文件(图片、CSS、JS、音频)。
177
+ MIME 类型: 根据文件后缀名自动判断返回的 MIME 类型。
178
+ 更新 LibraryManager: 在 e:\huggingface\WpfMdict\csharp\mdict\Services\LibraryManager.cs 中添加了以下辅助方法:
179
+
180
+ GetHtml(string key): 遍历所有已加载的词典,查找并返回第一个匹配词条的 HTML 内容。
181
+ GetResource(string key): 遍历所有已加载的词典,查找并返回第一个匹配资源的字节数据。
182
+ 注册 Scheme: 在 e:\huggingface\WpfMdict\csharp\mdict\App.xaml.cs 的 InitializeCef 方法中,注册了 mdx 协议:
183
+
184
+ C#
185
+
186
+
187
+
188
+ settings.RegisterScheme(new CefCustomScheme{ SchemeName = "mdx", SchemeHandlerFactory = new MdxSchemeHandlerFactory(), IsStandard = true, IsSecure = true, IsCorsEnabled = false});
189
+ 验证:
190
+ 构建状态: dotnet build 成功,无错误。
191
+ 功能预期:
192
+ 点击词典内的跳转链接(如 entry://key 被重写为 mdx://mdict.cn/service/entry?key=...)时,现在应该能正确加载目标词条。
193
+ 词典中的图片、CSS、JS 等资源(链接如 mdx://mdict.cn/service/mdd?key=\style.css)现在应该能正确加载并显示。
194
+ 不再出现 ERR_UNKNOWN_URL_SCHEME 错误。
195
+ 请运行程序并测试点击词条链接或查看包含图片的词条,确认跳转和资源加载是否正常。
196
+
197
+
198
+
199
 
200
 
201
  比较有用的提示词: