MIXERRULES
MIXERRULES•8mo ago

Change color of console.logs from a worker

Hello, I'm trying to work with the NPM Package tmi.js and i tried converting it over to typescript and Deno APIs but the script is just too complicated and relies on too many NPM packages, so im wondering if theres a way just to change all of the colors of console.log() without doing so in the code of the module since im running it as a worker
npm
tmi.js
Javascript library for the Twitch Messaging Interface.. Latest version: 1.8.5, last published: 3 years ago. Start using tmi.js in your project by running npm i tmi.js. There are 90 other projects in the npm registry using tmi.js.
2 Replies
Deno AI Helper
Deno AI Helper•8mo ago
Changing the color of console.log() output from a worker without modifying the code of the module can be a bit tricky, as the styling of console.log() typically happens inline with the log message itself. However, based on the documentation, Deno supports CSS styling for console.log() messages using the %c directive, which allows you to apply CSS styles to your log output. Here's an example from the documentation on how to use CSS styling in console.log():
console.log("%cHello World", "color: red; font-weight: bold");
console.log("%cHello World", "color: red; font-weight: bold");
In the case of a worker, since you cannot change the module's code, you would not be able to directly insert the %c directive into the existing console.log() calls within the module. One potential workaround, although not directly supported by the documentation, would be to intercept the calls to console.log() within the worker and apply the styling there. This would involve overriding the console.log() function in the worker's global scope before importing the module. Here's a general idea of how you might do that:
// Inside your worker script
const originalConsoleLog = console.log;

console.log = (...args) => {
// Apply your styling here
const styledArgs = args.map(arg => `%c${arg}`, "color: red");
originalConsoleLog(...styledArgs);
};

// Now, when the module calls console.log, it will use your styled version.
// Inside your worker script
const originalConsoleLog = console.log;

console.log = (...args) => {
// Apply your styling here
const styledArgs = args.map(arg => `%c${arg}`, "color: red");
originalConsoleLog(...styledArgs);
};

// Now, when the module calls console.log, it will use your styled version.
Please note that this is a general advice and not directly from the documentation. This approach may have limitations and might not work as expected if the module uses different logging mechanisms or if it relies on specific formatting of the log messages. If you're looking for a more robust solution, you might need to consider forking the module and modifying the console.log()
Deno AI Helper
Deno AI Helper•8mo ago
calls directly, or looking for a module that is more compatible with Deno's logging capabilities. 🦕