在Chrome扩展中获取选定的文本 [英] Get selected text in a chrome extension
问题描述
我想制作一个扩展程序,它将所选文本并在Google翻译
中进行搜索,但我无法弄清楚如何获取所选文本。
这是我的manifest.json
{
manifest_version:2,
name :
版本:1,
description:Saeed Translate for Chrome,
图标:{
16: icon.png
},
content_scripts:[{
all_frames:true,
js:[content_script.js],
matches:[http:// * / *,https:// * / *],
run_at:document_start
}],
背景:{
scripts:[background.js]
},
权限:[
contextMenus,
background,
标签
}
和我的background.js文件
var text =http://translate.google.com/#auto/fa/ ;
函数onRequest(request,sender,sendResponse){
text =http://translate.google.com/#auto/fa/;
text = text + request.action.toString();
sendResponse({});
};
chrome.extension.onRequest.addListener(onRequest);
chrome.contextMenus.onClicked.addListener(function(tab){
chrome.tabs.create({url:text});
});
chrome.contextMenus.create({title:Translate'%s',contexts:[selection]});
和我的content_script.js文件
var sel = window.getSelection();
var selectedText = sel.toString();
chrome.extension.sendRequest({action:selectedText},function(response){
console.log('Start action sent');
});
如何获取所选文字?
你做得比实际更复杂一点。您不需要在内容脚本和背景页面之间使用消息,因为contextMenus.create方法已经可以捕获选定的文本。尝试调整您的创作脚本类似于:
chrome.contextMenus.create({title:Translate'%s',上下文:[all],onclick:onRequest});
然后调整您的函数以获取info.selectionText:
函数onRequest(info,tab){
var selection = info.selectionText;
//用选择
做些事情;
请注意,如果您想远程访问外部网站,例如Google翻译,您可能需要调整权限设置。
I wanna make an extension that takes the selected text and searches it in google translate but I can't figure out how to get the selected text.
Here is my manifest.json
{
"manifest_version": 2,
"name": "Saeed Translate",
"version": "1",
"description": "Saeed Translate for Chrome",
"icons": {
"16": "icon.png"
},
"content_scripts": [ {
"all_frames": true,
"js": [ "content_script.js" ],
"matches": [ "http://*/*", "https://*/*" ],
"run_at": "document_start"
} ],
"background": {
"scripts": ["background.js"]
},
"permissions": [
"contextMenus",
"background",
"tabs"
]
}
and my background.js file
var text = "http://translate.google.com/#auto/fa/";
function onRequest(request, sender, sendResponse) {
text = "http://translate.google.com/#auto/fa/";
text = text + request.action.toString();
sendResponse({});
};
chrome.extension.onRequest.addListener(onRequest);
chrome.contextMenus.onClicked.addListener(function(tab) {
chrome.tabs.create({url:text});
});
chrome.contextMenus.create({title:"Translate '%s'",contexts: ["selection"]});
and my content_script.js file
var sel = window.getSelection();
var selectedText = sel.toString();
chrome.extension.sendRequest({action: selectedText}, function(response) {
console.log('Start action sent');
});
How do I get the selected text?
You are making it a bit more complicated than it really is. You don't need to use a message between the content script and background page because the contextMenus.create method already can capture selected text. Try adjusting your creations script to something like:
chrome.contextMenus.create({title:"Translate '%s'",contexts: ["all"], "onclick": onRequest});
Then adjust your function to simply get the info.selectionText:
function onRequest(info, tab) {
var selection = info.selectionText;
//do something with the selection
};
Please note if you want to remotely access an external site like google translate you may need to adjust your permissions settings.
这篇关于在Chrome扩展中获取选定的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!