Last active
October 12, 2023 02:43
-
-
Save yous/0b48b98352215ab22eb8591b78f39361 to your computer and use it in GitHub Desktop.
Marked on Google Drive
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name Marked on Google Drive | |
// @namespace https://github.com/yous | |
// @version 1.1.0 | |
// @description Render markdown preview on Google Drive | |
// @author yous | |
// @homepageURL https://gist.github.com/yous/0b48b98352215ab22eb8591b78f39361 | |
// @updateURL https://gist.github.com/yous/0b48b98352215ab22eb8591b78f39361/raw/marked-google-drive.user.js | |
// @downloadURL https://gist.github.com/yous/0b48b98352215ab22eb8591b78f39361/raw/marked-google-drive.user.js | |
// @license MIT | |
// @match https://drive.google.com/file/* | |
// ==/UserScript== | |
(function() { | |
'use strict'; | |
function loadLink(url) { | |
var link = document.createElement('link'); | |
link.rel = 'stylesheet'; | |
link.href = url; | |
document.body.appendChild(link); | |
} | |
function loadScript(url, callback) { | |
var script = document.createElement('script'); | |
script.src = url; | |
if (callback) { | |
script.onload = callback; | |
} | |
document.body.appendChild(script); | |
} | |
function main() { | |
var pre = document.querySelector('div[role="document"]>pre'); | |
if (!pre) { | |
setTimeout(main, 500); | |
return; | |
} | |
var highlightLoaded = false; | |
loadLink('https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.7.0/build/styles/default.min.css'); | |
loadScript('https://cdn.jsdelivr.net/gh/highlightjs/cdn-release@11.7.0/build/highlight.min.js', function() { | |
highlightLoaded = true; | |
}); | |
function markedOnLoad() { | |
if (!highlightLoaded) { | |
setTimeout(markedOnLoad, 500); | |
return; | |
} | |
marked.setOptions({ | |
highlight: function(code, lang) { | |
var language = hljs.getLanguage(lang) ? lang : 'plaintext'; | |
return hljs.highlight(code, { language: language }).value; | |
} | |
}); | |
pre.innerHTML = marked.parse(pre.textContent); | |
} | |
loadScript('https://cdn.jsdelivr.net/npm/marked@4.3.0/marked.min.js', markedOnLoad); | |
} | |
main(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment