Resource.js 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. export default class Resource{constructor(resourceTreeModel,request,url,documentURL,frameId,loaderId,type,mimeType,lastModified,contentSize){this._resourceTreeModel=resourceTreeModel;this._request=request;this.url=url;this._documentURL=documentURL;this._frameId=frameId;this._loaderId=loaderId;this._type=type||Common.resourceTypes.Other;this._mimeType=mimeType;this._lastModified=lastModified&&lastModified.isValid()?lastModified:null;this._contentSize=contentSize;this._content;this._contentLoadError;this._contentEncoded;this._pendingContentCallbacks=[];if(this._request&&!this._request.finished){this._request.addEventListener(SDK.NetworkRequest.Events.FinishedLoading,this._requestFinished,this);}}
  2. lastModified(){if(this._lastModified||!this._request){return this._lastModified;}
  3. const lastModifiedHeader=this._request.responseLastModified();const date=lastModifiedHeader?new Date(lastModifiedHeader):null;this._lastModified=date&&date.isValid()?date:null;return this._lastModified;}
  4. contentSize(){if(typeof this._contentSize==='number'||!this._request){return this._contentSize;}
  5. return this._request.resourceSize;}
  6. get request(){return this._request;}
  7. get url(){return this._url;}
  8. set url(x){this._url=x;this._parsedURL=new Common.ParsedURL(x);}
  9. get parsedURL(){return this._parsedURL;}
  10. get documentURL(){return this._documentURL;}
  11. get frameId(){return this._frameId;}
  12. get loaderId(){return this._loaderId;}
  13. get displayName(){return this._parsedURL.displayName;}
  14. resourceType(){return this._request?this._request.resourceType():this._type;}
  15. get mimeType(){return this._request?this._request.mimeType:this._mimeType;}
  16. get content(){return this._content;}
  17. contentURL(){return this._url;}
  18. contentType(){if(this.resourceType()===Common.resourceTypes.Document&&this.mimeType.indexOf('javascript')!==-1){return Common.resourceTypes.Script;}
  19. return this.resourceType();}
  20. async contentEncoded(){await this.requestContent();return this._contentEncoded;}
  21. requestContent(){if(typeof this._content!=='undefined'){return Promise.resolve({content:(this._content),isEncoded:this._contentEncoded});}
  22. let callback;const promise=new Promise(fulfill=>callback=fulfill);this._pendingContentCallbacks.push(callback);if(!this._request||this._request.finished){this._innerRequestContent();}
  23. return promise;}
  24. canonicalMimeType(){return this.contentType().canonicalMimeType()||this.mimeType;}
  25. async searchInContent(query,caseSensitive,isRegex){if(!this.frameId){return[];}
  26. if(this.request){return this.request.searchInContent(query,caseSensitive,isRegex);}
  27. const result=await this._resourceTreeModel.target().pageAgent().searchInResource(this.frameId,this.url,query,caseSensitive,isRegex);return result||[];}
  28. async populateImageSource(image){const{content}=await this.requestContent();const encoded=this._contentEncoded;image.src=Common.ContentProvider.contentAsDataURL(content,this._mimeType,encoded)||this._url;}
  29. _requestFinished(){this._request.removeEventListener(SDK.NetworkRequest.Events.FinishedLoading,this._requestFinished,this);if(this._pendingContentCallbacks.length){this._innerRequestContent();}}
  30. async _innerRequestContent(){if(this._contentRequested){return;}
  31. this._contentRequested=true;let loadResult;if(this.request){const contentData=await this.request.contentData();this._content=contentData.content;this._contentEncoded=contentData.encoded;loadResult={content:(contentData.content),isEncoded:contentData.encoded};}else{const response=await this._resourceTreeModel.target().pageAgent().invoke_getResourceContent({frameId:this.frameId,url:this.url});if(response[Protocol.Error]){this._contentLoadError=response[Protocol.Error];this._content=null;loadResult={error:response[Protocol.Error],isEncoded:false};}else{this._content=response.content;this._contentLoadError=null;loadResult={content:response.content,isEncoded:response.base64Encoded};}
  32. this._contentEncoded=response.base64Encoded;}
  33. if(this._content===null){this._contentEncoded=false;}
  34. for(const callback of this._pendingContentCallbacks.splice(0)){callback(loadResult);}
  35. delete this._contentRequested;}
  36. hasTextContent(){if(this._type.isTextType()){return true;}
  37. if(this._type===Common.resourceTypes.Other){return!!this._content&&!this._contentEncoded;}
  38. return false;}
  39. frame(){return this._resourceTreeModel.frameForId(this._frameId);}}
  40. self.SDK=self.SDK||{};SDK=SDK||{};SDK.Resource=Resource;