Skip to content

Commit

Permalink
fix: Avoid to have deformed webview warning logs
Browse files Browse the repository at this point in the history
when a webview emits a warning log, we get a log which looks like this :

```
 LOG  CozyWebView [Console HomeView] w a r n :   % c c o z y - s t a c k
 - c l i e n t   D e p r e c a t e d :   i n   n e x t   v e r s i o n s
 o f   c o z y - c l i e n t ,   i t   w i l l   n o t   b e   p o s s i
 b l e   t o   q u e r y   s e t t i n g s   w i t h   a n   i n c o m p
 l e t e   i d

   -   Q ( ' i o . c o z y . s e t t i n g s ' ) . g e t B y I d ( ' i n
   s t a n c e ' )
    +   Q ( ' i o . c o z y . s e t t i n g s ' ) . g e t B y I d ( ' i
    o . c o z y . s e t t i n g s . i n s t a n c e ' ) , c o l o r :
    # f f f ;   b a c k g r o u n d :   # b b b b 0 0 ;

```

This fix allows to get a normal display of the logs
  • Loading branch information
doubleface authored and doubleface committed Dec 8, 2023
1 parent c56c77a commit 790e90c
Showing 1 changed file with 53 additions and 53 deletions.
106 changes: 53 additions & 53 deletions src/components/webviews/jsInteractions/jsLogInterception.ts
Original file line number Diff line number Diff line change
@@ -1,53 +1,53 @@
/* eslint-disable no-console */
export const jsLogInterception = `
const originalJsConsole = console
const consoleLog = (type, log) => {
originalJsConsole[type](...log)
try {
window.ReactNativeWebView.postMessage(
JSON.stringify({
type: 'Console',
data: {type, log},
}),
)
} catch {}
}
console = {
log: (...log) => consoleLog('log', log),
debug: (...log) => consoleLog('debug', log),
info: (...log) => consoleLog('info', log),
warn: (...log) => consoleLog('log', 'warn: ' + log),
error: (...log) => consoleLog('error', log),
}
`

export const tryConsole = (
payload: { nativeEvent: { data: string } } | undefined,
logger: MiniLogger | undefined,
logId: string | undefined
): void => {
try {
if (!payload || !logger || !logId) return console.error('no payload')

const { data: rawData } = payload.nativeEvent

const dataPayload = JSON.parse(rawData) as {
type: string
data?: { type: string; log: string[] }
}

if (!dataPayload.data || dataPayload.type !== 'Console') return

const { type, log } = dataPayload.data

if (rawData.includes('@post-me')) return console.debug(...log)

if (typeof logger[type as keyof MiniLogger] === 'function') {
logger[type as keyof MiniLogger](`[Console ${logId}]`, ...log)
}
} catch (e) {
console.error('error', e)
}
}
/* eslint-disable no-console */
export const jsLogInterception = `
const originalJsConsole = console
const consoleLog = (type, log) => {
originalJsConsole[type](...log)
try {
window.ReactNativeWebView.postMessage(
JSON.stringify({
type: 'Console',
data: {type, log},
}),
)
} catch {}
}
console = {
log: (...log) => consoleLog('log', log),
debug: (...log) => consoleLog('debug', log),
info: (...log) => consoleLog('info', log),
warn: (...log) => consoleLog('log', ['warn:', ...log]),
error: (...log) => consoleLog('error', log),
}
`

export const tryConsole = (
payload: { nativeEvent: { data: string } } | undefined,
logger: MiniLogger | undefined,
logId: string | undefined
): void => {
try {
if (!payload || !logger || !logId) return console.error('no payload')

const { data: rawData } = payload.nativeEvent

const dataPayload = JSON.parse(rawData) as {
type: string
data?: { type: string; log: string[] }
}

if (!dataPayload.data || dataPayload.type !== 'Console') return

const { type, log } = dataPayload.data

if (rawData.includes('@post-me')) return console.debug(...log)

if (typeof logger[type as keyof MiniLogger] === 'function') {
logger[type as keyof MiniLogger](`[Console ${logId}]`, ...log)
}
} catch (e) {
console.error('error', e)
}
}

0 comments on commit 790e90c

Please sign in to comment.