Skip to content

Instantly share code, notes, and snippets.

@rudiedirkx
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rudiedirkx/9536267 to your computer and use it in GitHub Desktop.
Save rudiedirkx/9536267 to your computer and use it in GitHub Desktop.
Create a new popup to read the current selection
(function() {
function parents(node) {
var el = node,
els = [];
while ( el && (el = el.parentNode) ) {
els.push(el);
}
return els;
}
var s = getSelection(),
from = s.baseNode,
to = s.extentNode;
var parents1 = parents(from),
parents2 = parents(to),
ancestor;
outside:
for ( var i=0, L=parents1.length; i<L; i++ ) {
for ( var j=0, M=parents2.length; j<M; j++ ) {
if ( parents1[i] == parents2[j] ) {
ancestor = parents1[1];
break outside;
}
}
}
console.log('common ancestor', ancestor);
if ( ancestor ) {
console.log('====' + ancestor.textContent.trim() + '====')
with ( ancestor.style ) {
position = 'fixed';
top = '5%';
right = '5%';
bottom = '5%';
left = '5%';
outline = 'solid 200px rgba(0, 0, 0, 0.5)';
background = 'white';
color = 'black';
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment