Skip to content

Commit

Permalink
feat: add HTML logs to offscreen page
Browse files Browse the repository at this point in the history
  • Loading branch information
dawidsowardx committed Jan 25, 2024
1 parent cf30f9d commit 5b19ad7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
1 change: 1 addition & 0 deletions src/chrome/offscreen/index.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<!doctype html>
<html lang="en">
<body>
<div id="logs"></div>
<script type="module" src="/src/chrome/offscreen/offscreen.ts"></script>
</body>
</html>
24 changes: 14 additions & 10 deletions src/chrome/offscreen/logs-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ export type Log = ILogObjMeta & ILogObj
export type LogsClient = ReturnType<typeof LogsClient>

export const LogsClient = (
{ maxLogsLength } = {
{ maxLogsLength, htmlElementId } = {
htmlElementId: 'logs',
maxLogsLength: 1000,
},
) => {
let browser: string
const element = document.getElementById(htmlElementId)
const logs: string[] = []
return {
add: (logObj: Log) => {
Expand All @@ -24,15 +26,17 @@ export const LogsClient = (
.filter(Boolean)
.join('-')

logs.push(
`[${prefix}] ${Object.values(rest)
.map((singleLog) =>
typeof singleLog === 'object'
? JSON.stringify(singleLog, null, 2)
: singleLog,
)
.join(' ')}`,
)
const log = `[${prefix}] ${Object.values(rest)
.map((singleLog) =>
typeof singleLog === 'object'
? JSON.stringify(singleLog, null, 2)
: singleLog,
)
.join(' ')}`

logs.push(log)
element?.prepend(log)
element?.prepend(document.createElement('br'))

if (logs.length > maxLogsLength) {
logs.shift()
Expand Down

0 comments on commit 5b19ad7

Please sign in to comment.