Last active
May 3, 2023 21:05
-
-
Save sbamin/14f2fb68848c77450adc1b2d1df36da9 to your computer and use it in GitHub Desktop.
ChatGPT way of highlighting text in a html page and extracting it in a text file. https://shareg.pt/dIVonZZ
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
javascript:(function(){ | |
// via ChatGPT, https://shareg.pt/dIVonZZ | |
var highlights = []; | |
var style = document.createElement("style"); | |
style.type = "text/css"; | |
style.innerHTML = ".highlight { background-color: yellow !important; color: black !important; }"; | |
document.getElementsByTagName("head")[0].appendChild(style); | |
document.addEventListener("mouseup", function() { | |
var selection = window.getSelection().toString(); | |
if (selection && highlights.indexOf(selection) === -1) { | |
highlights.push(selection); | |
var range = window.getSelection().getRangeAt(0); | |
var highlightElement = document.createElement("span"); | |
highlightElement.className = "highlight"; | |
range.surroundContents(highlightElement); | |
} | |
}); | |
document.addEventListener("keydown", function(e) { | |
if (e.keyCode === 90 && e.ctrlKey && highlights.length > 0) { | |
var lastHighlight = highlights.pop(); | |
var highlightElements = document.getElementsByClassName("highlight"); | |
for (var i = 0; i < highlightElements.length; i++) { | |
if (highlightElements[i].innerText === lastHighlight) { | |
var parent = highlightElements[i].parentNode; | |
while (highlightElements[i].firstChild) { | |
parent.insertBefore(highlightElements[i].firstChild, highlightElements[i]); | |
} | |
parent.removeChild(highlightElements[i]); | |
} | |
} | |
} | |
}); | |
})(); |
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
javascript:(function() { | |
// via ChatGPT | |
// Use this code a second bookmarklet and once a page has all highlights via highlight.js bookmarklet, | |
// click this bookmarklet to fetch all of highlights in a text file. | |
var highlights = document.querySelectorAll('.highlight'); | |
var text = ''; | |
for (var i = 0; i < highlights.length; i++) { | |
text += '- ' + highlights[i].innerText + '\n'; | |
} | |
var blob = new Blob([text], {type: 'text/plain'}); | |
var link = document.createElement('a'); | |
link.href = URL.createObjectURL(blob); | |
link.download = 'highlights.txt'; | |
link.click(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment