import { trimMultiline , waitForCondition } from './misc' ; export interface IPageContextInfo extends _spPageContextInfo { __webAbsoluteUrl ? : string ; __siteAbsoluteUrl ? : string ; } export const isLocalhost = Boolean ( window . location . hostname === 'localhost' || window . location . hostname === '[::1]' || window . location . hostname . match ( /^127(?:\.(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)){3}$/ ) ) ; export const loadPageContext = async ( proxyEndpoint ? , timeout = 100 , tries = 50 ) : Promise < Partial < IPageContextInfo | undefined > > => { if ( typeof _spPageContextInfo !== 'undefined' ) { return _spPageContextInfo ; } if ( isLocalhost ) { const apiEndpoint = proxyEndpoint || window . location . origin ; const getWebInfo = ( ) : Promise < any > => { const restUrl = trimMultiline ( `
        ${ apiEndpoint }/_api/web?
          $select=Title,Language,ServerRelativeUrl,Url,
            CurrentUser/Id,CurrentUser/LoginName,CurrentUser/Email,CurrentUser/Title,CurrentUser/IsSiteAdmin&
          $expand=CurrentUser
      ` ) ; return fetch ( restUrl , { method : 'GET' , headers : [ [ 'Accept' , 'application/json;odata=verbose' ] , [ 'X-ProxyStrict' , 'false' ] ] } ) . then ( ( res ) => res . json ( ) ) ; } ; const getSiteInfo = ( ) : Promise < any > => { const restUrl = `${ apiEndpoint }/_api/site?$select=ServerRelativeUrl,Url` ; return fetch ( restUrl , { method : 'GET' , headers : [ [ 'Accept' , 'application/json;odata=verbose' ] , [ 'X-ProxyStrict' , 'false' ] ] } ) . then ( ( res ) => res . json ( ) ) ; } ; return Promise . all ( [ getWebInfo ( ) , getSiteInfo ( ) ] ) . then ( ( [ { d : webInfo } , { d : siteInfo } ] ) => { const _spPageContextInfoFake = { webTitle : webInfo . Title , webAbsoluteUrl : apiEndpoint + webInfo . ServerRelativeUrl , webServerRelativeUrl : webInfo . ServerRelativeUrl , currentLanguage : webInfo . Language , siteAbsoluteUrl : apiEndpoint + siteInfo . ServerRelativeUrl , siteServerRelativeUrl : siteInfo . ServerRelativeUrl , userId : webInfo . CurrentUser . Id , userLoginName : webInfo . CurrentUser . LoginName , userDisplayName : webInfo . CurrentUser . Title , userEmail : webInfo . CurrentUser . Email , isSiteAdmin : webInfo . CurrentUser . IsSiteAdmin , __webAbsoluteUrl : webInfo . Url , __siteAbsoluteUrl : siteInfo . Url , } ; ( window as unknown as { _spPageContextInfo : Partial < IPageContextInfo > ; } ) . _spPageContextInfo = _spPageContextInfoFake ; return _spPageContextInfoFake ; } ) ; } SP . SOD . executeFunc ( 'sp.js' , 'SP.ClientContext' , ( ) => { } ) ; await waitForCondition ( ( ) => typeof _spPageContextInfo !== 'undefined' , timeout , tries ) ; return _spPageContextInfo ; } ;