Skip to content

Ensure console.log (console.error) calls with non-string arguments get serialized in kiosk logs #335

@yfyf

Description

@yfyf

Currently, if the page calls console.error("foo", obj1, obj2), you will see useless logs in the console, like:

js: Service Worker registration failed: [object DOMException] [object Object]

Gemini suggests injecting a custom console.error handler into pages, not sure if that's a good idea, but preserving it here:

from PyQt6.QtWebEngineCore import QWebEngineScript

js_code = r"""
  (function() {
      const originalError = console.error;
      console.error = function(...args) {
          const parsedArgs = args.map(arg => {
              if (typeof arg === 'object' && arg !== null) {
                  if (arg instanceof Error || typeof DOMException !== 'undefined' && arg instanceof DOMException) {
                      return `${arg.name || 'Error'}: ${arg.message}\n${arg.stack || ''}`;
                  }
                  try {
                      return JSON.stringify(arg, null, 2);
                  } catch (e) {
                      return String(arg);
                  }
              }
              return arg;
          });
          originalError.apply(console, parsedArgs);
      };
  })();
  """
  
  script = QWebEngineScript()
  script.setName("ConsoleSerializer")
  script.setSourceCode(js_code)
  
  # Set injection parameters
  script.setInjectionPoint(QWebEngineScript.InjectionPoint.DocumentCreation)
  script.setWorldId(QWebEngineScript.ScriptWorldId.MainWorld)
  script.setRunsOnSubFrames(True)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions