From 7d46d3ec9d5f30138777d1bd12890ebe28ba6c31 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Thu, 26 Jan 2023 09:54:26 +0100 Subject: [PATCH] feat: emit `devtools-open-url` event for DevTools link selection (#36774) * feat: emit event for DevTools link selection * chore: devtools-open-in-new-tab -> devtools-open-url --- docs/api/web-contents.md | 8 ++++++++ docs/api/webview-tag.md | 8 ++++++++ lib/browser/web-view-events.ts | 1 + shell/browser/api/electron_api_web_contents.cc | 4 ++++ shell/browser/api/electron_api_web_contents.h | 1 + shell/browser/ui/inspectable_web_contents.cc | 5 ++++- shell/browser/ui/inspectable_web_contents_delegate.h | 1 + 7 files changed, 27 insertions(+), 1 deletion(-) diff --git a/docs/api/web-contents.md b/docs/api/web-contents.md index b0f7c06b52d3..54f69ecb94aa 100644 --- a/docs/api/web-contents.md +++ b/docs/api/web-contents.md @@ -492,6 +492,14 @@ The `focus` and `blur` events of `WebContents` should only be used to detect focus change between different `WebContents` and `BrowserView` in the same window. +#### Event: 'devtools-open-url' + +Returns: + +* `url` string - URL of the link that was clicked or selected. + +Emitted when a link is clicked in DevTools or 'Open in new tab' is selected for a link in its context menu. + #### Event: 'devtools-opened' Emitted when DevTools is opened. diff --git a/docs/api/webview-tag.md b/docs/api/webview-tag.md index c6f924af42ec..1acb17b0383a 100644 --- a/docs/api/webview-tag.md +++ b/docs/api/webview-tag.md @@ -981,6 +981,14 @@ Returns: Emitted when mouse moves over a link or the keyboard moves the focus to a link. +### Event: 'devtools-open-url' + +Returns: + +* `url` string - URL of the link that was clicked or selected. + +Emitted when a link is clicked in DevTools or 'Open in new tab' is selected for a link in its context menu. + ### Event: 'devtools-opened' Emitted when DevTools is opened. diff --git a/lib/browser/web-view-events.ts b/lib/browser/web-view-events.ts index d571ecedde53..e8086336f867 100644 --- a/lib/browser/web-view-events.ts +++ b/lib/browser/web-view-events.ts @@ -9,6 +9,7 @@ export const webViewEvents: Record = { 'dom-ready': [], 'console-message': ['level', 'message', 'line', 'sourceId'], 'context-menu': ['params'], + 'devtools-open-url': ['url'], 'devtools-opened': [], 'devtools-closed': [], 'devtools-focused': [], diff --git a/shell/browser/api/electron_api_web_contents.cc b/shell/browser/api/electron_api_web_contents.cc index a0f234aa3f89..38357a917003 100644 --- a/shell/browser/api/electron_api_web_contents.cc +++ b/shell/browser/api/electron_api_web_contents.cc @@ -3825,6 +3825,10 @@ void WebContents::DevToolsStopIndexing(int request_id) { devtools_indexing_jobs_.erase(it); } +void WebContents::DevToolsOpenInNewTab(const std::string& url) { + Emit("devtools-open-url", url); +} + void WebContents::DevToolsSearchInPath(int request_id, const std::string& file_system_path, const std::string& query) { diff --git a/shell/browser/api/electron_api_web_contents.h b/shell/browser/api/electron_api_web_contents.h index 835610c50e6a..6231c0b2e41f 100644 --- a/shell/browser/api/electron_api_web_contents.h +++ b/shell/browser/api/electron_api_web_contents.h @@ -704,6 +704,7 @@ class WebContents : public ExclusiveAccessContext, void DevToolsIndexPath(int request_id, const std::string& file_system_path, const std::string& excluded_folders_message) override; + void DevToolsOpenInNewTab(const std::string& url) override; void DevToolsStopIndexing(int request_id) override; void DevToolsSearchInPath(int request_id, const std::string& file_system_path, diff --git a/shell/browser/ui/inspectable_web_contents.cc b/shell/browser/ui/inspectable_web_contents.cc index 015f70eaf772..035bf7fe74ff 100644 --- a/shell/browser/ui/inspectable_web_contents.cc +++ b/shell/browser/ui/inspectable_web_contents.cc @@ -723,7 +723,10 @@ void InspectableWebContents::SetIsDocked(DispatchCallback callback, std::move(callback).Run(nullptr); } -void InspectableWebContents::OpenInNewTab(const std::string& url) {} +void InspectableWebContents::OpenInNewTab(const std::string& url) { + if (delegate_) + delegate_->DevToolsOpenInNewTab(url); +} void InspectableWebContents::ShowItemInFolder( const std::string& file_system_path) { diff --git a/shell/browser/ui/inspectable_web_contents_delegate.h b/shell/browser/ui/inspectable_web_contents_delegate.h index 62abef7485e4..09ab5c793041 100644 --- a/shell/browser/ui/inspectable_web_contents_delegate.h +++ b/shell/browser/ui/inspectable_web_contents_delegate.h @@ -31,6 +31,7 @@ class InspectableWebContentsDelegate { virtual void DevToolsIndexPath(int request_id, const std::string& file_system_path, const std::string& excluded_folders) {} + virtual void DevToolsOpenInNewTab(const std::string& url) {} virtual void DevToolsStopIndexing(int request_id) {} virtual void DevToolsSearchInPath(int request_id, const std::string& file_system_path,