Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
396 views
in Technique[技术] by (71.8m points)

javascript - Javascript:如何在光标的当前位置插入域?(Javascript: how do I insert the domain at the current position of the cursor?)

Can Javascript in the form of a bookmarklet or similar, insert the domain of the current page at the position of the cursor?(可以采用书签或类似形式的Javascript在光标所在的位置插入当前页面的域吗?)

How?(怎么样?) Interested in inserting both stackoverflow.com , www.stackoverflow.com as well as just www .(有兴趣插入stackoverflow.comwww.stackoverflow.com以及www 。) (Use case: I am writing a question here at SO, the cursor is in either the title text field or the question body text area and I want to insert the domain name at the current position of the cursor).((用例:我在SO处写一个问题,光标位于标题文本字段或问题正文文本区域中,我想在光标的当前位置插入域名)。)   ask by d-b translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

When focusing the textarea, click the bookmarklet, and use document.activeElement to identify the textarea, and .slice its value, concatenating with window.location.origin :(当集中文本区域时,单击小书签,并使用document.activeElement标识文本区域,并对其值进行.slice ,并与window.location.origin串联:)

javascript: (() => { const textarea = document.activeElement; const { selectionStart } = textarea; textarea.value = textarea.value.slice(0, selectionStart) + window.location.origin + textarea.value.slice(selectionStart); })(); If you want the bookmarklet to work for the iframe inside https://robot-parts.confetti.events/ , then access the contentWindow.document to get to the activeElement :(如果您希望小书签在https://robot-parts.confetti.events/的iframe中起作用, https://robot-parts.confetti.events/访问contentWindow.document进入activeElement :) javascript: (() => { const textarea = document.querySelector('.signup-iframe').contentWindow.document.activeElement; const { selectionStart } = textarea; textarea.value = textarea.value.slice(0, selectionStart) + window.location.origin + textarea.value.slice(selectionStart); })();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...