1/* JavaScript function to create passwordgrey toolbar in Dokuwiki */ 2/* see http://www.dokuwiki.org/plugin:passwordgrey for more info */ 3 4color_icobase = "../../plugins/passwordgrey/images/"; 5 6if (window.toolbar !== undefined) { 7 toolbar[toolbar.length] = { 8 "type": "format", 9 "title": "Password", 10 "icon": color_icobase + "lock_light_gray.png", 11 "open": "<pwd>", 12 "close": "</pwd>" 13 }; 14} 15document.addEventListener("DOMContentLoaded", _ => { 16 const labels = Array.from(document.querySelectorAll('label.passwordgrey')); 17 const passwordLength = 8; 18 19 for (let i = 0; i < labels.length; i++) { 20 console.debug("adding"); 21 const label = labels[i]; 22 const span = label.querySelector('span.passwordgrey'); 23 const password = span.textContent; 24 const stars = "".padStart(passwordLength, "•"); 25 span.style.visibility = "visible"; 26 span.textContent = stars; 27 span.title = LANG.plugins.passwordgrey.reveal; 28 const showInputText = _ => { 29 span.textContent = password; 30 }; 31 const hideInputText = _ => { 32 span.textContent = stars; 33 }; 34 35 const copy = document.createElement('span'); 36 copy.className = 'copy'; 37 copy.textContent = ''; 38 copy.title = LANG.plugins.passwordgrey.clipboard; 39 copy.onclick = _ => { 40 copyToClipboard(password); 41 copy.classList.add('copied'); 42 copy.textContent = '✓'; 43 setTimeout(_ => { 44 copy.classList.remove('copied'); 45 copy.textContent = ''; 46 }, 1500); 47 return false; 48 }; 49 span.onmousedown = span.ontouchstart = showInputText; 50 span.onmouseup = span.ontouchend = hideInputText; 51 label.append(copy); 52 } 53}); 54 55function copyToClipboard(text) { 56 if (window.clipboardData && window.clipboardData.setData) { 57 // Internet Explorer-specific code path to prevent textarea being shown while dialog is visible. 58 return window.clipboardData.setData("Text", text); 59 } else if (document.queryCommandSupported && document.queryCommandSupported("copy")) { 60 var textarea = document.createElement("textarea"); 61 textarea.textContent = text; 62 textarea.style.position = "fixed"; // Prevent scrolling to bottom of page in Microsoft Edge. 63 document.body.appendChild(textarea); 64 textarea.select(); 65 try { 66 return document.execCommand("copy"); // Security exception may be thrown by some browsers. 67 } catch (ex) { 68 console.warn("Copy to clipboard failed.", ex); 69 return prompt("Copy to clipboard: Ctrl+C, Enter", text); 70 } finally { 71 document.body.removeChild(textarea); 72 } 73 } 74}