2013-10-04 52 views
5

我想在用戶單擊我的擴展名圖標時顯示簡單的alert。我曾嘗試這樣的代碼:chrome.browserAction.onClicked.addListener( alert(1) );, 這裏是我manifest在Chrome擴展中顯示警告對話框

{ 
    "manifest_version": 2, 

    "name": "sample", 
    "description": "des", 
    "version": "1.0", 

    "browser_action": { 
    "default_icon": "icon.png" 
    }, 
    "permissions": [ 
    ] 
} 

如何顯示警報onClick事件?

+1

嘗試'的addListener(函數(){警報(1) })' – georg

+0

@ thg435它沒有工作 –

+0

「沒有工作?」嘗試給它一拳。 – georg

回答

6

更新:

根據documention它是這樣的:

chrome.browserAction.onClicked.addListener(function() { 
    alert('Hello, World!'); 
}) 

這裏是sample from Google (zip-file)

// Copyright (c) 2011 The Chromium Authors. All rights reserved. 
// Use of this source code is governed by a BSD-style license that can be 
// found in the LICENSE file. 

var min = 1; 
var max = 5; 
var current = min; 

function updateIcon() { 
    chrome.browserAction.setIcon({path:"icon" + current + ".png"}); 
    current++; 

    if (current > max) 
    current = min; 
} 

chrome.browserAction.onClicked.addListener(updateIcon); 
updateIcon(); 
+0

不工作,是我應該有一個完整的代碼? –

+0

@MickeyTin它應該工作! – CodeGroover

+1

感謝您的示例,我錯過了清單中的這一行:'「background」:{「scripts」:[「myScript.js」]}'。現在它可以工作。 –