Chrome 扩展程序获取选定的文本 [英] Chrome Extension get selected text

查看:29
本文介绍了Chrome 扩展程序获取选定的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种方法将所选文本放入我的 Chrome 扩展程序中.

I am looking for a way to get the selected text into my Chrome extension.

我想分手.在 facebook feed 中选择一个文本,当我点击我的图标时,它会得到它并在我的扩展中显示选定的文本.

I want to ex. select a text in facebook feed and when I click my icon it will get it and show the selected text in my Extension.

到目前为止我得到了这个:

I got this so far:

chrome.tabs.executeScript(null, {
  code: "alert(window.getSelection().toString());"
})

它获取选定的文本并在 Chrome 中用消息提醒它.但是我想在我的 html 弹出窗口中显示它.我想这样写:

it gets the selected text and alert it with a message in Chrome. However I want to show it in my html popup. I want to write it out like this:

document.getElementById("output").value = "Selected text here(but how)"

需要帮助!我知道还有其他问题,但他们没有给我我想要的.

Need help! and I know there is other question about this, but they didn't give me exactly what I want..

推荐答案

您可以在回调函数中使用由执行代码计算出的最后一个表达式:

You can use the last expression evaluated by the executed code in a callback function:

chrome.tabs.executeScript( {
  code: "window.getSelection().toString();"
}, function(selection) {
  document.getElementById("output").value = selection[0];
});

这篇关于Chrome 扩展程序获取选定的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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