Repository: microsoft/playwright
File: packages/playwright/src/mcp/browser/tab.ts (TypeScript)

Code before:
  private _resetConsoleLogFile() {
    if (this.context.config.outputMode !== 'file')
      return;
    this._consoleLog = new LogFile(Date.now(), this.context);
    this._lastReportedConsoleLogState = undefined;
  }

  private _handleRequest(request: playwright.Request) {
    this._requests.add(request);
    this._addLogEntry({ type: 'request', wallTime: Date.now(), request });
  }

  private _handleConsoleMessage(message: ConsoleMessage) {
    this._consoleMessages.push(message);
    const wallTime = Date.now();
    this._addLogEntry({ type: 'console', wallTime, message });
    this._writeConsoleEntryToFile(wallTime, message);
  }

  private _writeConsoleEntryToFile(wallTime: number, message: ConsoleMessage) {
    if (!this._consoleLog)
      return;

    const relativeTime = wallTime - this._consoleLog.startTime;
    const logLine = `[${String(relativeTime).padStart(8, ' ')}ms] [${message.type.toUpperCase()}] ${message.toString()}`;
    this._consoleLog.appendLine(logLine);
  }

  private _addLogEntry(entry: EventEntry) {
    this._recentEventEntries.push(entry);
  }

  private _onClose() {
    this._clearCollectedArtifacts();
    this._onPageClose(this);
  }

  async headerSnapshot(): Promise<TabHeader & { changed: boolean }> {
    let title: string | undefined;
    await this._raceAgainstModalStates(async () => {
      title = await callOnPageNoTrace(this.page, page => page.title());
    });
    if (this._lastHeader.title !== title || this._lastHeader.url !== this.page.url() || this._lastHeader.current !== this.isCurrentTab()) {
      this._lastHeader = { title: title ?? '', url: this.page.url(), current: this.isCurrentTab() };
      return { ...this._lastHeader, changed: true };
    }
    return { ...this._lastHeader, changed: false };
  }


  isCurrentTab(): boolean {

Review comment (suggestion):
Let's compute text here and include it into the log entry. That way we can implement it for both network and console.

Code after:
  }

  private _clearCollectedArtifacts() {
    this._consoleMessages.length = 0;
    this._downloads.length = 0;
    this._requests.clear();
    this._recentEventEntries.length = 0;
    this._resetConsoleLogFile();
  }

  private _resetConsoleLogFile() {
    if (this.context.config.outputMode !== 'file')
      return;
    this._consoleLog?.stop();
    this._consoleLog = new LogFile(this.context, 'console', 'Console');
  }

  private _handleRequest(request: playwright.Request) {
    this._requests.add(request);
    this._addLogEntry({ type: 'request', wallTime: Date.now(), request });
  }

  private _handleConsoleMessage(message: ConsoleMessage) {
    this._consoleMessages.push(message);
    const wallTime = Date.now();
    this._addLogEntry({ type: 'console', wallTime, message });
    this._consoleLog?.appendLine(wallTime, message.toString());
  }

  private _addLogEntry(entry: EventEntry) {
    this._recentEventEntries.push(entry);
  }

  private _onClose() {
    this._clearCollectedArtifacts();
    this._onPageClose(this);
  }

  async headerSnapshot(): Promise<TabHeader & { changed: boolean }> {
    let title: string | undefined;
    await this._raceAgainstModalStates(async () => {
      title = await callOnPageNoTrace(this.page, page => page.title());
    });
    if (this._lastHeader.title !== title || this._lastHeader.url !== this.page.url() || this._lastHeader.current !== this.isCurrentTab()) {
      this._lastHeader = { title: title ?? '', url: this.page.url(), current: this.isCurrentTab() };
      return { ...this._lastHeader, changed: true };
    }
    return { ...this._lastHeader, changed: false };
  }