我正在尝试一个简单的 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>
下面是我的manifest.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() 没有返回任何值。 请帮我找出问题所在。
最佳答案
你有很多错误
- 这是什么?
"background_html":"background.html"
"popup":"background.html"
- 弹出页面无法访问内容脚本var str=document.getSelection().toString();
- 您将收到页面“background.html”的选定 block ,但不会收到内容页面- 如果要在按下按钮时执行操作,请参阅 http://code.google.com/chrome/extensions/browserAction.html#event-onClicked
- 如何从后台向内容发送消息并返回 http://code.google.com/chrome/extensions/extension.html#method-sendRequest
关于javascript - getSelection 在 Chrome 扩展中不返回任何值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8303243/