这是我的background.html文件,它在当前选项卡中打开时工作正常,但我希望它在新选项卡中打开,我做错了什么?
<html> <head> <script> // Called when the user clicks on the browser action. chrome.browserAction.onClicked.addListener(function(tab) { var action_url = "javascript:location.href='http://www.reddit.com/submit?url='+encodeURIComponent(location.href)+'&title='+encodeURIComponent(document.title)"; chrome.tabs.create(tab.id,{url: action_url},function(tab)); }); </script> </head> </html>
解决方法
您应该再次阅读chrome.tabs.create
documentation.您正在传递invald参数.您还使用来自background.html文档的位置,而不是代码所期望的网页文档,而不是传递给chrome.browserAction.onClicked侦听器的tab参数.
<html> <head> <script> // Called when the user clicks on the browser action. chrome.browserAction.onClicked.addListener(function(tab) { var action_url = "http://www.reddit.com/submit?url=" + encodeURIComponent(tab.href) + '&title=' + encodeURIComponent(tab.title); chrome.tabs.create({ url: action_url }); }); </script> </head> </html>