ResourceUtils.js 2.2 KB

123456789101112131415161718
  1. export function resourceForURL(url){for(const resourceTreeModel of SDK.targetManager.models(SDK.ResourceTreeModel)){const resource=resourceTreeModel.resourceForURL(url);if(resource){return resource;}}
  2. return null;}
  3. export function displayNameForURL(url){if(!url){return'';}
  4. const resource=Bindings.resourceForURL(url);if(resource){return resource.displayName;}
  5. const uiSourceCode=Workspace.workspace.uiSourceCodeForURL(url);if(uiSourceCode){return uiSourceCode.displayName();}
  6. const mainTarget=SDK.targetManager.mainTarget();const inspectedURL=mainTarget&&mainTarget.inspectedURL();if(!inspectedURL){return url.trimURL('');}
  7. const parsedURL=inspectedURL.asParsedURL();const lastPathComponent=parsedURL?parsedURL.lastPathComponent:parsedURL;const index=inspectedURL.indexOf(lastPathComponent);if(index!==-1&&index+lastPathComponent.length===inspectedURL.length){const baseURL=inspectedURL.substring(0,index);if(url.startsWith(baseURL)){return url.substring(index);}}
  8. if(!parsedURL){return url;}
  9. const displayName=url.trimURL(parsedURL.host);return displayName==='/'?parsedURL.host+'/':displayName;}
  10. export function metadataForURL(target,frameId,url){const resourceTreeModel=target.model(SDK.ResourceTreeModel);if(!resourceTreeModel){return null;}
  11. const frame=resourceTreeModel.frameForId(frameId);if(!frame){return null;}
  12. return Bindings.resourceMetadata(frame.resourceForURL(url));}
  13. export function resourceMetadata(resource){if(!resource||(typeof resource.contentSize()!=='number'&&!resource.lastModified())){return null;}
  14. return new Workspace.UISourceCodeMetadata(resource.lastModified(),resource.contentSize());}
  15. export function frameIdForScript(script){const executionContext=script.executionContext();if(executionContext){return executionContext.frameId||'';}
  16. const resourceTreeModel=script.debuggerModel.target().model(SDK.ResourceTreeModel);if(!resourceTreeModel||!resourceTreeModel.mainFrame){return'';}
  17. return resourceTreeModel.mainFrame.id;}
  18. self.Bindings=self.Bindings||{};Bindings=Bindings||{};Bindings.resourceForURL=resourceForURL;Bindings.displayNameForURL=displayNameForURL;Bindings.metadataForURL=metadataForURL;Bindings.resourceMetadata=resourceMetadata;Bindings.frameIdForScript=frameIdForScript;