首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >使用Firefox/OS X检测锁屏或运行屏幕保护程序

使用Firefox/OS X检测锁屏或运行屏幕保护程序
EN

Stack Overflow用户
提问于 2013-11-01 22:45:20
回答 1查看 1.1K关注 0票数 1

我正在为Firefox (SDK插件)创建一个扩展,其中我需要检测屏幕保护程序和锁屏事件,以便我可以在web应用程序中设置用户的可用性状态。

我已经成功地在Windows上做到了这一点,现在需要移植到OS X。对于Windows版本,我使用对本地API的调用来确定屏幕是否被锁定,等等。是否有类似的方法从OS X上的Firefox扩展中获取操作系统信息?我试着用谷歌搜索这个,但没有找到一个可靠的答案-任何帮助感谢!

EN

回答 1

Stack Overflow用户

发布于 2013-11-05 12:18:28

在OSX上,您可以使用CGSessionCopyCurrentDictionary查询锁定屏幕/屏幕保护程序,并查找"CGSSessionScreenIsLocked"键的存在和值。

这是平台API,所以必须再次使用js-ctype并编写一堆代码来使其正常工作。

我确实让它工作了:下面的代码是一个可以在特权Scratchpad中运行的工作示例。要获得特权版本,请打开pad,例如about:newtab

代码语言:javascript
复制
Components.utils.import("resource://gre/modules/ctypes.jsm");

var CoreFoundation = new (function() {
    this.CFNumberRef = ctypes.voidptr_t;
    this.CFStringRef = ctypes.voidptr_t;
    this.CFDictionaryRef = ctypes.voidptr_t;

    var lib = ctypes.open("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation");
    this.CFRelease = lib.declare(
        "CFRelease",
        ctypes.default_abi,
        ctypes.void_t,
        ctypes.voidptr_t);

    var CFStringCreateWithCharacters = lib.declare(
        "CFStringCreateWithCharacters",
        ctypes.default_abi,
        this.CFStringRef,
        ctypes.voidptr_t,
        ctypes.jschar.ptr,
        ctypes.int32_t);
    this.CFStringCreateWithCharacters = function(str) {
        var rv = CFStringCreateWithCharacters(null, str, str.length);
        if (!rv || rv.isNull()) {
            return null;
        }
        return ctypes.CDataFinalizer(rv, this.CFRelease);
    };


    var CFDictionaryGetValue = lib.declare(
        "CFDictionaryGetValue",
        ctypes.default_abi,
        this.CFNumberRef,
        this.CFDictionaryRef,
        this.CFStringRef);
    this.CFDictionaryGetInt = function(dict, str) {
        var rv = CFDictionaryGetValue(dict, this.CFStringCreateWithCharacters(str));
        if (!rv || rv.isNull()) {
            return null;
        };
        return this.CFNumberGetValue(rv);
    };

    var CFNumberGetValue = lib.declare(
        "CFNumberGetValue",
        ctypes.default_abi,
        ctypes.bool,
        this.CFNumberRef,
        ctypes.int32_t,
        ctypes.int32_t.ptr);
    this.CFNumberGetValue = function(num) {
        var rv = new ctypes.int32_t();
        CFNumberGetValue(num, 3, rv.address());
        console.log("CFNumberGetValue", rv, rv.value);
        return rv.value;
    };
    this.close = function() {
        lib.close();
    };
})();
var ApplicationServices = new (function() {
    var lib = ctypes.open("/System/Library/Frameworks/ApplicationServices.framework/ApplicationServices");

    var CGSessionCopyCurrentDictionary = lib.declare(
        "CGSessionCopyCurrentDictionary",
        ctypes.default_abi,
        CoreFoundation.CFDictionaryRef);
    this.CGSessionCopyCurrentDictionary = function() {
        var rv = CGSessionCopyCurrentDictionary();
        if (!rv || rv.isNull()) {
            return null;
        }
        return ctypes.CDataFinalizer(rv, CoreFoundation.CFRelease);
    };

    this.close = function() {
        lib.close();
    };
})();

setInterval(function() {
    var dict = ApplicationServices.CGSessionCopyCurrentDictionary();
    if (dict) {
        var locked = CoreFoundation.CFDictionaryGetInt(dict, "CGSSessionScreenIsLocked");
        console.log("rv", locked);
        if (locked) {
            // do something;
        }
    }
}, 500);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19729071

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档