开发者

Firefox extension: How can I set the cursor position?

开发者 https://www.devze.com 2023-01-16 10:46 出处:网络
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:

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 t开发者_开发百科he cursor's position, but I need one to set it.

Any suggestions?


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!


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
0

精彩评论

暂无评论...
验证码 换一张
取 消