Merge pull request #163 from l1nxy/add-support-for-vivaldi
Add browser support for Vivaldi.
- 作者
- Berumotto
- 时间
- 2023/04/19 17:40
- 提交
- 5d84313
这是一个合并提交,下面是和第一个上一提交相比的改动。
Add browser support for Vivaldi.
这是一个合并提交,下面是和第一个上一提交相比的改动。
| @@ -208,6 +208,7 @@ | ||
| 208 | 208 | <Compile Include="Servicers\Interfaces\ISleepdiscover.cs" /> |
| 209 | 209 | <Compile Include="Servicers\Interfaces\IWebData.cs" /> |
| 210 | 210 | <Compile Include="Servicers\Interfaces\IWebFilter.cs" /> |
| 211 | 新增: <Compile Include="Servicers\Instances\VivaldiBrowserWatch.cs" /> | |
| 211 | 212 | </ItemGroup> |
| 212 | 213 | <ItemGroup> |
| 213 | 214 | <None Include="App.config" /> |
| @@ -22,6 +22,12 @@ namespace Core.Enums | ||
| 22 | 22 | /// <summary> |
| 23 | 23 | /// Microsoft Edge |
| 24 | 24 | /// </summary> |
| 25 | 删除: MSEdge | |
| 25 | 新增: MSEdge, | |
| 26 | 新增: /// <summary> | |
| 27 | 新增: /// Vivaldi | |
| 28 | 新增: /// </summary> | |
| 29 | 新增: Vivaldi | |
| 30 | 新增: | |
| 31 | 新增: | |
| 26 | 32 | } |
| 27 | 33 | } |
| @@ -109,6 +109,9 @@ namespace Core.Servicers.Instances | ||
| 109 | 109 | case "msedge": |
| 110 | 110 | StartWatch(BrowserType.MSEdge, args.Handle); |
| 111 | 111 | break; |
| 112 | 新增: case "vivaldi": | |
| 113 | 新增: StartWatch(BrowserType.Vivaldi, args.Handle); | |
| 114 | 新增: break; | |
| 112 | 115 | default: |
| 113 | 116 | StopWatch(); |
| 114 | 117 | break; |
| @@ -124,6 +127,9 @@ namespace Core.Servicers.Instances | ||
| 124 | 127 | { |
| 125 | 128 | case BrowserType.Chrome: |
| 126 | 129 | _browserWatcher = new ChromeBrowserWatch(); |
| 130 | 新增: break; | |
| 131 | 新增: case BrowserType.Vivaldi: | |
| 132 | 新增: _browserWatcher = new VivaldiBrowserWatch(); | |
| 127 | 133 | break; |
| 128 | 134 | case BrowserType.MSEdge: |
| 129 | 135 | _browserWatcher = new MSEdgeBrowserWatch(); |
| @@ -0,0 +1,220 @@ | ||
| 1 | 新增:using Core.Enums; | |
| 2 | 新增:using Core.Event; | |
| 3 | 新增:using Core.Librarys; | |
| 4 | 新增:using Core.Librarys.Browser.Favicon; | |
| 5 | 新增:using Core.Models.WebPage; | |
| 6 | 新增:using Core.Servicers.Interfaces; | |
| 7 | 新增:using NPOI.SS.Formula.Functions; | |
| 8 | 新增:using System; | |
| 9 | 新增:using System.Collections.Generic; | |
| 10 | 新增:using System.Data.SQLite; | |
| 11 | 新增:using System.Diagnostics; | |
| 12 | 新增:using System.IO; | |
| 13 | 新增:using System.Linq; | |
| 14 | 新增:using System.Security.Cryptography; | |
| 15 | 新增:using System.Text; | |
| 16 | 新增:using System.Threading.Tasks; | |
| 17 | 新增:using System.Windows.Automation; | |
| 18 | 新增:using System.Windows.Interop; | |
| 19 | 新增:using UIAutomationClient; | |
| 20 | 新增: | |
| 21 | 新增:namespace Core.Servicers.Instances | |
| 22 | 新增:{ | |
| 23 | 新增: public class VivaldiBrowserWatch : IBrowserWatcher | |
| 24 | 新增: { | |
| 25 | 新增: public event BrowserObserverEventHandler OnSiteChanged; | |
| 26 | 新增: private string url, title; | |
| 27 | 新增: CUIAutomation automation; | |
| 28 | 新增: // 根元素(监听站点切换使用) | |
| 29 | 新增: IUIAutomationElement rootElement; | |
| 30 | 新增: // 主元素(寻找URL使用) | |
| 31 | 新增: IUIAutomationElement mainElement; | |
| 32 | 新增: // 站点切换事件 | |
| 33 | 新增: AutomationPropertyChangedEventHandler siteChangedEventHandler; | |
| 34 | 新增: | |
| 35 | 新增: public VivaldiBrowserWatch() | |
| 36 | 新增: { | |
| 37 | 新增: automation = new CUIAutomation(); | |
| 38 | 新增: siteChangedEventHandler = new AutomationPropertyChangedEventHandler(OnNameChanged); | |
| 39 | 新增: } | |
| 40 | 新增: | |
| 41 | 新增: public void StartWatch(IntPtr handle) | |
| 42 | 新增: { | |
| 43 | 新增: try | |
| 44 | 新增: { | |
| 45 | 新增: if (handle == IntPtr.Zero) | |
| 46 | 新增: { | |
| 47 | 新增: throw new Exception("未传入浏览器句柄"); | |
| 48 | 新增: } | |
| 49 | 新增: | |
| 50 | 新增: Win32API.GetWindowThreadProcessId(handle, out int processID); | |
| 51 | 新增: Process process = Process.GetProcessById(processID); | |
| 52 | 新增: if (process == null) | |
| 53 | 新增: { | |
| 54 | 新增: return; | |
| 55 | 新增: } | |
| 56 | 新增: | |
| 57 | 新增: Debug.WriteLine("启动浏览器监听,目标:" + process.ProcessName); | |
| 58 | 新增: // 搜索根元素 | |
| 59 | 新增: rootElement = automation.ElementFromHandle(handle); | |
| 60 | 新增: if (rootElement == null) | |
| 61 | 新增: { | |
| 62 | 新增: throw new Exception("找不到根元素!"); | |
| 63 | 新增: } | |
| 64 | 新增: // 找到窗口 | |
| 65 | 新增: var condtionPanel = automation.CreatePropertyCondition(30003, 50033); | |
| 66 | 新增: var condtionName = automation.CreatePropertyCondition(30005, "Chrome_WidgetWin_1"); | |
| 67 | 新增: var condtionFindWindow = automation.CreateAndCondition(condtionPanel, condtionName); | |
| 68 | 新增: var window = rootElement.FindFirst(TreeScope.TreeScope_Children, condtionFindWindow); | |
| 69 | 新增: if (window != null) | |
| 70 | 新增: { | |
| 71 | 新增: // 第二个窗口 | |
| 72 | 新增: mainElement = window.FindFirst(TreeScope.TreeScope_Children, automation.CreateTrueCondition()); | |
| 73 | 新增: } | |
| 74 | 新增: else | |
| 75 | 新增: { | |
| 76 | 新增: mainElement = rootElement; | |
| 77 | 新增: Debug.WriteLine("没有找到主窗口,使用根元素作为主元素"); | |
| 78 | 新增: } | |
| 79 | 新增: | |
| 80 | 新增: | |
| 81 | 新增: if (mainElement == null) | |
| 82 | 新增: { | |
| 83 | 新增: throw new Exception("找不到主元素!"); | |
| 84 | 新增: } | |
| 85 | 新增: | |
| 86 | 新增: // 注册事件 | |
| 87 | 新增: IUIAutomationCacheRequest cacheRequest = automation.CreateCacheRequest(); | |
| 88 | 新增: cacheRequest.AddProperty(30005); | |
| 89 | 新增: int[] props = { 30005 }; | |
| 90 | 新增: | |
| 91 | 新增: automation.AddPropertyChangedEventHandler(rootElement, TreeScope.TreeScope_Element, cacheRequest, siteChangedEventHandler, props); | |
| 92 | 新增: Debug.WriteLine("启动浏览器监听成功"); | |
| 93 | 新增: | |
| 94 | 新增: Handle(process.MainWindowTitle); | |
| 95 | 新增: } | |
| 96 | 新增: catch (Exception e) | |
| 97 | 新增: { | |
| 98 | 新增: Logger.Error(e.ToString()); | |
| 99 | 新增: } | |
| 100 | 新增: } | |
| 101 | 新增: | |
| 102 | 新增: private void OnNameChanged(IUIAutomationElement s, int p, object v) | |
| 103 | 新增: { | |
| 104 | 新增: Handle(v.ToString()); | |
| 105 | 新增: } | |
| 106 | 新增: | |
| 107 | 新增: #region AutomationPropertyChangedEventHandler | |
| 108 | 新增: private class AutomationPropertyChangedEventHandler : IUIAutomationPropertyChangedEventHandler | |
| 109 | 新增: { | |
| 110 | 新增: public AutomationPropertyChangedEventHandler(Action<IUIAutomationElement, int, object> action) | |
| 111 | 新增: { | |
| 112 | 新增: if (action == null) | |
| 113 | 新增: throw new ArgumentNullException(nameof(action)); | |
| 114 | 新增: | |
| 115 | 新增: Action = action; | |
| 116 | 新增: } | |
| 117 | 新增: | |
| 118 | 新增: public Action<IUIAutomationElement, int, object> Action { get; } | |
| 119 | 新增: public void HandlePropertyChangedEvent(IUIAutomationElement sender, int propertyId, object newValue) => Action(sender, propertyId, newValue); | |
| 120 | 新增: } | |
| 121 | 新增: #endregion | |
| 122 | 新增: | |
| 123 | 新增: public void StopWatch() | |
| 124 | 新增: { | |
| 125 | 新增: try | |
| 126 | 新增: { | |
| 127 | 新增: Debug.WriteLine("已停止浏览器监听"); | |
| 128 | 新增: automation.RemovePropertyChangedEventHandler(rootElement, siteChangedEventHandler); | |
| 129 | 新增: } | |
| 130 | 新增: catch (Exception e) | |
| 131 | 新增: { | |
| 132 | 新增: Logger.Error(e.ToString()); | |
| 133 | 新增: } | |
| 134 | 新增: } | |
| 135 | 新增: | |
| 136 | 新增: private string FormatTitle(string title) | |
| 137 | 新增: { | |
| 138 | 新增: if (string.IsNullOrEmpty(title)) | |
| 139 | 新增: { | |
| 140 | 新增: return title; | |
| 141 | 新增: } | |
| 142 | 新增: int length = title.LastIndexOf('-') - 1; | |
| 143 | 新增: if (length < 1) | |
| 144 | 新增: { | |
| 145 | 新增: length = 1; | |
| 146 | 新增: } | |
| 147 | 新增: | |
| 148 | 新增: return title.Substring(0, length); | |
| 149 | 新增: } | |
| 150 | 新增: | |
| 151 | 新增: private async void Handle(string title) | |
| 152 | 新增: { | |
| 153 | 新增: title = FormatTitle(title); | |
| 154 | 新增: //Debug.WriteLine($"切换标题 {title}"); | |
| 155 | 新增: Site site = null; | |
| 156 | 新增: if (!string.IsNullOrEmpty(title)) | |
| 157 | 新增: { | |
| 158 | 新增: site = SiteCache.Get(title); | |
| 159 | 新增: } | |
| 160 | 新增: | |
| 161 | 新增: if (site == null) | |
| 162 | 新增: { | |
| 163 | 新增: string url = await GetURLSync(); | |
| 164 | 新增: site = new Site() | |
| 165 | 新增: { | |
| 166 | 新增: Url = url, | |
| 167 | 新增: Title = title | |
| 168 | 新增: }; | |
| 169 | 新增: SiteCache.Set(title, url); | |
| 170 | 新增: } | |
| 171 | 新增: | |
| 172 | 新增: ResponseEvent(site.Url, site.Title); | |
| 173 | 新增: } | |
| 174 | 新增: | |
| 175 | 新增: #region 获取浏览器当前URL | |
| 176 | 新增: /// <summary> | |
| 177 | 新增: /// 获取浏览器当前URL | |
| 178 | 新增: /// </summary> | |
| 179 | 新增: /// <returns></returns> | |
| 180 | 新增: private async Task<string> GetURLSync() | |
| 181 | 新增: { | |
| 182 | 新增: return await Task.Run(() => | |
| 183 | 新增: { | |
| 184 | 新增: string result = string.Empty; | |
| 185 | 新增: try | |
| 186 | 新增: { | |
| 187 | 新增: var condtionEdit = automation.CreatePropertyCondition(30003, 50004); | |
| 188 | 新增: var condtionName = automation.CreatePropertyCondition(30011, "urlFieldInput"); | |
| 189 | 新增: var condtion = automation.CreateAndCondition(condtionEdit, condtionAccessKey); | |
| 190 | 新增: var addressElement = rootElement.FindFirst(TreeScope.TreeScope_Subtree, condtionName); | |
| 191 | 新增: if (addressElement != null) | |
| 192 | 新增: { | |
| 193 | 新增: result = ((IUIAutomationValuePattern)addressElement.GetCurrentPattern(10002)).CurrentValue; | |
| 194 | 新增: } | |
| 195 | 新增: } | |
| 196 | 新增: catch (Exception e) | |
| 197 | 新增: { | |
| 198 | 新增: Logger.Error(e.ToString()); | |
| 199 | 新增: } | |
| 200 | 新增: return result; | |
| 201 | 新增: }); | |
| 202 | 新增: | |
| 203 | 新增: } | |
| 204 | 新增: #endregion | |
| 205 | 新增: | |
| 206 | 新增: private void ResponseEvent(string url, string title) | |
| 207 | 新增: { | |
| 208 | 新增: if (url != this.url) | |
| 209 | 新增: { | |
| 210 | 新增: this.url = url; | |
| 211 | 新增: this.title = title; | |
| 212 | 新增: OnSiteChanged?.Invoke(BrowserType.Chrome, new Models.WebPage.Site() | |
| 213 | 新增: { | |
| 214 | 新增: Url = url, | |
| 215 | 新增: Title = title | |
| 216 | 新增: }); | |
| 217 | 新增: } | |
| 218 | 新增: } | |
| 219 | 新增: } | |
| 220 | 新增:} | |