Skip to content

Instantly share code, notes, and snippets.

@yogendra
Last active September 14, 2017 09:16
Show Gist options
  • Save yogendra/6025babac0056a91a4e667aaa8234dca to your computer and use it in GitHub Desktop.
Save yogendra/6025babac0056a91a4e667aaa8234dca to your computer and use it in GitHub Desktop.
Simple Scripts to Modify HTML Content
(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