Skip to content
This repository has been archived by the owner on Jan 6, 2025. It is now read-only.

Commit

Permalink
Modify SchemeName
Browse files Browse the repository at this point in the history
  • Loading branch information
qwqcode committed Apr 21, 2019
1 parent 8eb81d1 commit b2909e1
Show file tree
Hide file tree
Showing 7 changed files with 219 additions and 222 deletions.
3 changes: 0 additions & 3 deletions Nacollector/App.config
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.2" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
Expand Down
2 changes: 1 addition & 1 deletion Nacollector/Browser/CrBrowser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ private void Browser_onFrameLoadEnd(object _sender, FrameLoadEndEventArgs e)
{
ChromiumWebBrowser sender = (ChromiumWebBrowser)_sender;
string url = e.Frame.Url;
if (url.IndexOf("http://127.0.0.1") == 0 || url.IndexOf("file://") == 0)
if (url.IndexOf("http://127.0.0.1") == 0 || url.IndexOf("nacollector://") == 0)
{
((DragDropHandler)browser.DragHandler).Enable = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ public IResourceHandler Create(IBrowser browser, IFrame frame, string schemeName
return new ResourceSchemeHandler();
}

public static string SchemeName { get { return "resource"; } }
public static string SchemeName { get { return "nacollector"; } }
}
217 changes: 1 addition & 216 deletions Nacollector/MainForm.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,9 @@
using CefSharp;
using CefSharp.WinForms;
using Nacollector.Browser;
using Nacollector.Browser.Handler;
using Nacollector.JsActions;
using Nacollector.Ui;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Security.Policy;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using NacollectorUtils;
using NacollectorUtils.Settings;

namespace Nacollector
{
Expand All @@ -47,7 +27,7 @@ private void InitBrowser()
// 初始化内置浏览器
#warning 记得修改
#if DEBUG
string htmlPath = "resource://html_res/index.html";
string htmlPath = "nacollector://html_res/index.html";
#else
string htmlPath = "http://127.0.0.1:8080";
#endif
Expand Down Expand Up @@ -99,200 +79,5 @@ private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
e.Cancel = true;
}
}

///
/// 任务执行
///

public AppDomain _spiderRawDomain = null;
public SpiderDomain _spiderDomain = null;

public SpiderDomain GetLoadSpiderDomain()
{
if (this._spiderDomain != null)
{
return this._spiderDomain;
}

// 创建新的 AppDomain
AppDomainSetup domaininfo = new AppDomainSetup
{
ApplicationBase = Environment.CurrentDirectory
};
Evidence adevidence = AppDomain.CurrentDomain.Evidence;
AppDomain rawDomain = AppDomain.CreateDomain("NacollectorSpiders", adevidence, domaininfo);

// 动态加载 dll
Type type = typeof(SpiderDomain);
var spiderDomain = (SpiderDomain)rawDomain.CreateInstanceAndUnwrap(type.Assembly.FullName, type.FullName);

spiderDomain.LoadAssembly(Path.Combine(Application.StartupPath, "NacollectorSpiders.dll"));

this._spiderRawDomain = rawDomain;
this._spiderDomain = spiderDomain;
return spiderDomain;
}

public void UnloadSpiderDomain()
{
if (_spiderRawDomain == null)
{
return;
}

// 卸载 dll
AppDomain.Unload(_spiderRawDomain);
_spiderRawDomain = null;
_spiderDomain = null;
}

/// <summary>
/// 任务线程
/// </summary>
public Dictionary<string, Thread> taskThreads = new Dictionary<string, Thread>();

public void AbortTask(string taskId)
{
if (!taskThreads.ContainsKey(taskId))
return;

// 主线程委托执行,防止 Abort() 后面的代码无效
this.BeginInvoke((MethodInvoker)delegate
{
if (taskThreads[taskId].IsAlive)
{
taskThreads[taskId].Abort();
}

taskThreads.Remove(taskId);
if (taskThreads.Count <= 0)
{
UnloadSpiderDomain();
}
});

return;
}

public void NewTaskThread(SpiderSettings settings)
{
// 创建任务执行线程
var thread = new Thread(new ParameterizedThreadStart(StartTask))
{
IsBackground = true
};
thread.Start(settings);

// 加入 Threads Dictionary
taskThreads.Add(settings.TaskId, thread);
}

private void ShowCreateTaskError(string taskId, string msg, Exception ex)
{
string errorText = "任务新建失败, " + msg;
Logging.Error($"{errorText} [{ex.Data.Keys + ": " + ex.Message}]");
MessageBox.Show(errorText, "Nacollector 错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
AbortTask(taskId);
}

/// <summary>
/// 开始执行任务
/// </summary>
/// <param name="obj"></param>
public void StartTask(object obj)
{
var settings = (SpiderSettings)obj;

SpiderDomain spiderDomain = null;
try
{
spiderDomain = GetLoadSpiderDomain();
}
catch (Exception ex)
{
ShowCreateTaskError((string)settings.TaskId, "NacollectorSpiders.dll 调用失败", ex);
#if DEBUG
Debugger.Break();
#endif
return;
}

// 调用目标函数
try
{
// Funcs
settings.BrowserJsRunFunc = new Action<string>((string str) => {
this.GetCrBrowser().RunJS(str);
});
settings.CrBrowserCookieGetter = new Func<CookieGetterSettings, string>((CookieGetterSettings cgSettings) =>
{
GetCrCookieGetter().CreateNew(cgSettings.StartUrl, cgSettings.EndUrlReg, cgSettings.Caption);

if (cgSettings.InputAutoCompleteConfig != null)
{
GetCrCookieGetter().UseInputAutoComplete(
(string)cgSettings.InputAutoCompleteConfig["pageUrlReg"],
(List<string>)cgSettings.InputAutoCompleteConfig["inputElemCssSelectors"]
);
}

GetCrCookieGetter().BeginWork();

return GetCrCookieGetter().GetCookieStr();
});

spiderDomain.Invoke("NacollectorSpiders.PokerDealer", "NewTask", settings);
}
catch (ThreadAbortException)
{
// 进程正在被中止
// 不进行操作
}
catch (Exception ex)
{
ShowCreateTaskError((string)settings.TaskId, "无法调用 NacollectorSpiders.PokerDealer.NewTask", ex);
#if DEBUG
Debugger.Break();
#endif
return;
}

AbortTask((string)settings.TaskId);
}

/// <summary>
/// 执行爬虫任务,新的 AppDomain 中
/// </summary>
public class SpiderDomain : MarshalByRefObject
{
Assembly assembly = null;

public void LoadAssembly(string assemblyPath)
{
try
{
assembly = Assembly.LoadFile(assemblyPath);
}
catch (Exception ex)
{
throw new InvalidOperationException(ex.Message);
}
}

public bool Invoke(string fullClassName, string methodName, params object[] args)
{
if (assembly == null)
return false;
Type tp = assembly.GetType(fullClassName);
if (tp == null)
return false;
MethodInfo method = tp.GetMethod(methodName);
if (method == null)
return false;
object obj = Activator.CreateInstance(tp);
method.Invoke(obj, args);
return true;
}
}
}
}
3 changes: 3 additions & 0 deletions Nacollector/Nacollector.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@
<Compile Include="Browser\Handler\MenuHandler.cs" />
<Compile Include="Browser\Handler\ResourceSchemeHandler .cs" />
<Compile Include="Browser\Handler\ResourceSchemeHandlerFactory.cs" />
<Compile Include="Task.cs">
<SubType>Form</SubType>
</Compile>
<Compile Include="Ui\Native.cs" />
<Compile Include="Ui\Shadow.cs">
<SubType>Form</SubType>
Expand Down
2 changes: 1 addition & 1 deletion Nacollector/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ private static void InitCef()
{
Cef.EnableHighDPISupport();
var settings = new CefSettings();
settings.CefCommandLineArgs.Add("--allow-file-access-from-files", "");
//settings.CefCommandLineArgs.Add("--allow-file-access-from-files", "");
settings.CefCommandLineArgs.Add("--disable-web-security", "");
settings.Locale = "zh-CN";
settings.AcceptLanguageList = "zh-CN,zh";
Expand Down
Loading

0 comments on commit b2909e1

Please sign in to comment.