|
| 1 | +/* |
| 2 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 4 | + * file, You can obtain one at https://mozilla.org/MPL/2.0/. |
| 5 | + */ |
| 6 | + |
| 7 | +let timeout; |
| 8 | +let requestComplete = false; |
| 9 | + |
| 10 | +/** |
| 11 | + * Determine if browser should attempt to download Firefox on page load. |
| 12 | + * @param {String} platform |
| 13 | + * @param {Boolean} fxSupported |
| 14 | + * @returns {Boolean} |
| 15 | + */ |
| 16 | +function shouldAutoDownload(platform, fxSupported) { |
| 17 | + const supportedPlatforms = ['windows', 'osx', 'android', 'ios']; |
| 18 | + |
| 19 | + if (fxSupported && supportedPlatforms.indexOf(platform) !== -1) { |
| 20 | + return true; |
| 21 | + } |
| 22 | + |
| 23 | + return false; |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Get the Firefox download link for the appropriate platform. |
| 28 | + * @param {Object} window.site |
| 29 | + * @returns {String} download url |
| 30 | + */ |
| 31 | +function getDownloadURL(site) { |
| 32 | + // TODO: confirm if we want to hardcode 'thanks-' prefix here |
| 33 | + // It is part of a helper, but maybe unnecessarily specific to /thanks page? |
| 34 | + const prefix = 'thanks-download-button-'; |
| 35 | + let link; |
| 36 | + let url; |
| 37 | + |
| 38 | + switch (site.platform) { |
| 39 | + case 'windows': |
| 40 | + link = document.getElementById(prefix + 'win'); |
| 41 | + break; |
| 42 | + case 'osx': |
| 43 | + link = document.getElementById(prefix + 'osx'); |
| 44 | + break; |
| 45 | + case 'linux': |
| 46 | + // Linux users get SUMO install instructions. |
| 47 | + link = null; |
| 48 | + break; |
| 49 | + case 'android': |
| 50 | + link = document.getElementById(prefix + 'android'); |
| 51 | + break; |
| 52 | + case 'ios': |
| 53 | + link = document.getElementById(prefix + 'ios'); |
| 54 | + break; |
| 55 | + } |
| 56 | + |
| 57 | + if (link && link.href) { |
| 58 | + url = link.href; |
| 59 | + } |
| 60 | + |
| 61 | + return url; |
| 62 | +} |
| 63 | + |
| 64 | +/** |
| 65 | + * Auto-start download |
| 66 | + */ |
| 67 | +function beginFirefoxDownload() { |
| 68 | + const directDownloadLink = document.getElementById('direct-download-link'); |
| 69 | + let downloadURL; |
| 70 | + |
| 71 | + // Only auto-start the download if a supported platform is detected. |
| 72 | + if ( |
| 73 | + shouldAutoDownload(window.site.platform, window.site.fxSupported) && |
| 74 | + typeof Mozilla.Utils !== 'undefined' |
| 75 | + ) { |
| 76 | + downloadURL = getDownloadURL(window.site); |
| 77 | + |
| 78 | + if (downloadURL) { |
| 79 | + // Pull download link from the download button and add to the 'Try downloading again' link. |
| 80 | + // Make sure the 'Try downloading again' link is well formatted! (issue 9615) |
| 81 | + if (directDownloadLink && directDownloadLink.href) { |
| 82 | + directDownloadLink.href = downloadURL; |
| 83 | + directDownloadLink.addEventListener( |
| 84 | + 'click', |
| 85 | + (event) => { |
| 86 | + try { |
| 87 | + Mozilla.TrackProductDownload.handleLink(event); |
| 88 | + } catch (error) { |
| 89 | + return; |
| 90 | + } |
| 91 | + }, |
| 92 | + false |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + // Start the platform-detected download a second after DOM ready event. |
| 97 | + Mozilla.Utils.onDocumentReady(() => { |
| 98 | + setTimeout(() => { |
| 99 | + try { |
| 100 | + Mozilla.TrackProductDownload.sendEventFromURL( |
| 101 | + downloadURL |
| 102 | + ); |
| 103 | + } catch (error) { |
| 104 | + return; |
| 105 | + } |
| 106 | + window.location.href = downloadURL; |
| 107 | + }, 1000); |
| 108 | + }); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + * On success of new download attribution request |
| 115 | + */ |
| 116 | +function onSuccess() { |
| 117 | + // Make sure we only initiate the download once! |
| 118 | + clearTimeout(timeout); |
| 119 | + if (requestComplete) { |
| 120 | + return; |
| 121 | + } |
| 122 | + requestComplete = true; |
| 123 | + |
| 124 | + // Fire GA event to log attribution success |
| 125 | + // GA4 |
| 126 | + window.dataLayer.push({ |
| 127 | + event: 'widget_action', |
| 128 | + type: 'direct-attribution', |
| 129 | + action: 'success', |
| 130 | + non_interaction: true |
| 131 | + }); |
| 132 | + |
| 133 | + beginFirefoxDownload(); |
| 134 | +} |
| 135 | + |
| 136 | +/** |
| 137 | + * On timeout of new download attribution request |
| 138 | + */ |
| 139 | +function onTimeout() { |
| 140 | + // Make sure we only initiate the download once! |
| 141 | + clearTimeout(timeout); |
| 142 | + if (requestComplete) { |
| 143 | + return; |
| 144 | + } |
| 145 | + requestComplete = true; |
| 146 | + |
| 147 | + // Fire GA event to log attribution timeout |
| 148 | + // GA4 |
| 149 | + window.dataLayer.push({ |
| 150 | + event: 'widget_action', |
| 151 | + type: 'direct-attribution', |
| 152 | + action: 'timeout', |
| 153 | + non_interaction: true |
| 154 | + }); |
| 155 | + |
| 156 | + beginFirefoxDownload(); |
| 157 | +} |
| 158 | + |
| 159 | +// Force cookie update if there is essential data to add |
| 160 | +if ( |
| 161 | + Mozilla.DownloadAttribution !== undefined && |
| 162 | + document.documentElement.hasAttribute( |
| 163 | + 'data-stub-attribution-campaign-force' |
| 164 | + ) |
| 165 | +) { |
| 166 | + // Custom success and timeout callbacks are only relevant for direct attribution |
| 167 | + // (i.e. when we have updated the cookie on the auto-download page) |
| 168 | + Mozilla.DownloadAttribution.successCallback = onSuccess; |
| 169 | + Mozilla.DownloadAttribution.timeoutCallback = onTimeout; |
| 170 | + // Don't wait too long before starting download |
| 171 | + // (even if it means we have to leave the essential information out) |
| 172 | + timeout = setTimeout(onTimeout, 2000); |
| 173 | + Mozilla.DownloadAttribution.initEssential(); |
| 174 | +} else { |
| 175 | + beginFirefoxDownload(); |
| 176 | +} |
| 177 | + |
| 178 | +// Bug 1354334 - add a hint for test automation that page has loaded. |
| 179 | +document.getElementsByTagName('html')[0].classList.add('download-ready'); |
0 commit comments