【问题标题】:Chrome extension API for alarms has method clear that is not working as per documentation用于警报的 Chrome 扩展 API 的方法 clear 不能按照文档工作
【发布时间】:2014-03-27 15:58:30
【问题描述】:

Chrome 版本为 33.0.1750.154 m。

根据http://developer.chrome.com/extensions/alarms#method-clear 的文档,clear 方法的签名是:chrome.alarms.clear(string name, function callback)。

我的扩展中有一个仍在开发中的警报:

chrome.alarms.get('refreshForNotification', function(alarm){
    console.log('>>>' + JSON.stringify(alarm));
});
>>>{"name":"refreshForNotification","periodInMinutes":2,"scheduledTime":1395892890429.581} 

现在,当我尝试使用以下命令清除此警报时:

chrome.alarms.clear('refreshForNotification', function(wasCleared){
    console.log('>>> wasCleared: ' + wasCleared);
});

我收到以下错误:

Error: Invocation of form alarms.clear(string, function) doesn't match definition alarms.clear(optional string name)
 message: "Invocation of form alarms.clear(string, function) doesn't match definition alarms.clear(optional string name)"

有人可以指导/指出我这里出了什么问题吗?根据文档,警报 api 从 chrome 22 开始是稳定的。如果我的代码没有错,那么要么文档太旧,要么文档太新,并且我的 chrome 上的当前行为将来会发生变化。

任何提示/帮助都会很有用。

谢谢

【问题讨论】:

  • 这个callback 功能是在 7 天前添加的:crrev.com/258526 如果你想使用这个功能,那么你必须使用 Chrome 的开发者/金丝雀版本。如果您处于稳定状态,则必须等待大约 12 周。
  • 感谢您提供的信息!我希望 stackoverflow 允许我接受此评论作为解决方案。

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


【解决方案1】:

chrome.alarms.clear 的可选回调是上周在http://crrev.com/258526 (Chrome 35.0.1903.0+) 中添加的。在线文档显示了最新(开发)版本的可用功能,而不是稳定版本。

Chrome 35 目前在developer channel 上可用,所以如果你真的想使用这个功能,那么你可以从here 安装 Chrome。

【讨论】:

    【解决方案2】:

    这对我来说在 35.0.1905.3 上很好用:

    manifest.json

    {
      "name": "22692926 Example",
      "description": "alarms",
      "version": "1.0",
      "manifest_version": 2,
      "permissions": ["alarms"],
      "background": {
        "scripts": ["background.js"],
        "persistent": false
      }
    }
    

    background.js

    var NAME = "foo";
    var alarmInfo = {
      'delayInMinutes': 1
    };
    
    chrome.alarms.create(NAME, alarmInfo);
    chrome.alarms.get(NAME, function(a) { console.log(a); });
    chrome.alarms.clear(NAME, function(wasCleared) { console.log(wasCleared); });
    

    输出

    Object {name: "foo", ...}
    true
    

    我知道这并不能直接回答您的问题,但也许您可以制作一个完全复制此代码的测试扩展,如果它有效,您可以将它与您的进行比较并找出不同之处。

    【讨论】:

      猜你喜欢
      • 2012-05-23
      • 1970-01-01
      • 2019-07-24
      • 1970-01-01
      • 1970-01-01
      • 2014-02-15
      • 1970-01-01
      • 2013-02-18
      • 2012-11-25
      相关资源
      最近更新 更多