简体   繁体   中英

Do Google Chrome extensions work in popup windows opened with Javascript?

So I'm working on creating an extension that'll need to work within a popup window that is triggered by my website... Standard Javascript...

function openwin(){
    window.open('http://stackoverflow.com','myname','height=100,width=100');
}

Now I need my extension to load within that window. Let's say the extension's purpose is to just alert a message, when I load that window the background.js should fire and do that, correct?

chrome.windows.create(object createData, function callback)

Creates (opens) a new browser with any optional sizing, position or default URL provided.

From: http://code.google.com/chrome/extensions/windows.html#method-create

Try this example:

var props = {
    url: "http://www.stackoverflow.com",
    height: "100",
    width: "100",
    type: "popup"
}

chrome.windows.create(props, function(windowObj){
    console.log("Here's the window object.");
    console.dir(windowObj);
});

Here's my answer after looking through the API. As I said, it checks each new window being created and checks the parameters of that window. Here's the code, which can be added to the background.js file of your extension:

chrome.windows.onCreated.addListener(function(win){
    chrome.windows.get(win.id,{populate:true},function(tabwin){
        setTimeout(function(){
            chrome.tabs.executeScript(tabwin.tabs[0].id,{code:"alert(JSON.stringify(window));",runAt:'document_idle'});
        },500);
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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