Javascript 在 Google Chrome 扩展程序上打开一个新标签

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8457382/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 06:05:19  来源:igfitidea点击:

Opening a new tab on Google Chrome Extension

javascriptgoogle-chromegoogle-chrome-extension

提问by orange

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?

这是我的 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>

回答by abraham

You should read the chrome.tabs.createdocumentationagain. You are passing it invald parameters. You are also using locationwhich is from the background.htmldocument not the webpage document the code is expecting instead of the tabparameter passed to the chrome.browserAction.onClickedlistener.

您应该再次阅读chrome.tabs.create文档。您正在向它传递 invald 参数。您还使用了location来自background.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>

回答by user219882

You can try this

你可以试试这个

<html>
...
<body>
    <script>
    function createTab() {
        chrome.tabs.create({url: "http://www.stackoverflow.com"});
    }
    </script>
    <a href="#" onclick="createTab();">Create a new tab</a>
</body>
</html>

回答by Mikett

<html>
 <head>
  <script type="text/javascript">
   window.master = ({
     newtab: function(url, callback) {
       callback = callback === true ? (function() { this.close(); }) : callback;

       try {
         chrome.tabs.create({
           url: url
         });

         if(typeof callback === "function") { callback.call(this, url); }
       } catch(e) {
         /* Catch errors due to possible permission issues. */
       }
     },

     link: function(event, close) {
       event = event ? event : window.event;
       event.preventDefault();
       this.newtab(event.href, close);
     },

     close: function() { window.self.close(); }
   });
  </script>
 </head>

 <body>
  <!-- Usage is simple:

        HTML:
         <a href="http://example.com/" onclick="master.link(event)" />

        JavaScript:
         master.newtab("http://example.com/", true);
  -->
 </body>
</html>

If you insist on using a popup and want it to close as soon as it is opened, then use what is above. Simply add the link string and a trueboolean to the master.newtabfunction to have it open the new tab and then close the popup.

如果您坚持使用弹出窗口并希望它在打开后立即关闭,请使用上面的内容。只需将链接字符串和一个true布尔值添加到master.newtab函数中,即可打开新选项卡,然后关闭弹出窗口。

If you change your mind about closing the popup, you can replace the trueboolean with a function to execute if the new tab was created without any errors. You can also use the master.linkfunction for calling the master.newtabfunction from an anchor element.

如果您改变主意关闭弹出窗口,您可以true用一个函数替换布尔值,如果新选项卡创建时没有任何错误,则要执行。您还可以使用该函数从锚元素master.link调用该master.newtab函数。

The best thing about using Chrome Extensions is you never have to worry about support issues! :D

使用 Chrome 扩展程序的最大好处是您永远不必担心支持问题!:D