File size: 7,407 Bytes
eb67da4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 |
(function(document) {
var interval,
defaultReloadFreq = 3,
previousText,
storage = chrome.storage.local;
function getExtension(url) {
url = url.substr(1 + url.lastIndexOf("/"))
.split('?')[0]
.split('#')[0];
return url.substr(1 + url.lastIndexOf("."))
}
function resolveImg(img) {
var src = $(img).attr("src");
if (src[0] == "/") {
$(img).attr("src", src.substring(1));
}
}
// Onload, take the DOM of the page, get the markdown formatted text out and
// apply the converter.
function makeHtml(data) {
storage.get('mathjax', function(items) {
// Convert MarkDown to HTML without MathJax typesetting.
// This is done to make page responsiveness. The HTML body
// is replaced after MathJax typesetting.
// BUG: CWE-79 Improper Neutralization of Input During Web Page Generation ('Cross-site Scripting')
//
// FIXED:
config.markedOptions.sanitize = items.mathjax ? false : true;
marked.setOptions(config.markedOptions);
var html = marked(data);
$(document.body).html(html);
$('img').on("error", function() {
resolveImg(this);
});
// Apply MathJax typesetting
if (items.mathjax) {
$.getScript(chrome.extension.getURL('js/marked.js'));
$.getScript(chrome.extension.getURL('js/highlight.js'), function() {
$.getScript(chrome.extension.getURL('js/config.js'));
});
// Create hidden div to use for MathJax processing
var mathjaxDiv = $("<div/>").attr("id", config.mathjaxProcessingElementId)
.text(data)
.hide();
$(document.body).append(mathjaxDiv);
$.getScript(chrome.extension.getURL('js/runMathJax.js'));
}
});
}
function getThemeCss(theme) {
return chrome.extension.getURL('theme/' + theme + '.css');
}
function setTheme(theme) {
var defaultThemes = ['Clearness', 'ClearnessDark', 'Github', 'TopMarks'];
if($.inArray(theme, defaultThemes) != -1) {
var link = $('#theme');
$('#custom-theme').remove();
if(!link.length) {
var ss = document.createElement('link');
ss.rel = 'stylesheet';
ss.id = 'theme';
//ss.media = "print";
ss.href = getThemeCss(theme);
document.head.appendChild(ss);
} else {
link.attr('href', getThemeCss(theme));
}
} else {
var themePrefix = 'theme_',
key = themePrefix + theme;
storage.get(key, function(items) {
if(items[key]) {
$('#theme').remove();
var theme = $('#custom-theme');
if(!theme.length) {
var style = $('<style/>').attr('id', 'custom-theme')
.html(items[key]);
$(document.head).append(style);
} else {
theme.html(items[key]);
}
}
});
}
}
function setMathJax() {
storage.get('enable_latex_delimiters', function(items) {
// Enable MathJAX LaTeX delimiters
if (items.enable_latex_delimiters) {
config.enableLatexDelimiters();
}
// Add MathJax configuration and js to document head
$.getScript('https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS_HTML');
var mjc = $('<script/>').attr('type', 'text/x-mathjax-config')
.html("MathJax.Hub.Config(" + JSON.stringify(config.mathjaxConfig) + ");");
$(document.head).append(mjc);
});
}
function stopAutoReload() {
clearInterval(interval);
}
function startAutoReload() {
stopAutoReload();
var freq = defaultReloadFreq;
storage.get('reload_freq', function(items) {
if(items.reload_freq) {
freq = items.reload_freq;
}
});
interval = setInterval(function() {
$.ajax({
url: location.href,
cache: false,
success: function(data) {
if (previousText == data) {
return;
}
makeHtml(data);
previousText = data;
}
});
}, freq * 1000);
}
function render() {
$.ajax({
url: location.href,
cache: false,
complete: function(xhr, textStatus) {
var contentType = xhr.getResponseHeader('Content-Type');
if(contentType && (contentType.indexOf('html') > -1)) {
return;
}
makeHtml(document.body.innerText);
var specialThemePrefix = 'special_',
pageKey = specialThemePrefix + location.href;
storage.get(['theme', pageKey], function(items) {
theme = items.theme ? items.theme : 'Clearness';
if(items[pageKey]) {
theme = items[pageKey];
}
setTheme(theme);
});
storage.get('auto_reload', function(items) {
if(items.auto_reload) {
startAutoReload();
}
});
}
});
}
storage.get(['exclude_exts', 'disable_markdown', 'mathjax'], function(items) {
if(items.disable_markdown) {
return;
}
if(items.mathjax) {
setMathJax();
}
var exts = items.exclude_exts;
if(!exts) {
render();
return;
}
var fileExt = getExtension(location.href);
if (typeof exts[fileExt] == "undefined") {
render();
}
});
chrome.storage.onChanged.addListener(function(changes, namespace) {
var specialThemePrefix = 'special_',
pageKey = specialThemePrefix + location.href;
for (key in changes) {
var value = changes[key];
if(key == pageKey) {
setTheme(value.newValue);
} else if(key == 'theme') {
storage.get(pageKey, function(items) {
if(!items[pageKey]) {
setTheme(value.newValue);
}
});
} else if(key == 'reload_freq') {
storage.get('auto_reload', function(items) {
startAutoReload();
});
} else if(key == 'auto_reload') {
if(value.newValue) {
startAutoReload();
} else {
stopAutoReload();
}
} else if(key == 'disable_markdown') {
location.reload();
}
}
});
}(document));
|