0
votes

i have a chrome extension that open a window using window.open, which is in a test.js file other than background.js i am not able to use chrome.windows.create(); method directly in test.js since it fires error "Uncaught TypeError: Cannot call method 'create' of undefined" so i use window.open('mysite.com'); chrome browser does not allow to open popup. its says popups are blocked. user have to allow the the popup for that site. Is there any way by code through i can allow popup for a particular site? or Any way i can use chrome.windows.create(); in any js file other than background.js ?

1
If test.js is a Content Script, you can send a message to your background page to open the popup for you. - rsanchez
@sunilrxg: Did you try my answer below ? Did it work for you ? - gkalpak

1 Answers

0
votes

You can't programmatically allow popups (afaik), but you can use (indirectly) chrome.windows.create().

Since chrome.windows is only available in the background-page, you need to utilize Message Passing, in order to communicate between the content-script and the background-page:

In content-script:

/* Instead of `window.open(someURL);` */
chrome.runtime.sendMessage({
    action: openWindow,
    url: someURL
});

In background-page:

/* Listen for and respond to messages from content-scripts */
chrome.runtime.onMessage.addListener(function(msg, sender) {
    if ((msg.action === 'openWindow') && (msg.url !== undefined)) {
        chrome.windows.create({ url: msg.url });
    }
});

(Of course, you can specify other parameters as well, besides the URL.)