【问题标题】:getSelection does not return any value in Chrome extensiongetSelection 在 Chrome 扩展中不返回任何值
【发布时间】:2011-11-28 22:23:50
【问题描述】:

我正在尝试一个简单的 chrome 扩展程序,我在其中选择一些文本并按下按钮,然后搜索所选术语。

下面是我的background.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>

下面是我的 ma​​nifest.json

{
     "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"
}

我已经尝试了所有方法,但 window.getSelection().toString() 没有返回任何值。 请帮我解决问题。

【问题讨论】:

  • 后台页面每个主进程只执行一次。您应该切换到内容脚本。
  • 我也试过用内容脚本来做,但不知道在 manifest.json 中写什么,甚至内容脚本也没有返回 window.getSelection().toString() 的值。
  • window.getSelection().toString() 在内容脚本中应该返回当前选择的文本。您确定在运行该函数时选择了某些文本吗?

标签: javascript google-chrome google-chrome-extension


【解决方案1】:

你有很多错误

【讨论】:

    猜你喜欢
    • 2018-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-08
    相关资源
    最近更新 更多