最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Show alert dialog in chrome extension - Stack Overflow

programmeradmin3浏览0评论

I want to display a simple alert when user clicks on my extension icon. I have tried this code :

chrome.browserAction.onClicked.addListener(
    alert(1)
);

Here is my manifest :

{
  "manifest_version": 2,

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

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

How do I show an alert onClick event ?

I want to display a simple alert when user clicks on my extension icon. I have tried this code :

chrome.browserAction.onClicked.addListener(
    alert(1)
);

Here is my manifest :

{
  "manifest_version": 2,

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

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

How do I show an alert onClick event ?

Share Improve this question edited Jan 14, 2019 at 10:15 Miraj50 4,4071 gold badge23 silver badges36 bronze badges asked Oct 4, 2013 at 9:01 Mickey TinMickey Tin 3,53310 gold badges44 silver badges72 bronze badges 4
  • 1 try addListener(function() { alert(1) }) – georg Commented Oct 4, 2013 at 9:02
  • @thg435 it did not work – Mickey Tin Commented Oct 4, 2013 at 9:07
  • "Didn't work?" Try giving it a punch. – georg Commented Oct 4, 2013 at 9:16
  • 1 @thg435 Punch :add to manifest "background": { "scripts": ["myScript.js"] } – Mickey Tin Commented Oct 4, 2013 at 9:27
Add a comment  | 

1 Answer 1

Reset to default 15

updated:

According the browserAction documentation it is like:

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

and here is the 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)

  1. 暂无评论