Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

<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>
like image 393
orange Avatar asked Dec 10 '11 15:12

orange


People also ask

What is new tab extension?

This extension replaces Chrome's New Tab page with a minimalist version which shows a blank white page and an imitation of the Bookmarks Bar. To recover the default New Tab page, simply remove the extension. 14.0. September 6, 2022.


3 Answers

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>
like image 72
abraham Avatar answered Nov 03 '22 13:11

abraham


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

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. You can also use the master.link function for calling the master.newtab function from an anchor element.

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

like image 40
Mikett Avatar answered Nov 03 '22 13:11

Mikett


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>
like image 2
user219882 Avatar answered Nov 03 '22 11:11

user219882