diff --git a/scripts/system/html/js/marketplacesClara.js b/scripts/system/html/js/marketplacesClara.js index 6ab9b860d9..d4e5c870ed 100644 --- a/scripts/system/html/js/marketplacesClara.js +++ b/scripts/system/html/js/marketplacesClara.js @@ -10,50 +10,132 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // -function onLoad() { - // Supporting styles from marketplaces.css. - // Glyph font family, size, and spacing adjusted because HiFi-Glyphs cannot be used cross-domain. - $("head").append( - '' - ); +(function () { + // Can't use $(document).ready() because jQuery isn't loaded early enough by Clara Web page. - // Supporting styles from edit-style.css. - // Font family, size, and position adjusted because Raleway-Bold cannot be used cross-domain. - $("head").append( - '' - ); + var locationHref = ""; + var checkLocationInterval = undefined; - // Make space for marketplaces footer. - $("head").append( - '' - ); + function checkLocation() { + // Have to manually monitor location for changes because Clara Web page replaced content rather than loading new page. - // Marketplaces footer. - $("body").append( - '
' + - '🛈 Check out other markets.' + - '' + - '
' - ); + if (location.href !== locationHref) { - // Marketplace footer action. - $("#all-markets").on("click", function () { - $("#marketplace-content").attr("src", "marketplacesDirectory.html"); - EventBridge.emitWebEvent("RELOAD_DIRECTORY"); - }); -} + // Clara library page. + if (location.href.indexOf("clara.io/library") !== -1) { + var elements = $("a.thumbnail"); + for (var i = 0, length = elements.length; i < length; i++) { + var value = elements[i].getAttribute("href"); + if (value.slice(-6) !== "/image") { + elements[i].setAttribute("href", value + "/image"); + } + } + } -window.addEventListener("load", onLoad); + // Clara item page. + if (location.href.indexOf("clara.io/view/") !== -1) { + var element = $("a[href^=\'/library\']")[0]; + var parameters = "?gameCheck=true&public=true"; + var href = element.getAttribute("href"); + if (href.slice(-parameters.length) !== parameters) { + element.setAttribute("href", href + parameters); + } + var buttons = $("a.embed-button").parent("div"); + if (buttons.length > 0) { + var downloadFBX = buttons.find("a[data-extension=\'fbx\']")[0]; + downloadFBX.addEventListener("click", startAutoDownload); + var firstButton = buttons.children(":first-child")[0]; + buttons[0].insertBefore(downloadFBX, firstButton); + downloadFBX.setAttribute("class", "btn btn-primary download"); + downloadFBX.innerHTML = " Download to High Fidelity"; + buttons.children(":nth-child(2), .btn-group , .embed-button").each(function () { this.remove(); }); + } + var downloadTimer; + function startAutoDownload() { + if (!downloadTimer) { + downloadTimer = setInterval(autoDownload, 1000); + } + } + function autoDownload() { + if ($("div.download-body").length !== 0) { + var downloadButton = $("div.download-body a.download-file"); + if (downloadButton.length > 0) { + clearInterval(downloadTimer); + downloadTimer = null; + var href = downloadButton[0].href; + EventBridge.emitWebEvent("CLARA.IO DOWNLOAD " + href); + console.log("Clara.io FBX file download initiated"); + $("a.btn.cancel").click(); + setTimeout(function () { window.open(href); }, 500); // Let cancel click take effect. + }; + } else { + clearInterval(downloadTimer); + downloadTimer = null; + } + } + } + + locationHref = location.href; + } + } + + function onLoad() { + + // Supporting styles from marketplaces.css. + // Glyph font family, size, and spacing adjusted because HiFi-Glyphs cannot be used cross-domain. + $("head").append( + '' + ); + + // Supporting styles from edit-style.css. + // Font family, size, and position adjusted because Raleway-Bold cannot be used cross-domain. + $("head").append( + '' + ); + + // Make space for marketplaces footer. + $("head").append( + '' + ); + + // Marketplaces footer. + $("body").append( + '
' + + '🛈 Check out other markets.' + + '' + + '
' + ); + + // Marketplace footer action. + $("#all-markets").on("click", function () { + $("#marketplace-content").attr("src", "marketplacesDirectory.html"); + EventBridge.emitWebEvent("RELOAD_DIRECTORY"); + }); + + checkLocation(); + checkLocationInterval = setInterval(checkLocation, 1000); + } + + function onUnload() { + clearInterval(checkLocationInterval); + checkLocationInterval = undefined; + locationHref = ""; + } + + window.addEventListener("load", onLoad); + window.addEventListener("unload", onUnload); + +}());