Skip to content

Commit

Permalink
Version 1.3.0.0! Update CefShap and Ui, fix some bugs.
Browse files Browse the repository at this point in the history
  • Loading branch information
qwqcode committed Dec 31, 2018
1 parent db0b527 commit 61c1abc
Show file tree
Hide file tree
Showing 33 changed files with 279 additions and 4,537 deletions.
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "NacollectorFrontend"]
path = NacollectorFrontend
url = https://github.com/qwqcode/NacollectorFrontend
File renamed without changes.
35 changes: 25 additions & 10 deletions Nacollector/Browser/CrBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,23 +26,28 @@ public CrBrowser(MainForm form, string address)
browser = new ChromiumWebBrowser(address);

// BrowserSettings 必须在 Controls.Add 之前
BrowserSettings browserSettings = new BrowserSettings();
// browserSettings.WebSecurity = CefState.Disabled
browserSettings.FileAccessFromFileUrls = CefState.Enabled; // 必须 Enabled 不然 AJAX 请求 file:// 会显示 Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.
browserSettings.UniversalAccessFromFileUrls = CefState.Enabled;
browserSettings.DefaultEncoding = "UTF-8";
browserSettings.BackgroundColor = (uint)ColorTranslator.FromHtml("#333333").ToArgb();
BrowserSettings browserSettings = new BrowserSettings
{
// FileAccessFromFileUrls 必须 Enabled
// 不然 AJAX 请求 file:// 会显示
// Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, https.
FileAccessFromFileUrls = CefState.Enabled,
UniversalAccessFromFileUrls = CefState.Enabled,
DefaultEncoding = "UTF-8",
BackgroundColor = (uint)ColorTranslator.FromHtml("#21252b").ToArgb()
};
browserSettings.WebSecurity = CefState.Disabled;
browser.BrowserSettings = browserSettings;

browser.MenuHandler = new MenuHandler();
browser.MenuHandler = new MenuHandler(this);
browser.LifeSpanHandler = new LifeSpanHandler();
browser.LoadHandler = new LoadHandler();
browser.DragHandler = new DragDropHandler();

browser.FrameLoadEnd += new EventHandler<FrameLoadEndEventArgs>(Browser_onFrameLoadEnd);
browser.IsBrowserInitializedChanged += new EventHandler<IsBrowserInitializedChangedEventArgs>(Browser_onIsBrowserInitializedChanged);
}

// Frame 加载完毕时执行
private void Browser_onFrameLoadEnd(object sender, FrameLoadEndEventArgs e)
{
Expand Down Expand Up @@ -70,8 +75,11 @@ private void Browser_onIsBrowserInitializedChanged(object sender, IsBrowserIniti
}
else if (message.Msg == (int)WindowMessages.WM_LBUTTONDOWN) // 鼠标左键按下
{
NativeMethods.ReleaseCapture();
form.SendHandleMessage(); // 执行 模拟标题栏拖动
form.Invoke((MethodInvoker)delegate
{
NativeMethods.ReleaseCapture();
NativeMethods.SendMessage(form.Handle, (int)WindowMessages.WM_NCLBUTTONDOWN, (int)HitTestValues.HTCAPTION, 0); // 执行 模拟标题栏拖动
});
}
else if (message.Msg == (int)WindowMessages.WM_RBUTTONDOWN) // 鼠标右键按下
{
Expand Down Expand Up @@ -139,5 +147,12 @@ await task.ContinueWith(res => {
}
return result;
}

public void DownloadUrl(string url)
{
var cefBrowser = browser.GetBrowser();
IBrowserHost ibwhost = cefBrowser == null ? null : cefBrowser.GetHost();
ibwhost.StartDownload(url);
}
}
}
4 changes: 2 additions & 2 deletions Nacollector/Browser/CrDownloads.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ private void DownloadDo(string doType, ChromiumWebBrowser browser, EventArgs e)
downloadUrl = downloadItem.OriginalUrl,
totalBytes = downloadItem.TotalBytes,
});
browser.ExecuteScriptAsync($"downloads.addTask({callbackObj})");
browser.ExecuteScriptAsync($"Downloads.addTask({callbackObj})");
}
else if (doType == "update")
{
Expand All @@ -191,7 +191,7 @@ private void DownloadDo(string doType, ChromiumWebBrowser browser, EventArgs e)
fullPath = downloadItem.FullPath,
downloadUrl = downloadItem.Url
});
browser.ExecuteScriptAsync($"downloads.updateTask({callbackObj})");
browser.ExecuteScriptAsync($"Downloads.updateTask({callbackObj})");
}
}
}
Expand Down
16 changes: 9 additions & 7 deletions Nacollector/Browser/Handler/MenuHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ namespace Nacollector.Browser.Handler
{
public class MenuHandler : IContextMenuHandler
{
private CrBrowser crBrowser;
private bool _showReload = false;

public MenuHandler(bool showReload = false)
public MenuHandler(CrBrowser crBrowser, bool showReload = false)
{
this.crBrowser = crBrowser;
_showReload = showReload;
}

Expand All @@ -24,7 +26,7 @@ public MenuHandler(bool showReload = false)
private const int SaveLink = 26504;
private const int CopyLink = 26505;
private const int LinkOpenDefaultBrowser = 26506;
private const int LinkToZneiatProject = 26507;
private const int LinkToQWQCODEProject = 26507;
private const int FeedbackProject = 26508;

void IContextMenuHandler.OnBeforeContextMenu(IWebBrowser browserControl, IBrowser browser, IFrame frame, IContextMenuParams parameters, IMenuModel model)
Expand Down Expand Up @@ -80,7 +82,7 @@ void IContextMenuHandler.OnBeforeContextMenu(IWebBrowser browserControl, IBrowse
if (_showReload)
model.AddItem(CefMenuCommand.ReloadNoCache, "刷新 (ReloadNoCache)");
model.AddItem((CefMenuCommand)FeedbackProject, "反馈问题");
model.AddItem((CefMenuCommand)LinkToZneiatProject, "开源项目");
model.AddItem((CefMenuCommand)LinkToQWQCODEProject, "开源项目");
# if DEBUG
model.AddItem((CefMenuCommand)ShowDevTools, "检查 (ShowDevTools)");
# endif
Expand Down Expand Up @@ -114,11 +116,11 @@ bool IContextMenuHandler.OnContextMenuCommand(IWebBrowser browserControl, IBrows
case LinkOpenDefaultBrowser:
System.Diagnostics.Process.Start("explorer.exe", parameters.UnfilteredLinkUrl);
break;
case LinkToZneiatProject:
System.Diagnostics.Process.Start("https://github.com/Zneiat/Nacollector");
case LinkToQWQCODEProject:
System.Diagnostics.Process.Start("https://github.com/qwqcode/Nacollector");
break;
case FeedbackProject:
System.Diagnostics.Process.Start("https://github.com/Zneiat/Nacollector/issues");
System.Diagnostics.Process.Start("https://github.com/qwqcode/Nacollector/issues");
break;
}

Expand All @@ -132,7 +134,7 @@ bool IContextMenuHandler.OnContextMenuCommand(IWebBrowser browserControl, IBrows
/// <param name="frame"></param>
void ExecuteSaveFileByUrl(string url, IFrame frame)
{
frame.ExecuteJavaScriptAsync("var a = document.createElement('a');a.setAttribute('href', \"" + url + "\");a.setAttribute('download','');a.click();");
crBrowser.DownloadUrl(url);
}

void IContextMenuHandler.OnContextMenuDismissed(IWebBrowser browserControl, IBrowser browser, IFrame frame)
Expand Down
62 changes: 62 additions & 0 deletions Nacollector/Browser/Handler/ResourceSchemeHandler .cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
using CefSharp;
using System;
using System.IO;
using System.Net;
using System.Reflection;
using System.Threading.Tasks;

public class ResourceSchemeHandler : ResourceHandler
{
public override bool ProcessRequestAsync(IRequest request, ICallback callback)
{
Uri u = new Uri(request.Url);
String file = u.Authority + u.AbsolutePath;

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 ".png":
mimeType = "image/png";
break;
case ".appcache":
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 true;
}
}
16 changes: 16 additions & 0 deletions Nacollector/Browser/Handler/ResourceSchemeHandlerFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CefSharp;

class ResourceSchemeHandlerFactory : ISchemeHandlerFactory
{
public IResourceHandler Create(IBrowser browser, IFrame frame, string schemeName, IRequest request)
{
return new ResourceSchemeHandler();
}

public static string SchemeName { get { return "resource"; } }
}
2 changes: 0 additions & 2 deletions Nacollector/ClassDiagram1.cd

This file was deleted.

6 changes: 6 additions & 0 deletions Nacollector/JsActions/AppAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,12 @@ public void logFileClear()
Logging.Clear();
}

// 调用浏览器下载文件
public void downloadUrl(string url)
{
crBrowser.DownloadUrl(url);
}

// 升级操作
public void appUpdateAction(string srcUrl, string updateType)
{
Expand Down
9 changes: 6 additions & 3 deletions Nacollector/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,15 @@ public MainForm()
private void InitBrowser()
{
// 初始化内置浏览器
string htmlPath = Utils.GetHtmlResPath("app.html");
#if !DEBUG
string htmlPath = Utils.GetHtmlResPath("index.html");
if (string.IsNullOrEmpty(htmlPath))
{
Application.Exit(); // 退出程序
}

#else
string htmlPath = "http://127.0.0.1:8080";
#endif
crBrowser = new CrBrowser(this, htmlPath);

// Need Update: https://github.com/cefsharp/CefSharp/issues/2246
Expand Down Expand Up @@ -125,7 +128,7 @@ private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
string dialogTxt = "确定退出 Nacollector?";

// 下载任务数统计
int downloadingTaskNum = Convert.ToInt32(crBrowser.EvaluateScript("downloads.countDownloadingTask();", 0, TimeSpan.FromSeconds(3)).GetAwaiter().GetResult());
int downloadingTaskNum = Convert.ToInt32(crBrowser.EvaluateScript("Downloads.countDownloadingTask();", 0, TimeSpan.FromSeconds(3)).GetAwaiter().GetResult());
if (downloadingTaskNum > 0)
dialogTxt = $"{downloadingTaskNum} 个下载任务仍在继续!确定结束下载并关闭程序?";

Expand Down
Loading

0 comments on commit 61c1abc

Please sign in to comment.