insomnia-plugin-valorant
Version:
Adds template tags to Insomnia with Valorant data
101 lines • 4.37 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.openWebViewPopup = void 0;
const parse_auth_redirect_1 = require("./parse-auth-redirect");
/**
* Opens a popup window to sign in to Riot
* The promise resolves with the access token and expiration time
* If the window is closed, the promise rejects with an error message
* @param context The Insomnia context, used for opening a dialog element
*/
async function openWebViewPopup(context) {
return new Promise((resolve, reject) => {
const valWebView = document.createElement('webview');
valWebView.style.display = 'none';
valWebView.classList.add('val-webview');
valWebView.nodeintegration = false;
// Set partition to avoid Insomnia stripping out the Origin headers needed for CORS
valWebView.partition = 'persist:valorant';
let shownSignIn = false;
let readyForHide = false;
let cleanedUp = false;
// Event handler to check for tokens in the urls of navigated and redirected events
const checkForToken = async (event) => {
if (event.url.startsWith('https://playvalorant.com/') && event.url.includes('access_token')) {
cleanupWebView();
// Close model
if (shownSignIn) {
const closeButtons = document.getElementsByClassName('modal__close-btn');
if (closeButtons.length !== 0) {
readyForHide = true;
for (const closeButton of closeButtons) {
closeButton.click();
}
}
}
try {
resolve((0, parse_auth_redirect_1.parseAuthRedirect)(event.url));
}
catch (e) {
reject(e);
}
}
};
// Event handler for when the modal is closed
const hideHandler = () => {
cleanupWebView();
if (!readyForHide) {
reject('Window closed');
}
};
const redirectHandler = (event) => {
checkForToken(event);
};
const navigateHandler = (event) => {
if (event.url.startsWith('https://authenticate.riotgames.com') && !shownSignIn) {
shownSignIn = true;
// Add styling to dom
const styleID = 'val-auth-style';
if (document.getElementById(styleID) === null) {
const style = document.createElement('style');
style.id = styleID;
style.innerHTML = `
div:has(> .val-webview) {
height: 100%;
}
`;
document.head.appendChild(style);
}
document.body.removeChild(valWebView);
valWebView.style.removeProperty('display');
context.app.dialog('Riot Sign In', valWebView, {
tall: true,
wide: true,
onHide: hideHandler
});
}
checkForToken(event);
};
const cleanupWebView = () => {
if (cleanedUp)
return;
cleanedUp = true;
valWebView.removeEventListener('did-redirect-navigation', redirectHandler);
valWebView.removeEventListener('did-navigate', navigateHandler);
// Ignore errors relating to the webview not being attached to the dom
try {
valWebView.stop();
}
catch (ignored) { }
if (!shownSignIn) {
document.body.removeChild(valWebView);
}
};
valWebView.addEventListener('did-redirect-navigation', redirectHandler);
valWebView.addEventListener('did-navigate', navigateHandler);
valWebView.src = 'https://auth.riotgames.com/authorize?redirect_uri=https%3A%2F%2Fplayvalorant.com%2Fopt_in&client_id=play-valorant-web-prod&response_type=token%20id_token&nonce=1&scope=account%20openid';
document.body.appendChild(valWebView);
});
}
exports.openWebViewPopup = openWebViewPopup;
//# sourceMappingURL=open-webview-popup.js.map