在Google Chrome扩展程序上打开新标签 [英] Opening a new tab on Google Chrome Extension

查看:99
本文介绍了在Google Chrome扩展程序上打开新标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的background.html文件,它在当前标签中打开时工作正常,但我希望它在新标签中打开,我做错了什么?

This is my background.html file, It works fine when opening in current tab but I want it to open in new tab, what am I doing wrong?

<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 再次文档 。你正在传递它的参数。您还在使用位置,它来自 background.html 文档,而不是代码所期望的网页文档,而不是选项卡传递给 chrome.browserAction.onClicked 侦听器的参数。

You should read the chrome.tabs.create documentation again. You are passing it invald parameters. You are also using location which is from the background.html document not the webpage document the code is expecting instead of the tab parameter passed to the chrome.browserAction.onClicked listener.

<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>

这篇关于在Google Chrome扩展程序上打开新标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆