[Greasemonkey] is selection a click?
Roderick McGuire
mcguire at telerama.com
Wed Mar 8 16:09:38 EST 2006
I have a script where it would be convenient if the user selecting text
with the mouse always generated at click event.
The user script below seems to demonstrate that a click is generated
only if the selection is entirely within one #text node - Try it with a
selection that spans DOM nodes, and then click the “dummy button”.
Is there any event generated when a DOM spanning selection is made?
Also I don't understand the logic of when selections are collapsed - if
you make a selection and then click on some body text the selection is
collapsed/removed. However if you click on the "dummy button" provided
by the below script, this click does not remove the selection (which of
course makes sense). What is the mechanism behind this?
// ==UserScript==
// @name is selection a click?
// @namespace ~rmm
// @description demonstrates click vs selection behavior
// @include *
// ==/UserScript==
function bodyClick(e){
var sel = window.getSelection();
var range = sel.getRangeAt(0);
GM_log('\ngot a click, selection = ' + sel);
if (!range.collapsed)
{var tempDiv = document.createElement('div');
tempDiv.appendChild(range.cloneContents());
GM_log('\nselection HTML =\n'
+ tempDiv.innerHTML);
}
}
PAN = document.createElement('div');
PAN.id ='MyPan';
PAN.innerHTML = '<button>dummy button</button>';
document.body.appendChild(PAN);
GM_addStyle('#MyPan {z-index: 1000; position: fixed; right: 0; bottom:
0; padding: 3px; color: rgb(0,0,0); background-color: rgb(255, 255, 200)}');
document.addEventListener("click", function (e){bodyClick(e)}, false);
More information about the Greasemonkey
mailing list