-
Notifications
You must be signed in to change notification settings - Fork 0
/
Scripts-n-Styles-body-script.js
64 lines (52 loc) · 2.17 KB
/
Scripts-n-Styles-body-script.js
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
//
// Paste this script into the <body> section of the Scripts tab of
// the Scripts-n-Styles plugin. This can be done on a per-post or per-page basis,
// or (untested at this time) can be applied at the Global level of Scripts-n-Styles.
//
function addScript(filename, onload) {
var head = document.getElementsByTagName('head')[0];
var script = document.createElement('script');
script.src = filename;
script.async = true;
script.type = 'text/javascript';
script.onload = onload;
head.appendChild(script);
}
function addCSS(filename){
var head = document.getElementsByTagName('head')[0];
var style = document.createElement('link');
style.href = filename;
style.type = 'text/css';
style.rel = 'stylesheet';
head.appendChild(style);
}
var smartdownGistSourceTitle = 'Smartdown Gist Source';
var articles = Array.from(document.getElementsByTagName('article'));
var smartdownArticles = articles.filter(function(article) {
var firstAnchor = document.querySelector('article#' + article.id + ' div.entry-content a');
return firstAnchor && firstAnchor.innerText === smartdownGistSourceTitle;
});
var firstSmartdownArticle = smartdownArticles[0];
if (firstSmartdownArticle) {
console.log('SMARTDOWN enabled', firstSmartdownArticle);
var contentDivSelector = 'article#' + firstSmartdownArticle.id + ' div.entry-content';
var contentDiv = document.querySelector(contentDivSelector);
contentDiv.classList.add('smartdown-container');
window.smartdownOutputDivSelector = contentDivSelector;
addCSS('https://smartdown.site/lib/smartdown.css');
addCSS('https://smartdown.site/lib/fonts.css');
var starter = 'https://smartdown.site/lib/starter.js';
addScript('https://smartdown.site/lib/smartdown.js', function() {
addScript(starter, function() {
var sel = window.smartdownOutputDivSelector + ' a';
var gistAnchor = document.querySelectorAll(sel)[0];
if (gistAnchor.innerHTML === smartdownGistSourceTitle) {
var gistPrefix = 'https://gist.github.com/';
window.smartdownBaseURL = 'https://smartdown.site/';
window.smartdownRawPrefix = gistAnchor.href;
var baseHash = '#gist/' + window.smartdownRawPrefix.slice(gistPrefix.length);
window.smartdownStarter(baseHash);
}
});
});
}