在Google Chrome扩展程序上打开新标签页

ora*_*nge 6 javascript google-chrome google-chrome-extension

这是我的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>
Run Code Online (Sandbox Code Playgroud)

abr*_*ham 8

您应该再次阅读chrome.tabs.create 文档.您正在传递invald参数.您还使用的locationbackground.html文档而不是代码所期望的网页文档,而不是tab传递给chrome.browserAction.onClicked侦听器的参数.

<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>
Run Code Online (Sandbox Code Playgroud)

  • 在哪里获取选项卡的 id? (2认同)