5 changed files with 120 additions and 3 deletions
@ -0,0 +1,72 @@ |
|||
using System; |
|||
using CefSharp; |
|||
using System.IO; |
|||
using System.Net; |
|||
using System.Reflection; |
|||
using System.Threading.Tasks; |
|||
|
|||
namespace 齐越慧眼.cefhelper |
|||
{ |
|||
|
|||
public class ResourceSchemeHandler : ResourceHandler |
|||
{ |
|||
public override CefReturnValue ProcessRequestAsync(IRequest request, ICallback callback) |
|||
{ |
|||
var names = this.GetType().Assembly.GetManifestResourceNames(); |
|||
|
|||
Uri u = new Uri(request.Url); |
|||
String file = u.Authority + u.AbsolutePath; // 注:目录名需全为小写字母,否则将无法得到 Resource
|
|||
|
|||
Assembly ass = Assembly.GetExecutingAssembly(); |
|||
String resourcePath = ass.GetName().Name + "." + file.Replace("/", "."); // 你可以设置断点看看这里的值
|
|||
|
|||
Task.Run(() => |
|||
{ |
|||
using (callback) |
|||
{ |
|||
if (ass.GetManifestResourceInfo(resourcePath) != null) |
|||
{ |
|||
Stream stream = ass.GetManifestResourceStream(resourcePath); |
|||
string mimeType = "application/octet-stream"; |
|||
switch (Path.GetExtension(file)) |
|||
{ |
|||
case ".html": |
|||
mimeType = "text/html"; |
|||
break; |
|||
case ".js": |
|||
mimeType = "text/javascript"; |
|||
break; |
|||
case ".css": |
|||
mimeType = "text/css"; |
|||
break; |
|||
case ".png": |
|||
mimeType = "image/png"; |
|||
break; |
|||
case ".appcache": |
|||
break; |
|||
case ".manifest": |
|||
mimeType = "text/cache-manifest"; |
|||
break; |
|||
} |
|||
|
|||
// Reset the stream position to 0 so the stream can be copied into the underlying unmanaged buffer
|
|||
stream.Position = 0; |
|||
// Populate the response values - No longer need to implement GetResponseHeaders (unless you need to perform a redirect)
|
|||
ResponseLength = stream.Length; |
|||
MimeType = mimeType; |
|||
StatusCode = (int)HttpStatusCode.OK; |
|||
Stream = stream; |
|||
|
|||
callback.Continue(); |
|||
} |
|||
else |
|||
{ |
|||
callback.Cancel(); |
|||
} |
|||
} |
|||
}); |
|||
|
|||
return CefReturnValue.Continue; |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,25 @@ |
|||
using System; |
|||
using System.Collections.Generic; |
|||
using System.Linq; |
|||
using System.Text; |
|||
using System.Threading.Tasks; |
|||
using CefSharp; |
|||
|
|||
namespace 齐越慧眼.cefhelper |
|||
{ |
|||
class ResourceSchemeHandlerFactory : ISchemeHandlerFactory |
|||
{ |
|||
public IResourceHandler Create(IBrowser browser, IFrame frame, string schemeName, IRequest request) |
|||
{ |
|||
return new ResourceSchemeHandler(); |
|||
} |
|||
|
|||
public static string SchemeName |
|||
{ |
|||
get |
|||
{ |
|||
return "nacollector"; // 这里我设置的 SchemeName 为 nacollector,当然你也可以改成其他的
|
|||
} |
|||
} |
|||
} |
|||
} |
Loading…
Reference in new issue