// // marketplacesInject.js // // Created by David Rowe on 12 Nov 2016. // Copyright 2016 High Fidelity, Inc. // // Injected into marketplace Web pages. // // Distributed under the Apache License, Version 2.0. // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // (function () { function injectCommonCode(isDirectoryPage) { // 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( '' ); // Footer. var isInitialDirectoryPage = location.href.match(/\/scripts\/system\/html\/marketplaces\.html$/); $("body").append( '
' ); // Footer actions. $("#back-button").on("click", function () { window.history.back(); }); $("#all-markets").on("click", function () { EventBridge.emitWebEvent("GOTO_DIRECTORY"); }); } function injectDirectoryCode() { // Remove e-mail hyperlink. var letUsKnow = $("#letUsKnow"); letUsKnow.replaceWith(letUsKnow.html()); // Add button links. $('#exploreClaraMarketplace').on('click', function () { window.location = "https://clara.io/library?gameCheck=true&public=true" }); $('#exploreHifiMarketplace').on('click', function () { window.location = "http://www.highfidelity.com/marketplace" }); } function injectHiFiCode() { // Nothing to do. } function updateClaraCode(currentLocation) { // Have to manually monitor location for changes because Clara Web page replaced content rather than loading new page. if (location.href !== currentLocation) { // Clara library page. if (location.href.indexOf("clara.io/library") !== -1) { // Make entries navigate to "Image" view instead of default "Real Time" view. 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"); } } } // Clara item page. if (location.href.indexOf("clara.io/view/") !== -1) { // Make site navigation links retain gameCheck etc. parameters. 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); } // Replace download options with a single, "Download to High Fidelity" option. 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(); }); } // Automatic download to High Fidelity. var downloadTimer; function startAutoDownload() { window.scrollTo(0, 0); // Scroll to top ready for history.back(). 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 for " + href); $("a.btn.cancel").click(); history.back(); // Remove history item created by clicking "download". }; } else { clearInterval(downloadTimer); downloadTimer = null; } } } currentLocation = location.href; } } function injectClaraCode() { // Make space for marketplaces footer in Clara pages. $("head").append( '' ); // Update code injected per page displayed. var currentLocation = ""; var checkLocationInterval = undefined; updateClaraCode(currentLocation); checkLocationInterval = setInterval(function () { updateClaraCode(currentLocation); }, 1000); window.addEventListener("unload", function () { clearInterval(checkLocationInterval); checkLocationInterval = undefined; currentLocation = ""; }); } function onLoad() { var DIRECTORY = 0; var HIFI = 1; var CLARA = 2; var pageType = DIRECTORY; if (location.href.indexOf("highfidelity.com/") !== -1) { pageType = HIFI; } if (location.href.indexOf("clara.io/") !== -1) { pageType = CLARA; } injectCommonCode(pageType === DIRECTORY); switch (pageType) { case DIRECTORY: injectDirectoryCode(); break; case HIFI: injectHiFiCode(); break; case CLARA: injectClaraCode(); break; } } // Load / unload. try { // This appears more responsive to the user but $ is not necessarily loaded in time for each marketplace. $(document).ready(function () { onLoad(); }); } catch (e) { window.addEventListener("load", onLoad); } }());