JavaScript generic ASN.1 parser (mirror)
at github-90 1.9 kB view raw
1const 2 id = (elem) => document.getElementById(elem), 3 contextMenu = id('contextmenu'), 4 btnHideTree = id('btnHideTree'), 5 btnCopyHex = id('btnCopyHex'), 6 btnCopyB64 = id('btnCopyB64'), 7 btnCopyTree = id('btnCopyTree'), 8 btnCopyValue = id('btnCopyValue'); 9 10export function bindContextMenu(node) { 11 const type = node.asn1.typeName(); 12 const valueEnabled = type != 'SET' && type != 'SEQUENCE'; 13 node.onclick = function (event) { 14 contextMenu.style.left = event.pageX + 'px'; 15 contextMenu.style.top = event.pageY + 'px'; 16 contextMenu.style.visibility = 'visible'; 17 contextMenu.node = this; 18 btnHideTree.innerText = (node.className == 'node') ? 'Hide subtree' : 'Show subtree'; 19 btnHideTree.style.display = node.className.startsWith('node') ? 'block' : 'none'; 20 btnCopyValue.style.display = valueEnabled ? 'block' : 'none'; 21 event.stopPropagation(); 22 }; 23} 24 25function close() { 26 contextMenu.style.visibility = 'hidden'; 27} 28 29contextMenu.onmouseleave = close; 30 31btnHideTree.onclick = function (event) { 32 event.stopPropagation(); 33 const node = contextMenu.node; 34 node.className = (node.className == 'node collapsed') ? 'node' : 'node collapsed'; 35 close(); 36}; 37 38btnCopyHex.onclick = function (event) { 39 event.stopPropagation(); 40 navigator.clipboard.writeText(contextMenu.node.asn1.toHexString('byte')); 41 close(); 42}; 43 44btnCopyB64.onclick = function (event) { 45 event.stopPropagation(); 46 navigator.clipboard.writeText(contextMenu.node.asn1.toB64String()); 47 close(); 48}; 49 50btnCopyTree.onclick = function (event) { 51 event.stopPropagation(); 52 navigator.clipboard.writeText(contextMenu.node.asn1.toPrettyString()); 53 close(); 54}; 55 56btnCopyValue.onclick = function (event) { 57 event.stopPropagation(); 58 navigator.clipboard.writeText(contextMenu.node.asn1.content()); 59 close(); 60};