1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 | import * as Common from '../../core/common/common.js';
|
36 | import * as Platform from '../../core/platform/platform.js';
|
37 | import * as SDK from '../../core/sdk/sdk.js';
|
38 | import type * as Protocol from '../../generated/protocol.js';
|
39 | import * as Workspace from '../workspace/workspace.js';
|
40 |
|
41 | export function resourceForURL(url: string): SDK.Resource.Resource|null {
|
42 | for (const resourceTreeModel of SDK.TargetManager.TargetManager.instance().models(
|
43 | SDK.ResourceTreeModel.ResourceTreeModel)) {
|
44 | const resource = resourceTreeModel.resourceForURL(url);
|
45 | if (resource) {
|
46 | return resource;
|
47 | }
|
48 | }
|
49 | return null;
|
50 | }
|
51 |
|
52 | export function displayNameForURL(url: string): string {
|
53 | if (!url) {
|
54 | return '';
|
55 | }
|
56 |
|
57 | const resource = resourceForURL(url);
|
58 | if (resource) {
|
59 | return resource.displayName;
|
60 | }
|
61 |
|
62 | const uiSourceCode = Workspace.Workspace.WorkspaceImpl.instance().uiSourceCodeForURL(url);
|
63 | if (uiSourceCode) {
|
64 | return uiSourceCode.displayName();
|
65 | }
|
66 |
|
67 | const mainTarget = SDK.TargetManager.TargetManager.instance().mainTarget();
|
68 | const inspectedURL = mainTarget && mainTarget.inspectedURL();
|
69 | if (!inspectedURL) {
|
70 | return Platform.StringUtilities.trimURL(url, '');
|
71 | }
|
72 |
|
73 | const parsedURL = Common.ParsedURL.ParsedURL.fromString(inspectedURL);
|
74 | if (!parsedURL) {
|
75 | return url;
|
76 | }
|
77 |
|
78 | const lastPathComponent = parsedURL.lastPathComponent;
|
79 | const index = inspectedURL.indexOf(lastPathComponent);
|
80 | if (index !== -1 && index + lastPathComponent.length === inspectedURL.length) {
|
81 | const baseURL = inspectedURL.substring(0, index);
|
82 | if (url.startsWith(baseURL)) {
|
83 | return url.substring(index);
|
84 | }
|
85 | }
|
86 |
|
87 | const displayName = Platform.StringUtilities.trimURL(url, parsedURL.host);
|
88 | return displayName === '/' ? parsedURL.host + '/' : displayName;
|
89 | }
|
90 |
|
91 | export function metadataForURL(target: SDK.Target.Target, frameId: Protocol.Page.FrameId, url: string):
|
92 | Workspace.UISourceCode.UISourceCodeMetadata|null {
|
93 | const resourceTreeModel = target.model(SDK.ResourceTreeModel.ResourceTreeModel);
|
94 | if (!resourceTreeModel) {
|
95 | return null;
|
96 | }
|
97 | const frame = resourceTreeModel.frameForId(frameId);
|
98 | if (!frame) {
|
99 | return null;
|
100 | }
|
101 | return resourceMetadata(frame.resourceForURL(url));
|
102 | }
|
103 |
|
104 | export function resourceMetadata(resource: SDK.Resource.Resource|null): Workspace.UISourceCode.UISourceCodeMetadata|
|
105 | null {
|
106 | if (!resource || (typeof resource.contentSize() !== 'number' && !resource.lastModified())) {
|
107 | return null;
|
108 | }
|
109 | return new Workspace.UISourceCode.UISourceCodeMetadata(resource.lastModified(), resource.contentSize());
|
110 | }
|