Javascript getSelection在Chrome扩展中不返回任何值

Javascript getSelection在Chrome扩展中不返回任何值,javascript,google-chrome,google-chrome-extension,Javascript,Google Chrome,Google Chrome Extension,我正在尝试一个简单的chrome扩展,在这里我选择一些文本并按下按钮,然后用谷歌搜索所选的术语 下面是我的背景。html <script> function Req() { var str=document.getSelection().toString(); //window.getSelection().toString() tried both alert(str); var Call =' http://www.google.com/search?q='+str; alert

我正在尝试一个简单的chrome扩展,在这里我选择一些文本并按下按钮,然后用谷歌搜索所选的术语

下面是我的背景。html

<script>
function Req() {
var str=document.getSelection().toString(); //window.getSelection().toString() tried both
alert(str);
var Call =' http://www.google.com/search?q='+str;
alert(Call)
chrome.tabs.create({url: Call});
}
</script>
</head>
<body onload="Req()" >
</body>
</html>
我尝试了所有方法,但window.getSelection().toString()不返回任何值。
请帮我解决这个问题。

你有很多错误

  • 这是什么<代码>“background\u html”:“background.html”
  • “弹出窗口”:“background.html”
    -弹出页面无权访问内容脚本
  • var str=document.getSelection().toString()-您将收到页面“background.html”的选定块,但不会收到内容页
  • 如果要在按下按钮时执行操作,请参阅
  • 如何将消息从后台发送到内容并返回

每个主进程只执行一次后台页面。您应该切换到内容脚本。我也尝试过使用内容脚本,但不知道在manifest.json中写什么,甚至内容脚本也不返回
window.getSelection().toString()的值
内容脚本内部应返回当前选定的文本。是否确实在运行函数时选择了某些文本?
{
     "background_html":"background.html",
     "name": "goog app",
   "description": "googles selected term",
   "browser_action": {
    "default_icon": "icon.png",
"popup":"background.html"
  },
"permissions": ["tabs","http://www.google.com/","https://www.google.com/"],

    "version": "1.0"
}