ConsoleMessage.ts (2211B)
1 /** 2 * @license 3 * Copyright 2020 Google Inc. 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 import type {Frame} from '../api/Frame.js'; 8 import type {JSHandle} from '../api/JSHandle.js'; 9 10 /** 11 * @public 12 */ 13 export interface ConsoleMessageLocation { 14 /** 15 * URL of the resource if known or `undefined` otherwise. 16 */ 17 url?: string; 18 19 /** 20 * 0-based line number in the resource if known or `undefined` otherwise. 21 */ 22 lineNumber?: number; 23 24 /** 25 * 0-based column number in the resource if known or `undefined` otherwise. 26 */ 27 columnNumber?: number; 28 } 29 30 /** 31 * The supported types for console messages. 32 * @public 33 */ 34 export type ConsoleMessageType = 35 | 'log' 36 | 'debug' 37 | 'info' 38 | 'error' 39 | 'warn' 40 | 'dir' 41 | 'dirxml' 42 | 'table' 43 | 'trace' 44 | 'clear' 45 | 'startGroup' 46 | 'startGroupCollapsed' 47 | 'endGroup' 48 | 'assert' 49 | 'profile' 50 | 'profileEnd' 51 | 'count' 52 | 'timeEnd' 53 | 'verbose'; 54 55 /** 56 * ConsoleMessage objects are dispatched by page via the 'console' event. 57 * @public 58 */ 59 export class ConsoleMessage { 60 #type: ConsoleMessageType; 61 #text: string; 62 #args: JSHandle[]; 63 #stackTraceLocations: ConsoleMessageLocation[]; 64 #frame?: Frame; 65 66 /** 67 * @internal 68 */ 69 constructor( 70 type: ConsoleMessageType, 71 text: string, 72 args: JSHandle[], 73 stackTraceLocations: ConsoleMessageLocation[], 74 frame?: Frame, 75 ) { 76 this.#type = type; 77 this.#text = text; 78 this.#args = args; 79 this.#stackTraceLocations = stackTraceLocations; 80 this.#frame = frame; 81 } 82 83 /** 84 * The type of the console message. 85 */ 86 type(): ConsoleMessageType { 87 return this.#type; 88 } 89 90 /** 91 * The text of the console message. 92 */ 93 text(): string { 94 return this.#text; 95 } 96 97 /** 98 * An array of arguments passed to the console. 99 */ 100 args(): JSHandle[] { 101 return this.#args; 102 } 103 104 /** 105 * The location of the console message. 106 */ 107 location(): ConsoleMessageLocation { 108 return ( 109 this.#stackTraceLocations[0] ?? 110 (this.#frame ? {url: this.#frame.url()} : {}) 111 ); 112 } 113 114 /** 115 * The array of locations on the stack of the console message. 116 */ 117 stackTrace(): ConsoleMessageLocation[] { 118 return this.#stackTraceLocations; 119 } 120 }