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

前端之家收集整理的这篇文章主要介绍了javascript – 在Google Chrome扩展程序上打开新标签页前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
这是我的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>
原文链接:https://www.f2er.com/js/150461.html

猜你在找的JavaScript相关文章