diff --git a/raw/1.0.0/index.js b/raw/1.0.0/index.js new file mode 100644 index 0000000..abeeab8 --- /dev/null +++ b/raw/1.0.0/index.js @@ -0,0 +1,142 @@ +(function () { + var authWindow; + + var darkModeSvg = ` + + + + + + + + + + + + + + + + + + + + + + + + + + + + +`; + + var lightModeSvg = ` + + + + + + + + + + + + + + + + + + + + + + + + + + + + +`; + + function handleMessage(event, authOrigin, successCallback) { + if (event.origin === authOrigin && event.data.is_authenticated) { + if (typeof window[successCallback] === "function") { + window[successCallback](event.data); // Call the global callback function + } + + if (authWindow) { + authWindow.close(); + } + + window.removeEventListener("message", handleMessage); + } + } + + function handleSignIn( + neynarLoginUrl, + clientId, + redirectUri, + successCallback + ) { + var authUrl = new URL(neynarLoginUrl); + authUrl.searchParams.append("client_id", clientId); + if (redirectUri) { + authUrl.searchParams.append("redirect_uri", redirectUri); + } + + var authOrigin = new URL(neynarLoginUrl).origin; + authWindow = window.open(authUrl.toString(), "_blank"); + window.addEventListener( + "message", + function (event) { + handleMessage(event, authOrigin, successCallback); + }, + false + ); + } + + function createSignInButton(element) { + var clientId = element.getAttribute("data-client_id"); + var neynarLoginUrl = element.getAttribute("data-neynar_login_url"); + var redirectUri = element.getAttribute("data-redirect_uri"); + var successCallback = element.getAttribute("data-success-callback"); + var theme = element.getAttribute("data-theme"); + + if (!clientId || !neynarLoginUrl) { + console.error("Neynar Signin: Missing required data attributes"); + return; + } + + // Check if the button already exists + var existingButton = element.querySelector("button"); + if (existingButton) { + return; // If button exists, do not create a new one + } + + var button = document.createElement("button"); + button.innerHTML = theme === "dark" ? darkModeSvg : lightModeSvg; + button.onclick = function () { + handleSignIn(neynarLoginUrl, clientId, redirectUri, successCallback); + }; + + button.style.cursor = "pointer"; + + element.appendChild(button); + } + + function init() { + var signinElements = document.querySelectorAll(".neynar_signin"); + signinElements.forEach(createSignInButton); + } + + if (document.readyState === "loading") { + document.addEventListener("DOMContentLoaded", init); + } else { + init(); + } +})();