Last active
September 14, 2017 09:16
-
-
Save yogendra/6025babac0056a91a4e667aaa8234dca to your computer and use it in GitHub Desktop.
Simple Scripts to Modify HTML Content
This file contains hidden or 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
(function(module){ | |
if(window[module.name]){ | |
return; | |
} | |
module.init(); | |
window[module.name] = module.run; | |
})({ | |
name: "highlightSelection", | |
init : function(){ | |
var css = document.createElement('link'); | |
css.rel = 'stylesheet'; | |
css.href= '//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/default.min.css'; | |
document.body.appendChild(css); | |
var script = document.createElement("script"); | |
script.src = "//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"; | |
document.body.appendChild(script); | |
}, | |
run : function(lang){ | |
if(!window.hljs){ | |
console.log("Waiting for highlight script to load"); | |
return; | |
} | |
var selection = window.getSelection().anchorNode; | |
if(!selection){ | |
return; | |
} | |
var target = selection.parentElement; | |
if(lang){ | |
target.className += " lang-" + lang; | |
} | |
hljs.highlightBlock(target); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment