"use strict"; //2023-11-20: //This function is the first to be called for this feature. //It checks the value of the "stop" querystring name. "stop" //can have a value of: undefined, null, "", 1, "1", "0", or 0. //If the "stop" parameter is equal to "1", or 1, exit the function. //This allows the feature to be used once, without entering an infinite loop. //If the "stop" parameter is not "1", or 1, then proceed with the function. //If the function is set to continue, the querystring "url" parameter is //determined. The "DOM_id" parameter specifies which element you're looking //to fetch within the HTTP data recieved from the "url". async function init_get_html() { var html_data = null; var url = null; var DOM_id = null; if (set_to_stop() !== false) { return; } else { url = get_qustring_url_value(); DOM_id = get_qustring_id_value(); //2023-12-14: //if no url is given, default to a URL that retrieves a blank pixel. if (url === "" || url === null || url === undefined) { url = "/chromosphere/images/blank.gif"; } html_data = await start_get_html(url, DOM_id); } } //This function checks the "url, and "id" querystring values //for validation. It URL encodes the strings, and starts the //initialization of the GET request to fetch HTTP data //returned by the "url" parameter. The "id" parameter //is set to "" if it hasn't been provided, or is of //a null or undefined value. async function start_get_html(url, DOM_id) { var address = null; var id = null; var requested_DOM_object = null; id = DOM_id.toString(); address = url.toString(); //2023-11-27: ////address = "/chromosphere/scripts/js/ui/null.aspx"; if (typeof id === "string" && typeof address === "string") { if (id.length >= 0 && address.length > 0) { id = encodeURI(id); address = encodeURI(address); requested_DOM_object = await init_GET_request(address, id); } } return requested_DOM_object; } //This function does the configuration work needed to //be executed before requesting the URL provided by the "url" //parameter. async function init_GET_request(address, id) { var request_data = null; request_data = await send_GET_request(address, id); return request_data; } //This function calls the function responsible for the //"url" parameter's GET request. //Secondly, it calls a function which extracts the HTML //element which is specified by the "id" parameter. If //the "id" parameter isn't provided, the function defaults //to returning all of the HTTP data of the GET request. async function send_GET_request(address, id) { var response_text = null; var element_html = null; var rendered_HTML = null; var unencoded = null; var iframe_content = null; var iframe = null; var iframe_srcdoc_node_0 = null; var imported_node = null; response_text = await get_HTML_text(address); unencoded = unencode_text(response_text); iframe = place_HTML_into_iframe(unencoded); //2023-12-14: //For reference: //iframe_srcdoc_node_0 = iframe.contentWindow.document.getElementsByTagName("*")[0]; //iframe_srcdoc_node_0 = iframe.contentWindow.document.documentElement; //imported_node = document.importNode(iframe_srcdoc_node_0); //2023-12-14: //replacing the entire DOM with the HTML content of the iframe. //document.documentElement.innerHTML = iframe.srcdoc; //2023-12-14: //Replace the HTML document with the iframe object's HTML document.documentElement.innerHTML = iframe.outerHTML; } function place_HTML_into_iframe(html = "") { var iframe = null; iframe = document.createElement("iframe"); iframe.setAttribute("id", "temp_frame"); iframe.style.position = "absolute"; iframe.style.top = "0"; iframe.style.left = "0"; iframe.style.margin = "0"; iframe.style.padding = "0"; iframe.style.width = (parseFloat(window.innerWidth) - 0) + "px"; iframe.style.height = (parseFloat(window.innerHeight) - 0) + "px"; iframe.style.border = "0"; iframe.srcdoc = html; document.body.appendChild(iframe); return iframe; } //After the GET request for the "url" parameter has returned //data, this function inserts the data into the document. //Depending on if there is a valid "id" parameter, //the function returns the outer HTML of the element specified //by "id". If "id" is a null string, or is invalid, the //function returns all HTML. If the "url" parameter isn't //valid, or is a null string, then the function returns //a zero-length string. function get_element_HTML(page_html = null, id = null) { var new_div = null; var id_string = null; var elem_html = null; var elem_obj = null; if (page_html !== null) { insert_HTTP_text_into_document(page_html, id); if (id !== null && id !== undefined) { id_string = id; elem_html = get_html_element_by_id(page_html, id); } else { elem_html = page_html; } } else { elem_html = ""; } return elem_html; } //This function uses the Fetch API to send a GET request //to the "url" parameter. It returns the HTTP response data //of the request. //NOTE: These were previously included headers //that I don't believe belonged in the GET request above. /* "Content-Type": "text/*;charset=utf-8", "Connection": "keep-alive", */ async function get_HTML_text(url) { let d = await fetch(url, { method: "GET", mode: "no-cors", headers: { "Accept": "text/html,text/javascript,text/plain,text/xml,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8" } }); let t = await d.text(); return t; } //This function takes the HTTP data of the specified "url" //parameter and places the string into the DOM. function insert_HTTP_text_into_document(t = "", id = "") { var dom_parser = null; var node = null; var head_html = null; var body_html = null; var new_doc = null; var element_html = null; //DOMParser is not needed here, but keeping it for future reference. dom_parser = new DOMParser(); node = dom_parser.parseFromString(t, "text/html"); head_html = "")[0] + ""; body_html = "")[0] + ""; new_doc = head_html + body_html; element_html = get_html_element_by_id(new_doc, id); post_new_html(window.location.href, element_html); } //This function creates a form element, and appends it to //the current web page. It submits the data using the POST method. //The form data is posted to the same ASP.NET page associated with this script. //Note that there is a "stop" parameter added to the POST URL to prevent //an infinite loop from occurring. function post_new_html(url = window.location.href, formData = "") { var form = null; var form_data_field = null; var form_data = null; var post_url = null; var qstring = null; post_url = url; if (post_url.indexOf("?") > -1.0) { post_url = post_url.split("?")[0]; } post_url = post_url + "?stop=1"; form_data = html_encode_new_html(formData); form = document.createElement("form"); form.setAttribute("method", "POST"); form.setAttribute("id", "post_new_html_form"); form.setAttribute("action", post_url); //form.setAttribute("enctype", "text/plain"); form.setAttribute("enctype", "application/x-www-form-urlencoded"); form_data_field = document.createElement("input"); form_data_field.setAttribute("type", "hidden"); form_data_field.setAttribute("name", "new_html"); form_data_field.setAttribute("value", form_data); form.appendChild(form_data_field); document.body.appendChild(form); form.submit(); } //This function HTML-Encodes all HTML text input function html_encode_new_html(html = "") { var encoded_str = null; encoded_str = html.replace(//g, ">"); return encoded_str; } //This function returns the querystring value located in //the URL string by name. The URL is the address of //the associated web page to this script. function get_qstring_by_name(url_txt, qStr_name) { var qmark_loc = null; var q_mark_loc_url_encoded = null; var url = null; var q_string = null; var qstring = null; var items_array = null; var array_item = null; var item_name_value_array = null; var item_name = null; var item_value = null; var output = null; url = url_txt.toString(); qmark_loc = url.indexOf("?"); if (qmark_loc > -1) { q_string = url.toString().substring((parseFloat(qmark_loc) + 1), parseFloat(url.toString().length)); //grab full querystring //if the name of the querystring value is present if (q_string.indexOf(qStr_name) > -1) { items_array = q_string.split("&"); if (items_array.length > 0) { for (i = 0; i < items_array.length; i++) { array_item = items_array[i]; if (array_item.indexOf("=") > 0) { item_name_value_array = array_item.split("="); item_name = item_name_value_array[0]; item_value = item_name_value_array[1]; if (item_name === qStr_name) { output = item_value; } } } } else { if (q_string.indexOf("=") > -1) { item_name_value_array = q_string.split("="); item_name = item_name_value_array[0]; item_value = item_name_value_array[1]; if (item_name === qStr_name) { output = item_value; } else { output = q_string; } } else { output = q_string; } } } else { //if the name of the querystring value is not present, return "" output = ""; } } else { output = ""; } return output; } //This function checks the current window's query string //for the "stop" parameter. If there is a "stop" parameter //set to 1, or "1", it returns true. Otherwise, it returns //false. function set_to_stop() { var stop = null; var nogo = null; nogo = false; stop = get_qstring_by_name(window.location.href, "stop").toString(); if (stop !== "" && stop !== null && stop !== undefined) { if (stop === "1" || stop === 1) { nogo = true; } } return nogo; } //This function gets the window URL's "url" parameter //in the querystring. If non-existent, or invalid, //the "url" parameter is returned as a null string. function get_qustring_url_value() { var url = null; var qstr_url = null; url = window.location.href; qstr_url = get_qstring_by_name(url, "url"); if (qstr_url !== null && qstr_url !== "" && qstr_url !== undefined) { if (typeof qstr_url === "string") { if (qstr_url.length > 0) { return qstr_url; } } } return ""; } //This function gets the window URL's "id" parameter //in the querystring. If non-existent, or invalid, //the "id" parameter is returned as a null string. function get_qustring_id_value() { var url = null; var qstr_id = null; url = window.location.href; qstr_id = get_qstring_by_name(url, "id"); if (qstr_id !== null && qstr_id !== "" && qstr_id !== undefined) { if (typeof qstr_id === "string") { if (qstr_id.length > 0) { return qstr_id; } } } return ""; } //If an "id" parameter is passed into the current page's //URL query string, then return the associated element's //outer HTML. If the element is not found in the page, //a null string is returned. If the "id" parameter is //non-existent, or is a null string, then all of the //HTML input is returned. function get_html_element_by_id(html = "", id = "") { var div = null; var html_elements = null; var element_count = null; var i = null; var element = null; var elem_id = null; var DOM_element = null; div = document.createElement("div"); div.innerHTML = html; html_elements = div.getElementsByTagName("*"); element_count = html_elements.length; if (id !== null && id !== undefined) { if (typeof id === "string" && id.length > 0) { for (i = 0; i < element_count; i++) { element = html_elements[i]; if (element.hasAttribute("id") === true) { elem_id = element.getAttribute("id").toString(); console.log(elem_id); if (elem_id === id) { DOM_element = element.outerHTML; break; } } } if (DOM_element === null) { DOM_element = ""; } } else if (id === "" && typeof id === "string") { DOM_element = html; } } else { DOM_element = html; } return DOM_element; } function unencode_text(text = "") { var textarea = null; textarea = document.createElement("textarea"); textarea.value = text; return textarea.value; }