Created
November 23, 2023 11:28
-
-
Save vindolin/18f2a393350b4bdf7a501d570554f88b to your computer and use it in GitHub Desktop.
colorize_ansi.js
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
// ==UserScript== | |
// @name Colorize Ansi Codes | |
// @namespace http://tampermonkey.net/ | |
// @version 0.1 | |
// @description Colorize the ANSI codes in a HTML element. | |
// @author You | |
// @match http://pi4:9001/tail.html* | |
// @icon https://www.google.com/s2/favicons?sz=64&domain=githubusercontent.com | |
// @grant none | |
// @require https://gist.githubusercontent.com/vindolin/4a5dc01877b9531da39b30646dd882a3/raw/164473bdec4b3b17fd6455a4741f5d1a4e31c5b6/ansicolor.js | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
// change this selector to match your element which contains the ANSI text | |
const subject = document.getElementsByTagName('pre')[0]; | |
colorizeAnsi(subject); | |
function colorizeAnsi(element) { | |
const text = element.textContent.trim(); | |
// console.log(text); | |
const lines = text.match(/[^\n]+/g); | |
element.innerHTML = ''; | |
for(const line of lines) { | |
const spans = Colors.parse(line).spans; | |
for(const span of spans) { | |
const spanElement = document.createElement('span'); | |
spanElement.style.cssText = span.css; | |
spanElement.textContent = span.text; | |
element.appendChild(spanElement); | |
} | |
element.appendChild(document.createElement('br')); | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment