(function () { var svgLink = "" + "" + "" + "" + "" + "" + ""; /** * Post data to file list */ function uploadFile(file, callback) { var now = new Date(); function two(x) { return x <= 9 ? '0' + x : x; } // file prefix - current timestamp var prefix = now.getFullYear() + "_" + two(now.getMonth()) + "_" + two(now.getDay()) + "_" + two(now.getHours()) + "_" + two(now.getMinutes()) + "_" + two(now.getSeconds()) + "_"; // FileList page url var fileListUrl = location.origin + "/Tools/Private/FileList.aspx"; fetch(fileListUrl).then(function (response) { var p = new DOMParser(); response.text().then(function (html) { var doc = p.parseFromString(html, "text/html"); var __EVENTTARGET = doc.querySelector("#__EVENTTARGET").getAttribute("value"); // console.log('__EVENTTARGET=', __EVENTTARGET); var __EVENTARGUMENT = doc.querySelector("#__EVENTARGUMENT").getAttribute("value"); // console.log('__EVENTARGUMENT=', __EVENTARGUMENT); var __VIEWSTATE = doc.querySelector("#__VIEWSTATE").getAttribute("value"); // console.log('__VIEWSTATE=', __VIEWSTATE); var __VIEWSTATEGENERATOR = doc.querySelector("#__VIEWSTATEGENERATOR").getAttribute("value"); // console.log('__VIEWSTATEGENERATOR=', __VIEWSTATEGENERATOR); var __EVENTVALIDATION = doc.querySelector("#__EVENTVALIDATION").getAttribute("value"); // console.log('__EVENTVALIDATION=', __EVENTVALIDATION); var formData = new FormData(); formData.append("__EVENTTARGET", __EVENTTARGET); formData.append("__EVENTARGUMENT", __EVENTARGUMENT); formData.append("__VIEWSTATE", __VIEWSTATE); formData.append("__VIEWSTATEGENERATOR", __VIEWSTATEGENERATOR); formData.append("__EVENTVALIDATION", __EVENTVALIDATION); formData.append("uploadedFile", file, prefix + file.name); formData.append('uploadButton', 'Загрузить'); fetch(fileListUrl, { method: "POST", body: formData }).then(function (response) { response.text().then(function (newHtml) { var newDoc = p.parseFromString(newHtml, "text/html"); var a = newDoc.querySelector("#errorText > a"); if (a) { callback(a.href); } else { var errorTextNode = newDoc.querySelector("#errorText"); console.warn("File uploaded but href for the uploaded file not found"); if (errorTextNode && errorTextNode.textContent) callback(errorTextNode.textContent); } }, function (err) { console.error('Cannot read upload file page content', err); }) }, function (err) { console.error('Failed to upload file (POST filaed):', fileListUrl, err); }) }, function (err) { console.error('Failed to read files page content:', err); }); }, function (err) { console.error('Failed to get files page (GET filaed):', fileListUrl, err); }) } /** * insert tag into text area */ function typeInTextarea(newText) { var el = document.querySelector("#msgEdit"); var start = el.selectionStart var end = el.selectionEnd var text = el.value var before = text.substring(0, start) var after = text.substring(end, text.length) el.value = (before + newText + after) el.selectionStart = el.selectionEnd = start + newText.length el.focus() } /** * build HTML browse toolbar button */ function buildButton(code) { var btn = document.createElement("button"); btn.className = "tb-btn"; btn.type = "button"; btn.style.fontFamily = "FontAwesome"; btn.style.fontStyle = "normal"; btn.style.fontWeight = "normal"; btn.style.fontVariant = "normal"; btn.style.lineHeight = 1; btn.innerHTML = ''; var existing = document.querySelector("button.tb-btn.img-btn"); existing.parentElement.insertBefore(btn, existing.nextSibling); return btn; } /** * upload file and update edit box */ function insertFile(file) { uploadFile(file, function (url) { var text = "[img]" + url + "[/img]"; typeInTextarea(text); }); } /** * upload toolbar button */ function addToolbarButton() { var input = document.createElement('input'); input.type = 'file'; input.onchange = function (e) { var file = e.target.files[0]; insertFile(file); } var btn = buildButton(); btn.onclick = function () { input.click(); } } /** * paste handler */ function addPasteHandler() { document.onpaste = function (event) { var items = event.clipboardData.items; for (var i = 0; i < items.length; ++i) { var item = items[i]; if (item.kind === 'file') { var file = items[i].getAsFile(); insertFile(file); } } } } function fallbackCopyTextToClipboard(text) { var textArea = document.createElement("textarea"); textArea.value = text; // Avoid scrolling to bottom textArea.style.top = "0"; textArea.style.left = "0"; textArea.style.position = "fixed"; document.body.appendChild(textArea); textArea.focus(); textArea.select(); try { var successful = document.execCommand('copy'); var msg = successful ? 'successful' : 'unsuccessful'; // console.log('Fallback: Copying text command was ' + msg); } catch (err) { console.error('Fallback: Oops, unable to copy', err); } document.body.removeChild(textArea); } function copyTextToClipboard(text) { if (!navigator.clipboard) { fallbackCopyTextToClipboard(text); return; } navigator.clipboard.writeText(text).then(function() { // console.log('Async: Copying to clipboard was successful!'); }, function(err) { console.error('Async: Could not copy text: ', err); }); } /** * copy link handler */ function buildLinkButton(href) { var a = document.createElement("a"); a.innerHTML = svgLink; a.title = "Скопировать ссылку"; a.href = href; a.style.cursor = "pointer"; a.style.border = "none"; a.style.display = "inline-block"; a.style.position = "relative"; var tip = document.createElement("div"); tip.textContent = "Скопировано: " + href; tip.style.position = "absolute"; tip.style.right = "0"; tip.style.top = "18px"; tip.style.color = "black"; tip.style.backgroundColor = "lightYellow"; tip.style.padding = "10px"; tip.style.display = "none"; tip.style.fontFamily = "Verdana,Geneva,sans-serif"; a.appendChild(tip); a.onclick = function (evt) { if (!evt.ctrlKey && !evt.shiftKey) { evt.preventDefault(); copyTextToClipboard(href); tip.style.display = "block"; setTimeout(function () { tip.style.display = "none"; }, 3000); return false; } } return a; } function addCopyLinkHandler() { var messages = document.querySelectorAll(".msg-hdr"); for (var i = 0; i < messages.length; ++i) { var message = messages[i]; var tb = message.querySelector(".right-tb"); if (tb) { var showBtn = tb.querySelector(".show-in-topic-btn") || tb.querySelector(".show-all-btn"); if (showBtn) { var href = showBtn.href.split(/\?|\.flat/)[0] + ".1"; var a = buildLinkButton(href); tb.appendChild(a); } } } } document.addEventListener('DOMContentLoaded', function () { // only on new message page if (location.href.indexOf("/Forum/NewMsg.aspx") >= 0) { addToolbarButton(); addPasteHandler(); } addCopyLinkHandler(); }); })();