javascript - chrome.tabs.query- Url Specification - Stack Overflow

I'm making an extension for Pandora and when the user clicks the extension icon (In the top right)

I'm making an extension for Pandora and when the user clicks the extension icon (In the top right), the extension opens a new tab with pandora. What i'm trying to avoid is the user pressing the button multiple times and opening multiple tabs that will all play music at once.

This is what I have so far:

// When extension is clicked
chrome.browserAction.onClicked.addListener(function(tab) {

    // checks to see if pandora tab is already open. if it's already open,
    // the script returns (Or atleast thats what I want it to do)

    chrome.tabs.query({url: "/*"}, function(results) {
        return;
    });

    // injects the javascript
    chrome.tabs.executeScript(null,{file: "buy.js"});
});

I use google's Developer Site as a reference in making this and my manifest page does have tab permissions.

I'm making an extension for Pandora and when the user clicks the extension icon (In the top right), the extension opens a new tab with pandora.. What i'm trying to avoid is the user pressing the button multiple times and opening multiple tabs that will all play music at once.

This is what I have so far:

// When extension is clicked
chrome.browserAction.onClicked.addListener(function(tab) {

    // checks to see if pandora tab is already open. if it's already open,
    // the script returns (Or atleast thats what I want it to do)

    chrome.tabs.query({url: "http://www.pandora./*"}, function(results) {
        return;
    });

    // injects the javascript
    chrome.tabs.executeScript(null,{file: "buy.js"});
});

I use google's Developer Site as a reference in making this and my manifest page does have tab permissions.

Share Improve this question edited Mar 9, 2014 at 22:52 Jeffrey Yasskin 5,7423 gold badges29 silver badges42 bronze badges asked Mar 9, 2014 at 14:56 JordanJordan 4952 gold badges10 silver badges22 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 3

You'll need at least host permission for http://www.pandora. in order to query tabs visiting it. Then you want something like:

chrome.tabs.query({url: "http://www.pandora./*"}, function(results) {
    if (results.length == 0) {
        chrome.tabs.create({url: 'http://www.pandora./'}, function(tab) {
            chrome.tabs.executeScript(tab.id,{file: "buy.js"});
        });
    }
});

in order to create the tab after learning whether there's an existing tab, and then to inject the script after the new tab's created.

You'll do something similar to inject script when there is an existing Pandora tab.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745113921a4612012.html

相关推荐

  • javascript - chrome.tabs.query- Url Specification - Stack Overflow

    I'm making an extension for Pandora and when the user clicks the extension icon (In the top right)

    8小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信