Skip to content

Instantly share code, notes, and snippets.

@JavaScriptDude
Last active October 22, 2022 23:12
Show Gist options
  • Save JavaScriptDude/2a599a9ea3af1ff9477b4285d8051cfb to your computer and use it in GitHub Desktop.
Save JavaScriptDude/2a599a9ea3af1ff9477b4285d8051cfb to your computer and use it in GitHub Desktop.
Debugging JavaScript for WkHtmlToX

WkHtmlToX projects uses WebKit as a backend to virutally render a web page and execute its JavaScript to get the final page for printing.

The issue is that the version of WebKit used is very old and this makes debugging JS regressions difficult. WkHtmlToX does offer a way to output the console, but it gives no way to do active debugging and provides no line numbers!

To debug JavaScript using the same WebKit version, I tracked down the version of Chromium that uses a close enough webkit version that this library leverages (534.34). Chromium 13.0.767.1, which is available here as a standalone exe for windows.

If your page does not already have a handler for window.onerror, add one with the following code in your page boot code.

// Detect old safari version used by QT and Chromium 13.0.767.1
if (naviagtor.userAgent.indexOf("Safari/534.3") > -1) {
  window.onerror = function _window_onerr(msg, url, line){
    console.error("Error Occurred: " + String(msg) + ", url: " + String(url) + ", line: " + String(line))
  }
}

When you test in Chromium, you will get the line number that is failing.

I have no idea why the QT WebKit used by this library is loosing the line number but I suspect is a compilation setting somewhere in the stack.

Note: This description is for the version of WkHtmlToPdf that has a user agent containing 'Safari/534.3'. Please let me know if a newer version os created and I can dig up the respective version of Chromium.

Hope this helps.

@JavaScriptDude
Copy link
Author

Date of ChromiumPortable 13.0.767.1 is 2011-05-18

@JavaScriptDude
Copy link
Author

Note: there is no way to do this on *nix platforms as there is no known standalone version of Chromium available for linux that I can find. If someone has a solution please provide. The example above is for Windohs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment