5

As per my requirement, i need to create a Google Chrome Extension which opens multiple links (25+) on a single click in different tabs of a single chrome window. The code was working fine earlier till the Chrome 18. Now, I am using chrome 24 and that code stopped working. I was simply storing all the links in an array and opening them using a for loop as follows:

  for(var i = 0; i<links.length; i++)
  {
    var tablink = links[i];
    if(links[i] != "")
    {
            tablink = *"somedomain"* + tablink;
        setTimeout(window.open(tablink), 500);  
    }
  }  

As a result, only two links were open in tabs and rest will open in different chrome windows. What should i do to overcome this?

Edit #1

In my manifest file

"content_scripts": [
    {
      "matches": ["http://*/*", "https://*/*"],
      "js": ["script.js", "jquery.js", "dialog.js"]
    }
  ],


"permissions": [
    "tabs", "http://*/*", "https://*/*"
  ],

The code given first is in dialog.js

Saurabh Saxena
  • 3,005
  • 10
  • 31
  • 46
  • 4
    If this really is an extension, why aren't you using the [tab api](http://developer.chrome.com/extensions/tabs.html#method-create)? – sachleen Nov 20 '12 at 06:49
  • `I need to open multiple links (25+) on a single click` 25 tabs opened at the same time... o.O – Derek 朕會功夫 Nov 20 '12 at 06:55
  • i have tried using chrome.tabs.create({"url":tablink}); but it won't worked – Saurabh Saxena Nov 20 '12 at 06:56
  • i tried this window.open("http://www.google.co.in/");window.open("https://www.facebook.com/"); window.open("https://www.yahoo.com/"); window.open("https://www.in.com/"); but it did not open on new window? – Sudarshan Nov 20 '12 at 07:06
  • @SaurabhSaxena can you share code for chrome.tabs.create({"url":tablink});, it worked for me? – Sudarshan Nov 20 '12 at 07:08
  • @Sudarshan ... in which page you have used this code, like in backgroung page, background script or else – Saurabh Saxena Nov 20 '12 at 07:13
  • @Sudarshan ... have you tried running your code from within a loop – Saurabh Saxena Nov 20 '12 at 07:14
  • @SaurabhSaxena : It is in content scripts, yes i tried running it in loop; Show your code it speaks better. – Sudarshan Nov 20 '12 at 07:29
  • @Sudarshan ... you can check in the edit now – Saurabh Saxena Nov 20 '12 at 07:34
  • @SaurabhSaxena tried the same able to open 50 + windows here... – Sudarshan Nov 20 '12 at 07:40
  • @Sudarshan ... check my permissions value in manifest file. Is it correct? – Saurabh Saxena Nov 20 '12 at 07:49
  • @Sudarshan .. try reading urls from a file and then open them. – Saurabh Saxena Nov 20 '12 at 08:00
  • @SaurabhSaxena: Permission are good, if permissions have gone wrong you would have not seen a window at all;what difference does url(reading from file etc) for window.open() make; Can you try this code and let me know results; for(i=0;i<10;i++){ setTimeout(window.open("http://www.google.co.in/"),500); setTimeout(window.open("https://www.facebook.com/"),500); setTimeout(window.open("https://www.yahoo.com/"),500); setTimeout(window.open("https://www.gaana.com/"),500); setTimeout(window.open("https://www.youtube.com/"),500);} – Sudarshan Nov 20 '12 at 08:48
  • first two links opened in tabs and rest in different windows. Could it be a problem with chrome version? – Saurabh Saxena Nov 20 '12 at 10:09
  • I am on linux Ubuntu 10.04 LTS – Saurabh Saxena Nov 20 '12 at 11:19

3 Answers3

1

This seems to be a common error in JavaScript. setTimeout(window.open(tablink), 500); means to invoke what window.open returns after 500 milliseconds. The return value of window.open is typically a Window object, which makes setTimeout fail and your code stops executing. That's what caused the problem. Please use setTimeout(function(){window.open(tablink)}, 500); instead.

rubdottocom
  • 8,110
  • 10
  • 39
  • 59
方 觉
  • 4,042
  • 1
  • 24
  • 28
-1

I tried to open number of sites at the same time,

found that "pop-ups were blocked on that page "

you can see that in address-bar.

:)

ameya rote
  • 1,116
  • 1
  • 9
  • 13
-2

Got the solution, hit n trial rocks :)

I just removed setTimeout function and it works. I still don't why it was causing the problem.

for(var i = 0; i<links.length; i++)
  {
    var tablink = links[i];
    if(links[i] != "")
    {
            tablink = *"somedomain"* + tablink;
        window.open(tablink);  
    }
  }  
Saurabh Saxena
  • 3,005
  • 10
  • 31
  • 46