Html Chrome 扩展程序获取选定的文本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19164474/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 14:04:35  来源:igfitidea点击:

Chrome Extension get selected text

javascripthtmlgoogle-chromegoogle-chrome-extension

提问by Christoffer Jacobsen

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

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

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.

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

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:

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

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..

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

回答by rsanchez

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];
});

回答by Vaibs_Cool

You can do this by using Extensions Messaging. Basically, your "background page" will send the request to your service. For example, lets say you have a "popup" and once you click on it, it will do a "Google search" which is your service.

您可以通过使用Extensions Messaging来做到这一点。基本上,您的“背景页面”会将请求发送到您的服务。例如,假设您有一个“弹出窗口”,一旦您点击它,它就会进行“Google 搜索”,这是您的服务。

chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
    if (request.method == "getSelection")
      sendResponse({data: window.getSelection().toString()});
    else
      sendResponse({}); // snub them.
});

Some References

一些参考资料

  1. Creating a chrome extension which takes highlighted text on the page and inserts it into a textarea in popup.html
  1. 创建一个 chrome 扩展,它在页面上突出显示文本并将其插入到 popup.html 中的 textarea 中

Or you can use this plugin

或者你可以使用这个插件

  1. https://chrome.google.com/webstore/detail/view-selection-source/fbhgckgfljgjkkfngcoeajbgndkeoaaj
  1. https://chrome.google.com/webstore/detail/view-selection-source/fbhgckgfljgjkkfngcoeajbgndkeoaaj

回答by Vasya Shevchenko

For Angular 8.2 I use this code:

对于 Angular 8.2,我使用以下代码:

chrome.tabs.executeScript( { code: 'window.getSelection().toString();'}, selectedText => {
 (document.getElementById('text-input') as HTMLInputElement).value = selectedText[0];
  console.log(selectedText[0]);
});