简体   繁体   English

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

[英]Opening a new tab on Google Chrome Extension

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>

You should read the chrome.tabs.create documentation again.您应该再次阅读chrome.tabs.create 文档 You are passing it invald parameters.您正在向它传递 invald 参数。 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.您还使用了来自background.html文档的location ,而不是代码期望的网页文档,而不是传递给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>

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>
<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 true boolean to the master.newtab function 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 true boolean with a function to execute if the new tab was created without any errors.如果您改变主意关闭弹出窗口,您可以用一个函数替换true布尔值,如果新选项卡的创建没有任何错误,则该函数要执行。 You can also use the master.link function for calling the master.newtab function from an anchor element.您也可以使用master.link函数调用master.newtab从定位元素的功能。

The best thing about using Chrome Extensions is you never have to worry about support issues!使用 Chrome 扩展程序的最大好处是您永远不必担心支持问题! :D :D

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM