【问题标题】:Show alert dialog in chrome extension在 chrome 扩展中显示警报对话框
【发布时间】:2020-04-02 15:08:40
【问题描述】:

当用户点击我的扩展图标时,我想显示一个简单的alert。我试过这段代码:

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

这是我的manifest

{
  "manifest_version": 2,

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

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

如何显示警报onClick 事件?

【问题讨论】:

  • 试试addListener(function() { alert(1) })
  • @thg435 没用
  • “没用?”试着给它一拳。
  • @thg435 打孔:添加到清单“背景”:{“脚本”:[“myScript.js”]}

标签: javascript google-chrome google-chrome-extension


【解决方案1】:

更新:

根据documentation 是这样的:

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();

【讨论】:

  • 感谢您的示例,我错过了清单中的这一行:"background": { "scripts": ["myScript.js"] }。现在它可以工作了。
  • 清单在哪里?我明白了:清单不是有效的 JSON。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 2020-01-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多