I'm actually trying to make a source code viewer for website based on AJAX and popups windows. This will allowing to me to customize the viewer. I'm using hightlight.js for syntaxing, but it's the first time I use that libray.
The process is simple:
- I'm getting the page HTML with its URL using AJAX (here I'm using jQuery)
- I put the resulted content in a window and show it.
- On the window, Highlight.js is (supposed) to get the code colorized
Similar to this topic: Programmatically open "View Source" HTML Window in Browser with Javascript? but with syntax highlighting in addition. Unfortunely the window is shown with the code inside, but that one is not colorized.
This is the entire function (it provides a fallback for cross-origin requests)
function show_source(link) {
$.ajax({
method: "GET",
url: link,
dataType: "html",
success: function (data) {
var source = data;
//parse special chars
source = source.replace(/</g, "<").replace(/>/g, ">");
// add <pre> tags to preserve whitespace
source = "<!DOCTYPE html><html><head><link rel='stylesheet' href='/css/include/default.css'><script src='/lib/include/highlight.pack.js'></script><script>hljs.initHighlightingOnLoad(document.getElementById('code'));</script></head><body><pre id='code' class='html'>" + source + "</pre></body></html>";
//now open the window and set the source as the content
var sourceWindow = window.open('', 'Source code of ' + link + '', 'height=800,width=800,scrollbars=1,resizable=1');
if (!sourceWindow || sourceWindow.closed || typeof sourceWindow.closed == 'undefined') {
alert("Please allows this popup window to be shown");
} else {
sourceWindow.document.write(source);
sourceWindow.document.close(); //close the document for writing, not the window
//give source window focus
if (window.focus) {
sourceWindow.focus();
}
}
},
error: function(){
window.open("view-source:"+link+"", "_blank");
}
});
}
But I verified my code many times and everything seems fine. Firefox dev tools indicated that CSS and JS file are loaded successfully (GET) and no JS errors. Where is the problem? Thanks to help me.
EDIT: added a missing parenthesis but not working much.