【问题标题】:I am developing a Google Chrome Extension and I need to limit the usage of the extension. What's the best way to do that?我正在开发 Google Chrome 扩展程序,我需要限制扩展程序的使用。最好的方法是什么?
【发布时间】:2016-04-18 11:20:36
【问题描述】:

我不想使用服务,并希望让应用程序在本地确定它何时使用了 X 小时(连续使用或部分使用)。在那之后,我想终止这些功能,只显示一个错误,比如“quote over”之类的。

【问题讨论】:

  • 你如何定义你的扩展程序的使用?只要安装好?

标签: google-chrome google-chrome-extension


【解决方案1】:

一个基本的想法是计算每个浏览器启动/关闭事件之间的持续时间,同时在每次浏览器启动时启动一个计时器,间隔等于MAX_TIME - TIME_USED

但是,根据这里的描述,Event onBrowserClose for Google Chrome Extension? 没有onBrowserClose 事件,如果使用background 权限,Chrome 将在用户登录计算机后立即运行(不可见),它也会使 Chrome 继续运行运行直到用户明确退出 chrome。我认为这不是一个好方法。

我认为以下可能是一种解决方法,虽然不是那么完美

您可以启动一个小间隔的计时器(取决于您希望用户使用多少小时,我相信 1 分钟可能是一个不错的选择)。代码 sn-ps 看起来像

manifest.json

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

background.js

setInterval(function () {
    chrome.storage.local.get("usageInMinutes", function (result) {
        var usageInMinutes = result.usageInMinutes;
        if (typeof usageInMinutes === "undefined") {
            chrome.storage.local.set({ usageInMinutes: 1 });
        } else {
            usageInMinutes += 1;
            if (usageInMinutes >= MAX_USAGE_IN_MINUTES) {
                // Your usage has expired
            } else {
                chrome.storage.local.set({ usageInMinutes: usageInMinutes });
            }
        }
    });
}, 1000 * 60);

【讨论】:

    猜你喜欢
    • 2018-09-24
    • 1970-01-01
    • 2012-09-16
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 2018-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多