Browse Source

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
Shelley Vohr 2 years ago
parent
commit
7d46d3ec9d

+ 8 - 0
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.

+ 8 - 0
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.

+ 1 - 0
lib/browser/web-view-events.ts

@@ -9,6 +9,7 @@ export const webViewEvents: Record<string, readonly string[]> = {
   'dom-ready': [],
   'console-message': ['level', 'message', 'line', 'sourceId'],
   'context-menu': ['params'],
+  'devtools-open-url': ['url'],
   'devtools-opened': [],
   'devtools-closed': [],
   'devtools-focused': [],

+ 4 - 0
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) {

+ 1 - 0
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,

+ 4 - 1
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) {

+ 1 - 0
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,