diff --git a/webinterface-upload-button.js b/webinterface-upload-button.js index 8756e2e..2efed77 100644 --- a/webinterface-upload-button.js +++ b/webinterface-upload-button.js @@ -1,18 +1,13 @@ // https://github.com/rM-self-serve/webinterface-upload-button -async function wait4header() { - for (let i=0; i<20; i++) { - const hgroups = document.getElementsByClassName('header-group'); - if (hgroups.length > 1) { - return hgroups; - } - await new Promise(r => setTimeout(r, 100)) - } - return null; -} -async function init() { - var css = `.header-button-ovrd { +(function() { + 'use strict'; + +let global_is_uploading = false; + +function init() { + var css = `.header-button-ovrd { background-color: transparent; };`; var style = document.createElement('style'); @@ -22,8 +17,8 @@ async function init() { style.appendChild(document.createTextNode(css)); } document.getElementsByTagName('head')[0].appendChild(style); - - const hgroups = await wait4header(); + + const hgroups = document.getElementsByClassName('header-group'); const my_files_box = hgroups[0]; my_files_box.style.flex = "10 1 auto"; @@ -33,25 +28,31 @@ async function init() { let upbtn_node = document.createElement("a"); upbtn_node.role = "button"; upbtn_node.id = "upbtn"; - upbtn_node.className = hbtn_elm.classList[0]; - upbtn_node.appendChild(hbtn_elm.firstChild.cloneNode(true)); + upbtn_node.className = hbtn_elm ? hbtn_elm.classList[0] : "header-button"; + if (hbtn_elm) { + upbtn_node.appendChild(hbtn_elm.firstChild.cloneNode(true)); + } else { + const i = document.createElement("i"); + i.className = "icon-rm_ikoner_final_action_download-98"; + const div = document.createElement("div"); + div.appendChild(i); + upbtn_node.appendChild(div); + } upbtn_node.classList.add("header-button-ovrd") upbtn_node.style.borderWidth = "0px 0px 0px 1px"; let icon_elm = upbtn_node.firstChild.firstChild; icon_elm.style.transform = "rotate(180deg)"; - btns_elm.insertBefore(upbtn_node, hbtn_elm); upbtn_node.addEventListener("click", handleSubmit); } -let global_is_uploading = false; - async function handleSubmit(_) { if (global_is_uploading) { return; } + global_is_uploading = true; let input = document.createElement('input'); input.type = 'file'; input.multiple = true; @@ -61,7 +62,6 @@ async function handleSubmit(_) { } async function input_auto_submit(input) { - global_is_uploading = true; let list_elm = document.getElementsByClassName('list')[0]; let list_ref = list_elm.removeChild(list_elm.childNodes[1]); let loaderNode = document.createElement("div"); @@ -151,5 +151,13 @@ function create_list_entry(file_name) { return outer_div; } +const callback = _ => { + if (document.getElementsByClassName("header-group").length >= 2) { + observer.disconnect(); + init(); + } +}; -init() +const observer = new MutationObserver(callback); +observer.observe(document.body, {childList: true, subtree: true}); +})();