stackoverflow-login.js (1640B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 console.warn( 8 `When logging in, Firefox calls the Storage Access API on behalf of the site. See https://bugzilla.mozilla.org/show_bug.cgi?id=1949491 for details.` 9 ); 10 11 const STORAGE_ACCESS_ORIGIN = "https://stackexchange.com"; 12 13 const STACKOVERFLOW_LOGIN_HERF_PREFIX = "https://stackoverflow.com/users/login"; 14 const STACKOVERFLOW_SIGNUP_HERF_PREFIX = 15 "https://stackoverflow.com/users/signup"; 16 17 document.documentElement.addEventListener( 18 "click", 19 e => { 20 const { target, isTrusted } = e; 21 if (!isTrusted) { 22 return; 23 } 24 25 // Find the click event on button elements. 26 const button = target.closest("a.s-btn"); 27 if (!button) { 28 return; 29 } 30 31 // Only request storage access for login/signup buttons. 32 if ( 33 !button.href.startsWith(STACKOVERFLOW_LOGIN_HERF_PREFIX) && 34 !button.href.startsWith(STACKOVERFLOW_SIGNUP_HERF_PREFIX) 35 ) { 36 return; 37 } 38 39 console.warn( 40 "Calling the Storage Access API on behalf of " + STORAGE_ACCESS_ORIGIN 41 ); 42 43 // Disable the button to prevent it from being clicked again. 44 button.style.pointerEvents = "none"; 45 e.stopPropagation(); 46 e.preventDefault(); 47 document 48 .requestStorageAccessForOrigin(STORAGE_ACCESS_ORIGIN) 49 .finally(() => { 50 // Re-enable the button and proceed with the login/signup flow. 51 button.style.pointerEvents = ""; 52 button.click(); 53 }); 54 }, 55 true 56 );