Skip to content

Instantly share code, notes, and snippets.

@cmcculloh
Last active August 8, 2017 18:10
Show Gist options
  • Save cmcculloh/5cf71af2c055344b28e02815339599d9 to your computer and use it in GitHub Desktop.
Save cmcculloh/5cf71af2c055344b28e02815339599d9 to your computer and use it in GitHub Desktop.
Bookmarklet that allows you to highlight (permanently for that visit) text on page.
javascript: (function() {
/* STYLES */
const css = '.highlight { background: yellow; }';
const head = document.head || document.getElementsByTagName('head')[0];
const style = document.createElement('style');
style.type = 'text/css';
if (style.styleSheet){
style.styleSheet.cssText = css;
} else {
style.appendChild(document.createTextNode(css));
}
head.appendChild(style);
/* HIGHLIGHT FUNCTION */
function highlight(text) {
let innerHTML = document.body.innerHTML.toLowerCase();
const index = innerHTML.indexOf(text.toLowerCase());
if ( index >= 0 ) {
innerHTML = innerHTML.substring(0,index) + "<span class='highlight'>" + innerHTML.substring(index,index+text.length) + "</span>" + innerHTML.substring(index + text.length);
document.body.innerHTML = innerHTML
}
}
/* EVENT HANDLER */
window.onmouseup = function(e) {
highlight(window.getSelection().toString());
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment