views:

43

answers:

2

I have a page with possibly several content-editable iframes (editors).

Now I would like to use my custom Firefox extension to do the following: Setting the cursor to the end (or last HTML element) of the editor the cursor actually is in.

I found many solutions to get the cursor's position, but I need one to set it.

Any suggestions?

+2  A: 

XPCOM likely includes such functionality as part of the testing rig. Mochitest at least is capable of this (again, probably though XPCOM).

On the other hand, when a user is on the system this a generally a gross violation of user interaction practices. Be sure you have a good justification for doing it. It may seem convenient but what if they're doing something else whilst using your addon? I usually have various apps open at once, Fx extensions are only part of that. I don't want it taking control of my mouse, EVER.

Is there something wrong with setting the focus? At least that only forces the user's hand at a window level.

It also suspect it make it quite difficult to get past AMO review. You'd have to justify why it was necessary to invoke such low-level functionality. If you interact with a window, for example, the window might be able to affect the input of your functions which in turn control the mouse... and then a random web site has access to the user's window!

Rushyo
AMO review is not necesary for me, the extension i develop is for one of our customers. In order to get a workaround for a replacement bug of spellchecking in hyphenated words in bold-tags a solution is to manually set the cursor elsewhere (at least it works if i do it as user by hand). The cursor is being set to the end of the sugested word by the spellchecker after the misspelled word has been replaced - so the user would not be aware of the cursor movement.
Thariama
Sorry for the seemingly silly question: Could you just elaborate on your definition of cursor?
Rushyo
the cursor (position) is the position where the blinking "|" stands and when a user types a letter it is written down (when in editable context)
Thariama
Aha. I was operating on the mistake of it being the mouse cursor! You'd be pretty lucky to find a function like that, even in the depths of XPCOM. I guess it must be in there for testing purposes but I sure wouldn't want to go fishing for it. Your best bet is probably to search MXR for an appropriate XPCOM component, if one exists, or ask on the Firefox IRC. I think it's just a little too specialised for SO.
Rushyo
well, i'll try that - thx for your suggestions
Thariama
A: 

Found the solution to my problem myself. This code myself will set the Cursor position to the last Paragraph of my editor:

var frame = window.content.document.getElementsByTagName('iframe')[2];
var win = frame.contentWindow;
var editingSession = Components.classes["@mozilla.org/editor/editingsession;1"].createInstance(Components.interfaces.nsIEditingSession);
var editor = editingSession.getEditorForWindow(win);

selection = window.getSelection();            

var body = frame.contentDocument.body;

text = frame.contentDocument.createTextNode(".");
body.lastChild.appendChild(text);  // add textnode to be selected

var range = editor.document.createRange();   
range.setStartBefore(text);
range.setEndAfter(text);
editor.selection.removeAllRanges();
editor.selection.addRange(range);

body.lastChild.removeChild(text); // remove Child
Thariama