JavaScript generic ASN.1 parser (mirror)
at github-104 1.6 kB view raw
1const 2 id = (elem) => document.getElementById(elem), 3 contextMenu = id('contextmenu'), 4 btnCopyHex = id('btnCopyHex'), 5 btnCopyB64 = id('btnCopyB64'), 6 btnCopyTree = id('btnCopyTree'), 7 btnCopyValue = id('btnCopyValue'); 8 9export function bindContextMenu(node) { 10 const type = node.asn1.typeName(); 11 const valueEnabled = type != 'SET' && type != 'SEQUENCE'; 12 node.onclick = function (event) { 13 // do not show the menu in case of clicking the icon 14 if (event.srcElement.nodeName != 'SPAN') return; 15 contextMenu.style.left = event.pageX + 'px'; 16 contextMenu.style.top = event.pageY + 'px'; 17 contextMenu.style.visibility = 'visible'; 18 contextMenu.node = this; 19 btnCopyValue.style.display = valueEnabled ? 'block' : 'none'; 20 event.preventDefault(); 21 event.stopPropagation(); 22 }; 23} 24 25function close(event) { 26 contextMenu.style.visibility = 'hidden'; 27 event.stopPropagation(); 28} 29 30contextMenu.onmouseleave = close; 31 32btnCopyHex.onclick = function (event) { 33 navigator.clipboard.writeText(contextMenu.node.asn1.toHexString('byte')); 34 close(event); 35}; 36 37btnCopyB64.onclick = function (event) { 38 event.stopPropagation(); 39 navigator.clipboard.writeText(contextMenu.node.asn1.toB64String()); 40 close(event); 41}; 42 43btnCopyTree.onclick = function (event) { 44 event.stopPropagation(); 45 navigator.clipboard.writeText(contextMenu.node.asn1.toPrettyString()); 46 close(event); 47}; 48 49btnCopyValue.onclick = function (event) { 50 event.stopPropagation(); 51 navigator.clipboard.writeText(contextMenu.node.asn1.content()); 52 close(event); 53};