const apiURLbase = "https://cmeapi.infosentience.com/CME/"; const apiEndpoint = "GetHTML"; const apiDirectEndpoint = "CMEDirectHTML"; let css = ""; function getCommodityCode() { let url = new URL(location.href); let commodityCode = url.searchParams.get('product'); return commodityCode == null ? "" : commodityCode; } function getUserId() { let url = new URL(location.href); let userId = url.searchParams.get('userId'); return userId == null ? "" : userId; } function getEnvCode() { let url = new URL(location.href); let envCode = url.searchParams.get('env'); return envCode == null ? "" : envCode; } function getEnvParam() { let envCode = getEnvCode(); if (envCode == "") return ""; return "&env=" + envCode; } function getCSSParam() { if (css == "") return ""; return "&excludecss=true"; } function getIdParam() { let id = getUserId(); if (id == "") return ""; return "&userId=" + id; } function makeRequestForCommodity(useCommodityCode) { let url = getApiUrl(useCommodityCode); var http = new XMLHttpRequest(); http.onreadystatechange = getResponseFunc(http); http.open("GET", url, true); http.send(); } function getCommodityData() { makeRequestForCommodity(getCommodityCode()); } function getApiUrl(useCommodityCode) { let endpoint = getDashboardDiv() == null ? apiDirectEndpoint : apiEndpoint; return apiURLbase + endpoint + "?product=" + useCommodityCode + getEnvParam() + getCSSParam() + getIdParam(); } function getResponseFunc(useReq) { let func = function() { if (useReq.readyState == XMLHttpRequest.DONE) { if (useReq.status == 200) { handleResponseText(useReq.responseText); } else if (useReq.status == 400) { alert('Error fetching dashboard for commodity ' + getCommodityCode()) } } } return func; } function handleResponseText(useText) { setDashboardHTML(getDashboardContainer(), useText); reducePadding(); } function getDashboardContainer() { var div = document.getElementById("dashboard-div"); if (div != null) { return div; } return document.body; } function getDashboardDiv() { return document.getElementById("dashboard-div"); } function clearDashboardContainer() { let container = getDashboardContainer() while (container.children.length > 0) { container.removeChild(container.children[0]); } } function setDashboardHTML(elm, html, append = false) { if (css != "") { html = insertCSS(html); } else { stripCSS(html); } if (append) { elm.innerHTML += html; } else elm.innerHTML = html; let scripts = Array.from(elm.querySelectorAll("script")); if (scripts.length < 1) return; makeNewScript(scripts[0], scripts); } function stripCSS(htmlStr) { var startIndex = htmlStr.indexOf("") + 6; var retStr = htmlStr.slice(0, cssIndex) + css + htmlStr.slice(cssIndex); return retStr; } function makeNewScript(oldScript, scripts) { if (oldScript === undefined || oldScript === null) { return;} const newScript = document.createElement("script"); Array.from(oldScript.attributes) .forEach(attr => newScript.setAttribute(attr.name, attr.value)); newScript.appendChild(document.createTextNode(oldScript.innerHTML)); if (true || oldScript.innerHTML != "") { oldScript.parentNode.replaceChild(newScript, oldScript); } else { oldScript.parentNode.removeChild(oldScript); document.getElementsByTagName("head")[0].appendChild(newScript); } if (oldScript.id == "bsgjs" || oldScript.id == "apexjs") { newScript.onload = function() { makeNewScript(getNextScript(oldScript, scripts), scripts); } } else { makeNewScript(getNextScript(oldScript, scripts), scripts); } } function getNextScript(oldScript, scripts) { if (scripts === undefined) return; let ind = scripts.indexOf(oldScript); if (ind >= scripts.length - 1 || ind < 0) { return null; } else { return scripts[ind + 1]; } } function resetPage(newCommodity) { if (newCommodity.trim() == "") return; clearDashboardContainer() makeRequestForCommodity(newCommodity); var newUrl = removeParam("chart"); history.replaceState(null, newCommodity, replaceUrlParam(newUrl, "product", newCommodity)); } function replaceUrlParam(url, paramName, paramValue) { if (paramValue == null) { paramValue = ''; } var pattern = new RegExp('\\b('+paramName+'=).*?(&|#|$)'); if (url.search(pattern)>=0) { return url.replace(pattern,'$1' + paramValue + '$2'); } url = url.replace(/[?#]$/,''); return url + (url.indexOf('?')>0 ? '&' : '?') + paramName + '=' + paramValue; } function removeParam(param) { var url = new URL(window.location.href); var params = new URLSearchParams(url.search); params.delete(param); return url.origin + url.pathname + "?" + params.toString(); } function addParamToUrl(paramName, paramValue) { var url = new URL(window.location.href); var params = new URLSearchParams(url.search); params.delete(paramName); params.append(paramName, paramValue); history.replaceState(null, "", url.origin + url.pathname + "?" + params.toString()); } function selectChartFromParams() { var urlParams = new URLSearchParams(window.location.search); } function getCarouselItemByHeader(useHeader) { var items = document.getElementsByClassName("carousel-item"); for (var i = 0; i < items.length; i++) { if (items[i].getAttribute("header-text") == useHeader) return items[i]; } } function getMQStyle() { return "@media (max-width:1024px) { .container.default-vertical-padding{padding-left:0px !important;padding-right:0px !important;padding-top:0px !important;})" } function reducePadding() { var el = document.getElementById("dashboard-div"); var style = document.createElement("style"); style.textContent = getMQStyle(); el.appendChild(style); } function setProductCookie() { var urlParams = new URLSearchParams(window.location.search); var code = urlParams.get('product'); document.cookie = "product=" + code + ";"; } function setTradeDateCookie() { var dashDiv = document.getElementById("dashboard-div"); var meta = dashDiv.getElementsByTagName("meta")[0]; document.cookie = "tradeDate=" + meta.dataset["tradeDate"] + ";"; } function isMarketPulse() { return getDashboardDiv() != null; } function initMarketPulse() { getDashboardContainer().style.width = "100%"; } if (isMarketPulse()) { getCommodityData(); initMarketPulse(); }