将该字符串粘贴到文本框中。 Chrome扩展程序 [英] Paste the string to a text box. Chrome Extension

查看:177
本文介绍了将该字符串粘贴到文本框中。 Chrome扩展程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作需要复制和粘贴文本的Chrome扩展程序。假设用户在任何网页上选择一些文本,它应该被复制到一个变量中,并且当用户按下ctrl + 3时(如果在文本框或我们正常的ctrl + v工作的地方)它应该粘贴它。一个正常的复制粘贴工具,但具有不同的快捷键。

I am making a chrome extension that requires copy and pasting text. Suppose a user selects some text on any webpage, it should be copied into a variable and when the user presses ctrl+3, (if in a textbox or somewhere where our normal ctrl+v works) it should paste it. A normal copy paste tool but with different shortcuts.

目前我的脚本有这个功能:
我正在检索数据,但不知道如何粘贴。

Currently my script has this function: I am retrieving the data correctly but don't know how to paste it.

var copy_paste1;
document.onkeydown = function(evt) {
    evt = evt || window.event;
    if (evt.keyCode == 49 && evt.ctrlKey) {
        var c = window.getSelection();
        copy_paste1 = c
        alert(copy_paste1);
        document.getElementById("myButtonId").click();
    }
    if (evt.keyCode == 51 && evt.ctrlKey) {
        alert(copy_paste1);
        //****INSTEAD OF THIS ALERT I WANT TO PASTE!
    }
}; 

如何继续?

How can I proceed?

推荐答案

您可以使用 document.activeElement 获取当前关注的元素,然后尝试设置它的值:

You could use document.activeElement to get the currently focused element, and then try to set it's value:

document.activeElement.value = copy_paste1;

当然,这是最基本的方法 - 建议至少添加一些检查之前只是试图设置值(即检查类型是文本框,textarea等),但这应该让你开始。

This is, of course, the bare bones approach - it would be recommended to at least add some checks before simply trying to set the value (ie. check if type is textbox, textarea, etc.), but this should get you started.

这篇关于将该字符串粘贴到文本框中。 Chrome扩展程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆