Skip to content

Instantly share code, notes, and snippets.

@toastal
Last active October 18, 2021 07:54
Show Gist options
  • Save toastal/01c2387456ad2e6385f4 to your computer and use it in GitHub Desktop.
Save toastal/01c2387456ad2e6385f4 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+
new Set(
Array.prototype.filter.call(document.querySelectorAll('a[href],area[href]'), function(acc, el) {
el.href[0] !== '#' && acc.push(el)
return acc
}, [])
).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(){new Set(Array.prototype.reduce(document.querySelectorAll('a[href],area[href]'),function(a,e){ e.href[0]!=='#'&&a.push(e);return a},[])).forEach(function(a){window.open(a,'_blank')})}());
@toastal
Copy link
Author

toastal commented Oct 18, 2021

Updated to be more efficient. I wrote this 6 years ago with very little JavaScript experience at the time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment