import { hasContainLoginCodeAndState, parseQueryResult } from '../utils' async function handleRedirectCallback( url: string = window.location.href, stop = false, ) { if (!hasContainLoginCodeAndState()) return const queryStringFragments = url.split('?').slice(1).length !== 0 ? url.split('?').slice(1) : url.split('#').slice(1) if (queryStringFragments.length === 0) throw new Error('There are no query params available for parsing.') const { state, code, error, error_description } = parseQueryResult( queryStringFragments.join(''), ) if ( stop !== true && queryStringFragments[0] !== '' && (window.self !== window.top || (window.self !== window.parent.opener && window.parent.opener !== null)) ) { // 给父页面发送消息(iframe和pupop两种方式) const target = window.parent.opener || window.parent target.postMessage( { type: 'authorization_response', response: { code, state, }, }, '*', ) } } handleRedirectCallback() window.addEventListener('hashchange', () => { handleRedirectCallback() })