Skip to content

Instantly share code, notes, and snippets.

@sutlxwhx
Last active August 20, 2018 18:15
Show Gist options
  • Save sutlxwhx/bf799bd97d519f973653785d018f5460 to your computer and use it in GitHub Desktop.
Save sutlxwhx/bf799bd97d519f973653785d018f5460 to your computer and use it in GitHub Desktop.
Copy to the clipborad list of all h1/h2/h3 elements on the page
# Declare tag name that you want to extract
var elements = document.getElementsByTagName("h2");
# Create a temporary element where all the data will be stored
var buffer = document.createElement("buffer");
document.body.appendChild(buffer);
# Declare the string var where all the data from the page will be stored
var names = '';
# Create a foor loop and append all new elements to the string
for(var i=0; i<elements.length; i++) {
names += elements[i].innerText + '\n';;
}
# Merge temporary element data with acquired data from the current page
buffer.innerText = names.toString();
# Use native Javascript command to copy the gathered data
# https://developers.google.com/web/updates/2015/04/cut-and-copy-commands
var range = document.createRange();
range.selectNode(buffer);
window.getSelection().addRange(range);
# Perform the copy to clipboard process
document.execCommand("copy");
# Delete the temporary element with all tag data
document.body.removeChild(buffer);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment