Created
April 12, 2023 14:28
-
-
Save jerrylususu/310619613d45a7dc53485ffac1ef2267 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
<title>Parse Protobuf File in Frontend Example</title> | |
</head> | |
<body> | |
<textarea id="protobuf-content" rows="10" cols="80"></textarea> | |
<button id="parse-button">Parse</button> | |
<pre id="pb-json"></pre> | |
<script src="https://cdn.jsdelivr.net/npm/protobufjs/dist/protobuf.js"></script> | |
<script> | |
document.getElementById("parse-button").addEventListener("click", function() { | |
var data = document.getElementById("protobuf-content").value; | |
console.log(data); | |
var root = new protobuf.Root(); | |
try { | |
protobuf.parse(data, root, { keepCase: true, alternateCommentMode: false, preferTrailingComment: false }); | |
root.resolveAll(); | |
} catch (err) { | |
console.error(err); | |
return; | |
} | |
var pbjson = document.getElementById("pb-json"); | |
pbjson.innerHTML = JSON.stringify(root.toJSON(), null, 2); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment