figshare.js (2499B)
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 /** 8 * Bug 1895990 - figshare login broken with dFPI enabled 9 * 10 * The websites that use figshare for login require unpartitioned third-party 11 * cookie access for https://figshare.com. The figshare login process sets a 12 * third-party cookie for https://figshare.com, which is used as an proof of 13 * authentication on redirect back to the main site. This shim adds a request 14 * for storage access for https://figshare.com when the user tries to log in. 15 */ 16 17 // Third-party origin we need to request storage access for. 18 const STORAGE_ACCESS_ORIGIN = "https://figshare.com"; 19 20 console.warn( 21 `When logging in, Firefox calls the Storage Access API on behalf of the site. See https://bugzilla.mozilla.org/show_bug.cgi?id=1895990 for details.` 22 ); 23 24 document.documentElement.addEventListener( 25 "click", 26 e => { 27 const { target, isTrusted } = e; 28 if (!isTrusted) { 29 return; 30 } 31 32 // If the user clicks the login link, we need to request storage access 33 // for https://figshare.com. 34 const link = target.closest(`a[href^="https://login.figshare.com"]`); 35 if (!link) { 36 return; 37 } 38 39 console.warn( 40 "Calling the Storage Access API on behalf of " + STORAGE_ACCESS_ORIGIN 41 ); 42 43 e.stopPropagation(); 44 e.preventDefault(); 45 document.requestStorageAccessForOrigin(STORAGE_ACCESS_ORIGIN).then(() => { 46 link.click(); 47 }); 48 }, 49 true 50 ); 51 52 function watchFirefoxNotificationDialogAndHide() { 53 const observer = new MutationObserver((mutations, obs) => { 54 const element = document.querySelector( 55 "div[data-alerts-channel='firefox-notifications']" 56 ); 57 if (element) { 58 element.style.display = "none"; 59 obs.disconnect(); // Stop observing once we've found and hidden the element 60 } 61 }); 62 63 // Start observing the document. 64 observer.observe(document.body, { 65 childList: true, 66 subtree: true, 67 }); 68 } 69 70 // Hide the Firefox error notification 71 const notificationElement = document.querySelector( 72 "div[data-alerts-channel='firefox-notifications']" 73 ); 74 if (notificationElement) { 75 notificationElement.style.display = "none"; 76 } else { 77 // Add a listener to watch for the Firefox notification element to load. 78 window.addEventListener( 79 "DOMContentLoaded", 80 watchFirefoxNotificationDialogAndHide 81 ); 82 }