Last active
January 26, 2021 20:29
-
-
Save Kolobok12309/3c469723783179e720d5d9a12753eb3e to your computer and use it in GitHub Desktop.
Скрипт авторизации в московском метро, `completion` callback возврата ios commands
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(async function() { | |
await new Promise((res) => { | |
if (document.readyState === 'complete') { | |
res(); | |
} else { | |
const handler = () => { | |
res(); | |
window.removeEventListener('load', handler); | |
}; | |
window.addEventListener('load', handler); | |
} | |
}); | |
const sendToken = (token) => { | |
const url = new URL(location.href); | |
const segment = url.searchParams.get('segment') || 'metro'; | |
document.cookie = 'afVideoPassed=0'; | |
return fetch(`${url.origin}/auth/init?mode=0&segment=${segment}`, { | |
method: 'POST', | |
headers: { | |
'X-CSRF-Token': token, | |
}, | |
}); | |
}; | |
class TokenExtractor { | |
extract() {} | |
} | |
class MetaTokenExtractor extends TokenExtractor { | |
extract() { | |
const metaCSRF = document.querySelector('meta[name=csrf-token]'); | |
return metaCSRF && metaCSRF.getAttribute('content'); | |
} | |
} | |
class LinkTokenExtractor extends TokenExtractor { | |
extract() { | |
const links = [...document.querySelectorAll('a')]; | |
return links.reduce((acc, link) => { | |
if (acc) return acc; | |
const match = link.href.match(/token=([^&\s]+)&?/); | |
if (match) { | |
return match[1]; | |
} | |
}, null); | |
} | |
} | |
const extractors = [new MetaTokenExtractor(), new LinkTokenExtractor()]; | |
const token = extractors | |
.reduce((acc, extractor) => acc || extractor.extract(), null); | |
if (!token) throw new Error('Токен не найден'); | |
await sendToken(token); | |
})() | |
.then(completion) | |
.catch(completion); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(async function() { | |
await new Promise((res) => { | |
if (document.readyState === 'complete') { | |
res(); | |
} else { | |
const handler = () => { | |
res(); | |
window.removeEventListener('load', handler); | |
}; | |
window.addEventListener('load', handler); | |
} | |
}); | |
const sendToken = (token) => { | |
const url = new URL(location.href); | |
const segment = url.searchParams.get('segment') || 'metro'; | |
document.cookie = 'afVideoPassed=0'; | |
return fetch(`${url.origin}/auth/init?mode=0&segment=${segment}`, { | |
method: 'POST', | |
headers: { | |
'X-CSRF-Token': token, | |
}, | |
}); | |
}; | |
class TokenExtractor { | |
extract() {} | |
} | |
class MetaTokenExtractor extends TokenExtractor { | |
extract() { | |
const metaCSRF = document.querySelector('meta[name=csrf-token]'); | |
return metaCSRF && metaCSRF.getAttribute('content'); | |
} | |
} | |
class LinkTokenExtractor extends TokenExtractor { | |
extract() { | |
const links = [...document.querySelectorAll('a')]; | |
return links.reduce((acc, link) => { | |
if (acc) return acc; | |
const match = link.href.match(/token=([^&\s]+)&?/); | |
if (match) { | |
return match[1]; | |
} | |
}, null); | |
} | |
} | |
const extractors = [new MetaTokenExtractor(), new LinkTokenExtractor()]; | |
const token = extractors | |
.reduce((acc, extractor) => acc || extractor.extract(), null); | |
if (!token) throw new Error('Токен не найден'); | |
await sendToken(token); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment