No need to lean on jQuery. The best and simplest approach is to use
new XMLSerializer().serializeToString(document)
which will always give you the contents of the entire page including DOCTYPE tag, and it is supported in all modern browsers: https://caniuse.com/#feat=xml-serializer