2014-02-23 56 views
1

這裏是我的manifest.json瀏覽器擴展程序來執行,而不是點擊時

{ 
    "manifest_version": 2, 

    "name": "PageEscape", 
    "version": "1.0", 
    "description": "", 

    "background": { 
     "scripts": ["background.js"] 
    }, 

    "browser_action": { 
     "default_icon": "escape_icon.png" 
    } 
} 

這是我background.js

chrome.browserAction.onClicked.addListener(window.open("url", "_blank")) 

我的目標是爲用戶點擊Chrome擴展隨時希望快速切換到另一個頁面(我現在選擇的),但問題是,只要啓用擴展,它就會進入我選擇的網頁,並單擊該擴展的任何內容都不會執行任何操作。我能在這裏更改,使這項工作

回答

1
chrome.browserAction.onClicked.addListener(function() { 
    window.open("url", "_blank"); 
}); 
0

嘗試這樣的:

var url = 'http://www.example.com/' 
chrome.browserAction.onClicked.addListener(function(tab) { 
    openPage(url); 
}); 

/** 
* Open the url specified page, there are three cases: 
* 1. If the page has opened, select it; 
* 2. Else if here is a new tab, open the page in the new tab; 
* 3. Else open a new tab in the current window. 
* 
* @param url 
*/ 
function openPage(url) { 
    chrome.tabs.query(function (tabs) { 
    var newTab = null; 

    // search all tabs 
    for (var i = 0; i < tabs.length; i++) { 
     var tab = tabs[i]; 

     if (tab.url === url) { 
     chrome.tabs.update(tab.id, {url: url, selected: true}); 
     return; 
     } else if (tab.url === 'chrome://newtab/') { 
     newTab = tab; 
     } 
    } 

    if (newTab) { 
     chrome.tabs.update(newTab.id, {url: url, selected: true}); 
    } else { 
     chrome.windows.getCurrent(function (win) { 
     var winId = win.id; 
     chrome.tabs.create({windowId: winId, url: url}); 
     }); 
    } 
    }); 
} 
相關問題