Skip to content

Commit

Permalink
Context menu and copyAsString added
Browse files Browse the repository at this point in the history
  • Loading branch information
olibu committed Jan 28, 2024
1 parent 64546e4 commit 151289b
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 4 deletions.
9 changes: 5 additions & 4 deletions dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,11 @@ class ASN1DOM extends ASN1 {
};
// handler to copy the complete hex dump into the clipboard
node.onclick = function (event) {
const pos = parseInt(this.getAttribute('pos'));
const end = parseInt(this.getAttribute('end'));
const hex = this.asn1.buf2hex(window.derBuffer.subarray(pos, end));
navigator.clipboard.writeText(hex);
let contextMenu = document.getElementById('contextmenu');
contextMenu.style.left = event.clientX + "px";
contextMenu.style.top = event.clientY + "px";
contextMenu.style.visibility = 'visible';
document.getElementById('contextmenu').node = this;
event.stopPropagation();
};
if (root == node) {
Expand Down
20 changes: 20 additions & 0 deletions index.css
Original file line number Diff line number Diff line change
Expand Up @@ -212,3 +212,23 @@ header {
#dump .hexCurrent .dlen { color: #004040; }
#file { display: none; }
#area { width: 100%; }

#contextmenu {
position: absolute;
top: 0;
left: 0;
padding: 2px;
background-color: var(--button-bg-color);
border: 1px solid var(--button-bg-color);
z-index: 2;
}

#contextmenu > button {
width: 120px;
background-color: var(--button-bg-color);
color: var(--main-text-color);
border: 1px solid var(--button-border-color);
}
#contextmenu > button:hover {
background-color: var(--button-bghover-color);
}
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<link rel="icon" type="image/svg+xml" sizes="192x192" href="favicon.svg">
</head>
<body>
<div id="contextmenu" onmouseleave="this.style.visibility = 'hidden';"><button id="btnCopyHex">Copy as HEX</button><br><button id="btnCopyString">Copy as String</button></div>
<header>
<div class="title">
<h1>ASN.1 JavaScript decoder</h1>
Expand Down
25 changes: 25 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
'use strict';

const
ASN1 = require('./asn1'),
ASN1DOM = require('./dom'),
Base64 = require('./base64'),
Hex = require('./hex'),
Expand Down Expand Up @@ -239,4 +240,28 @@ selectTag.onchange = function (ev) {
window.location.href = 'https://rawcdn.githack.com/lapo-luchini/asn1js/' + tag + '/index.html';
};

// register context menu function
document.getElementById('btnCopyHex').onclick = function (event) {
let contextMenu = document.getElementById('contextmenu');
let node = contextMenu.node;
const pos = parseInt(node.getAttribute('pos'));
const end = parseInt(node.getAttribute('end'));
const hex = node.asn1.buf2hex(window.derBuffer.subarray(pos, end));
navigator.clipboard.writeText(hex);
contextMenu.style.visibility = 'hidden';
event.stopPropagation();
};

document.getElementById('btnCopyString').onclick = function (event) {
let contextMenu = document.getElementById('contextmenu');
let node = contextMenu.node;
const pos = parseInt(node.getAttribute('pos'));
const end = parseInt(node.getAttribute('end'));
// const hex = node.asn1.buf2hex(window.derBuffer.subarray(pos, end));
let result = ASN1.decode(window.derBuffer.subarray(pos, end));
// navigator.clipboard.writeText(hex);
contextMenu.style.visibility = 'hidden';
event.stopPropagation();
};

});

0 comments on commit 151289b

Please sign in to comment.