Last active
December 22, 2015 00:08
-
-
Save zachleat/6386606 to your computer and use it in GitHub Desktop.
AnchorsAway.js
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
/*! AnchorsAway - v0.1.0 - 2013-08-30 | |
* Copyright (c) 2013 Zach Leatherman | |
* See also @zachleat http://www.zachleat.com | |
* MIT License | |
* | |
* A bookmarklet to show all of the available anchor links on a page. | |
* Originally developed to allow easier direct linking to specific paragraphs | |
* on Wikipedia. | |
* | |
* Demo: http://jsbin.com/UNiWiCO/1 | |
*/ | |
(function( win, doc ) { | |
if( !('querySelectorAll' in doc ) || !Array.prototype.forEach ) { | |
return; | |
} | |
Array.prototype.slice.call( doc.querySelectorAll('[id]') ).forEach(function( el ) { | |
var url = '#' + el.getAttribute( 'id' ), | |
offset = doc.createElement( 'div' ), | |
style = { | |
position: 'absolute', | |
left: el.offsetLeft + 'px', | |
top: el.offsetTop + 'px', | |
border: '2px solid', | |
background: '#ffc', | |
padding: '.1em .3em' | |
}, | |
overrides = { | |
background: 'rgba( 255, 255, 204, .8)' | |
}; | |
function styles( obj ) { | |
var str = ''; | |
for( var j in obj ) { | |
str += j + ':' + obj[j] + ';'; | |
} | |
return str; | |
} | |
offset.style.cssText = styles( style ) + styles( overrides ); | |
offset.innerHTML = '<a href="' + url + '">' + url + '</a>'; | |
if( el.tagName !== 'HTML' ) { | |
el.parentNode.insertBefore( offset, el.nextSibling || el ); | |
} | |
}); | |
})( this, this.document ); | |
// Bookmarklet | |
// javascript:(function(win,doc){if(!('querySelectorAll' in doc)||!Array.prototype.forEach){return;}Array.prototype.slice.call(doc.querySelectorAll('[id]')).forEach(function(el){var url='#'+el.getAttribute('id'),offset=doc.createElement('div'),style={position:'absolute',left:el.offsetLeft+'px',top:el.offsetTop+'px',border:'2px solid',background:'#ffc',padding:'.1em .3em'},overrides={background:'rgba( 255, 255, 204, .8)'};function styles(obj){var str='';for(var j in obj){str+=j+':'+obj[j]+';';}return str;}offset.style.cssText=styles(style)+styles(overrides);offset.innerHTML='<a href="'+url+'">'+url+'</a>';if(el.tagName!=='HTML'){el.parentNode.insertBefore(offset,el.nextSibling||el);}});})(this,this.document); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment