50760a2d5de6d3ebbf3b8c8d81a131a9c75a092a
[WebKit-https.git] / Source / WebInspectorUI / UserInterface / Views / ContextMenuUtilities.js
1 /*
2  * Copyright (C) 2016 Devin Rousso <webkit@devinrousso.com>. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 WI.appendContextMenuItemsForSourceCode = function(contextMenu, sourceCodeOrLocation)
27 {
28     console.assert(contextMenu instanceof WI.ContextMenu);
29     if (!(contextMenu instanceof WI.ContextMenu))
30         return;
31
32     let sourceCode = sourceCodeOrLocation;
33     let location = null;
34     if (sourceCodeOrLocation instanceof WI.SourceCodeLocation) {
35         sourceCode = sourceCodeOrLocation.sourceCode;
36         location = sourceCodeOrLocation;
37     }
38
39     console.assert(sourceCode instanceof WI.SourceCode);
40     if (!(sourceCode instanceof WI.SourceCode))
41         return;
42
43     contextMenu.appendSeparator();
44
45     WI.appendContextMenuItemsForURL(contextMenu, sourceCode.url, {sourceCode, location});
46
47     if (sourceCode instanceof WI.Resource) {
48         if (sourceCode.urlComponents.scheme !== "data") {
49             contextMenu.appendItem(WI.UIString("Copy as cURL"), () => {
50                 sourceCode.generateCURLCommand();
51             });
52         }
53     }
54
55     contextMenu.appendItem(WI.UIString("Save File"), () => {
56         sourceCode.requestContent().then(() => {
57             const forceSaveAs = true;
58             WI.saveDataToFile({
59                 url: sourceCode.url || "",
60                 content: sourceCode.content
61             }, forceSaveAs);
62         });
63     });
64
65     contextMenu.appendSeparator();
66
67     if (location && (sourceCode instanceof WI.Script || (sourceCode instanceof WI.Resource && sourceCode.type === WI.Resource.Type.Script))) {
68         let existingBreakpoint = WI.debuggerManager.breakpointForSourceCodeLocation(location);
69         if (existingBreakpoint) {
70             contextMenu.appendItem(WI.UIString("Delete Breakpoint"), () => {
71                 WI.debuggerManager.removeBreakpoint(existingBreakpoint);
72             });
73         } else {
74             contextMenu.appendItem(WI.UIString("Add Breakpoint"), () => {
75                 WI.debuggerManager.addBreakpoint(new WI.Breakpoint(location));
76             });
77         }
78
79         contextMenu.appendSeparator();
80     }
81 };
82
83 WI.appendContextMenuItemsForURL = function(contextMenu, url, options)
84 {
85     if (!url)
86         return;
87
88     let {sourceCode, location, frame} = options;
89     function showResourceWithOptions(options) {
90         if (location)
91             WI.showSourceCodeLocation(location, options);
92         else if (sourceCode)
93             WI.showSourceCode(sourceCode, options);
94         else
95             WI.openURL(url, frame, options);
96     }
97
98     contextMenu.appendItem(WI.UIString("Open in New Tab"), () => {
99         const frame = null;
100         WI.openURL(url, frame, {alwaysOpenExternally: true});
101     });
102
103     if (WI.frameResourceManager.resourceForURL(url)) {
104         if (WI.settings.experimentalEnableSourcesTab.value) {
105             if (!WI.isShowingSourcesTab()) {
106                 contextMenu.appendItem(WI.UIString("Reveal in Sources Tab"), () => {
107                     showResourceWithOptions({preferredTabType: WI.SourcesTabContentView.Type});
108                 });
109             }
110         } else if (!WI.isShowingResourcesTab()) {
111             contextMenu.appendItem(WI.UIString("Reveal in Resources Tab"), () => {
112                 showResourceWithOptions({ignoreNetworkTab: true, ignoreSearchTab: true});
113             });
114         }
115         if (!WI.isShowingNetworkTab()) {
116             contextMenu.appendItem(WI.UIString("Reveal in Network Tab"), () => {
117                 showResourceWithOptions({ignoreResourcesTab: true, ignoreDebuggerTab: true, ignoreSearchTab: true});
118             });
119         }
120     }
121
122     contextMenu.appendItem(WI.UIString("Copy Link Address"), () => {
123         InspectorFrontendHost.copyText(sourceCode.url);
124     });
125 };
126
127 WI.appendContextMenuItemsForDOMNode = function(contextMenu, domNode, options = {})
128 {
129     console.assert(contextMenu instanceof WI.ContextMenu);
130     if (!(contextMenu instanceof WI.ContextMenu))
131         return;
132
133     console.assert(domNode instanceof WI.DOMNode);
134     if (!(domNode instanceof WI.DOMNode))
135         return;
136
137     let copySubMenu = options.copySubMenu || contextMenu.appendSubMenuItem(WI.UIString("Copy"));
138
139     let isElement = domNode.nodeType() === Node.ELEMENT_NODE;
140     if (domNode.ownerDocument && isElement) {
141         copySubMenu.appendItem(WI.UIString("Selector Path"), () => {
142             let cssPath = WI.cssPath(domNode);
143             InspectorFrontendHost.copyText(cssPath);
144         });
145     }
146
147     if (domNode.ownerDocument && !domNode.isPseudoElement()) {
148         copySubMenu.appendItem(WI.UIString("XPath"), () => {
149             let xpath = WI.xpath(domNode);
150             InspectorFrontendHost.copyText(xpath);
151         });
152     }
153
154     contextMenu.appendSeparator();
155
156     if (domNode.isCustomElement()) {
157         contextMenu.appendItem(WI.UIString("Jump to Definition"), () => {
158             function didGetFunctionDetails(error, response) {
159                 if (error)
160                     return;
161
162                 let location = response.location;
163                 let sourceCode = WI.debuggerManager.scriptForIdentifier(location.scriptId, WI.mainTarget);
164                 if (!sourceCode)
165                     return;
166
167                 let sourceCodeLocation = sourceCode.createSourceCodeLocation(location.lineNumber, location.columnNumber || 0);
168                 WI.showSourceCodeLocation(sourceCodeLocation, {
169                     ignoreNetworkTab: true,
170                     ignoreSearchTab: true,
171                 });
172             }
173
174             function didGetProperty(error, result, wasThrown) {
175                 if (error || result.type !== "function")
176                     return;
177
178                 DebuggerAgent.getFunctionDetails(result.objectId, didGetFunctionDetails);
179                 result.release();
180             }
181
182             WI.RemoteObject.resolveNode(domNode).then((remoteObject) => {
183                 remoteObject.getProperty("constructor", didGetProperty);
184                 remoteObject.release();
185             });
186         });
187
188         contextMenu.appendSeparator();
189     }
190
191     if (WI.domDebuggerManager.supported && isElement && !domNode.isPseudoElement() && domNode.ownerDocument) {
192         contextMenu.appendSeparator();
193
194         const allowEditing = false;
195         WI.DOMBreakpointTreeController.appendBreakpointContextMenuItems(contextMenu, domNode, allowEditing);
196     }
197
198     contextMenu.appendSeparator();
199
200     if (!options.excludeLogElement && !domNode.isInUserAgentShadowTree() && !domNode.isPseudoElement()) {
201         let label = isElement ? WI.UIString("Log Element") : WI.UIString("Log Node");
202         contextMenu.appendItem(label, () => {
203             WI.RemoteObject.resolveNode(domNode, WI.RuntimeManager.ConsoleObjectGroup).then((remoteObject) => {
204                 let text = isElement ? WI.UIString("Selected Element") : WI.UIString("Selected Node");
205                 const addSpecialUserLogClass = true;
206                 WI.consoleLogViewController.appendImmediateExecutionWithResult(text, remoteObject, addSpecialUserLogClass);
207             });
208         });
209     }
210
211     if (!options.excludeRevealElement && window.DOMAgent && domNode.ownerDocument) {
212         contextMenu.appendItem(WI.UIString("Reveal in DOM Tree"), () => {
213             WI.domTreeManager.inspectElement(domNode.id);
214         });
215     }
216
217     if (!options.excludeRevealLayer && window.LayerTreeAgent && domNode.parentNode) {
218         contextMenu.appendItem(WI.UIString("Reveal in Layers Tab"), () => {
219             WI.showLayersTab({nodeToSelect: domNode});
220         });
221     }
222
223     if (window.PageAgent) {
224         contextMenu.appendItem(WI.UIString("Capture Screenshot"), () => {
225             PageAgent.snapshotNode(domNode.id, (error, dataURL) => {
226                 if (error) {
227                     const target = WI.mainTarget;
228                     const source = WI.ConsoleMessage.MessageSource.Other;
229                     const level = WI.ConsoleMessage.MessageLevel.Error;
230                     let consoleMessage = new WI.ConsoleMessage(target, source, level, error);
231                     consoleMessage.shouldRevealConsole = true;
232
233                     WI.consoleLogViewController.appendConsoleMessage(consoleMessage);
234                     return;
235                 }
236
237                 let date = new Date;
238                 let values = [
239                     date.getFullYear(),
240                     Number.zeroPad(date.getMonth() + 1, 2),
241                     Number.zeroPad(date.getDate(), 2),
242                     Number.zeroPad(date.getHours(), 2),
243                     Number.zeroPad(date.getMinutes(), 2),
244                     Number.zeroPad(date.getSeconds(), 2),
245                 ];
246                 let filename = WI.UIString("Screen Shot %s-%s-%s at %s.%s.%s").format(...values);
247                 WI.saveDataToFile({
248                     url: encodeURI(`web-inspector:///${filename}.png`),
249                     content: parseDataURL(dataURL).data,
250                     base64Encoded: true,
251                 });
252             });
253         });
254     }
255
256     if (isElement) {
257         contextMenu.appendItem(WI.UIString("Scroll Into View"), () => {
258             domNode.scrollIntoView();
259         });
260     }
261
262     contextMenu.appendSeparator();
263 };