Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.