小编sta*_*ine的帖子

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

我想做一个扩展,采取所选文本并在谷歌翻译中搜索它,但我无法弄清楚如何获取所选文本.

这是我的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"
]

}
Run Code Online (Sandbox Code Playgroud)

和我的background.js文件

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"]});
Run Code Online (Sandbox Code Playgroud)

和我的content_script.js文件

var …
Run Code Online (Sandbox Code Playgroud)

javascript google-chrome google-chrome-extension

2
推荐指数
1
解决办法
5443
查看次数