Chrome扩展程序获取所选文字 [英] Chrome Extension get selected text

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

问题描述

我正在寻找将所选文字放入我的Chrome扩展程序的方法。



我想要前。在Facebook供稿中选择一个文本,当我点击我的图标时,它会得到它并在我的扩展中显示选定的文本。



到目前为止:

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

它会获取所选文字,并在Chrome中通过消息提醒。但是我想在我的html弹出窗口中显示它。我想这样写出来:

  document.getElementById(output)。value =这里选择的文本(但是如何)

需要帮助!我知道还有其他问题,但他们没有给我完全我想要的东西。

使用回调函数中执行代码评估的最后一个表达式:

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


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

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());"})

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天全站免登陆