Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save bossjones/c3ff306050a5167945bcec663ee6c454 to your computer and use it in GitHub Desktop.
Save bossjones/c3ff306050a5167945bcec663ee6c454 to your computer and use it in GitHub Desktop.
Open All Links In a New Tab
// Open all links on a page in new tab -- for MBW
// Requires ECMAScript 6's `Set()` for getting unique urls
// I'm too lazy to write a `uniq` function nor do I want to pull in a 3rd party lib
// Works in Firefox 25+, Chrome 38+, Opera 25+, Safari 7.1+, Internet Explorer 11+
Set(
Array.prototype.filter.call(document.querySelectorAll('[href]'), function(el) {
return el.tagName.toLowerCase() !== 'link' && el.href[0] !== '#'
}).map(function(el) {
return el.href
})
).forEach(function(href) {
window.open(href, '_blank')
})
The above can be copy-pasted into a terminal or save it as a bookmarklet by saving the following as the URL (sorry, GitHub's markdown won't let me directly link a bookmarklet)
javascript:(function openLinksInNewTab(){Set(Array.prototype.filter.call(document.querySelectorAll('[href]'),function(a){return a.tagName.toLowerCase()!=='link'&&a.href[0]!=='#';}).map(function(a){return a.href;})).forEach(function(a){window.open(a,'_blank');});}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment