/* JavaScript function to create passwordgrey toolbar in Dokuwiki */ /* see http://www.dokuwiki.org/plugin:passwordgrey for more info */ color_icobase = "../../plugins/passwordgrey/images/"; if (window.toolbar !== undefined) { toolbar[toolbar.length] = { "type": "format", "title": "Password", "icon": color_icobase + "lock_light_gray.png", "open": "", "close": "" }; } document.addEventListener("DOMContentLoaded", _ => { const labels = Array.from(document.querySelectorAll('label.passwordgrey')); const passwordLength = 8; for (let i = 0; i < labels.length; i++) { console.debug("adding"); const label = labels[i]; const span = label.querySelector('span.passwordgrey'); const password = span.textContent; const stars = "".padStart(passwordLength, "•"); span.style.visibility = "visible"; span.textContent = stars; span.title = LANG.plugins.passwordgrey.reveal; const showInputText = _ => { span.textContent = password; }; const hideInputText = _ => { span.textContent = stars; }; const copy = document.createElement('span'); copy.className = 'copy'; copy.textContent = '📋'; copy.title = LANG.plugins.passwordgrey.clipboard; copy.onclick = _ => { copyToClipboard(password); copy.classList.add('copied'); copy.textContent = '✓'; setTimeout(_ => { copy.classList.remove('copied'); copy.textContent = '📋'; }, 1500); return false; }; span.onmousedown = span.ontouchstart = showInputText; span.onmouseup = span.ontouchend = hideInputText; label.append(copy); } }); function copyToClipboard(text) { if (window.clipboardData && window.clipboardData.setData) { // Internet Explorer-specific code path to prevent textarea being shown while dialog is visible. return window.clipboardData.setData("Text", text); } else if (document.queryCommandSupported && document.queryCommandSupported("copy")) { var textarea = document.createElement("textarea"); textarea.textContent = text; textarea.style.position = "fixed"; // Prevent scrolling to bottom of page in Microsoft Edge. document.body.appendChild(textarea); textarea.select(); try { return document.execCommand("copy"); // Security exception may be thrown by some browsers. } catch (ex) { console.warn("Copy to clipboard failed.", ex); return prompt("Copy to clipboard: Ctrl+C, Enter", text); } finally { document.body.removeChild(textarea); } } }