使用Firefox / OS X检测锁定屏幕或正在运行的屏幕保护程序
Detect lock screen or running screensaver with Firefox/OS X
我正在为 Firefox 创建一个扩展(SDK 附加组件),我需要在其中检测屏幕保护程序和锁屏事件,以便我可以在 Web 应用程序中设置用户的可用性状态。
我已经设法为Windows执行此操作,现在需要移植到OS X。对于 Windows 版本,我使用对本机 API 的调用来确定屏幕是否被锁定等。有没有类似的方法可以从OS X上的Firefox扩展获取操作系统信息?我尝试过谷歌搜索这个,但没有找到一个可靠的答案 - 任何帮助感谢!
在 OSX 上,您可以使用 CGSessionCopyCurrentDictionary
查询锁定的屏幕/屏幕保护程序,并查找"CGSSessionScreenIsLocked"
键的存在和值。
这是平台API,所以人们将不得不再次使用js-ctypes并编写一堆代码来使其工作。
我确实让它工作了:以下代码是一个工作示例,您可以在特权暂存器中运行。要获得特权,请打开一个垫子,例如 about:newtab
.
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);
相关文章:
- ng应用程序使脚本无限运行
- 仅为一个窗体运行应用程序脚本
- 如何在没有单词的情况下从命令行运行Node.js程序'节点'
- Meteor应用程序无法运行-对象#<编译器>没有方法'主机'
- 检查在使用节点运行程序时是否给出ARGV
- 在浏览器上显示实习运行程序功能测试的结果
- 如何正确地包含jasmine节点测试运行程序的源文件
- 如何使用Karma单元测试运行程序捕获console.error
- 是否有可能通过单击html页面上的按钮在我的Web服务器上运行c ++程序
- Spotify应用程序的推荐单元测试框架/测试运行程序
- 放肆的控制台运行程序无声失败
- 用Javascript在tomcat服务器上运行程序,可以吗?
- Windows脚本主机查找/运行程序的变量路径使用正则表达式
- TheIntern.IO测试运行程序实习生客户端问题“;试图要求卸载的模块superagent”
- 从子帧运行程序调用函数
- 使用url运行程序
- 任何使用grunt-contrib-jasmine输出规范运行程序的方法
- 开始使用Underscore.js(一个简单的运行程序)
- Node.js-无法读取属性'toString'在Windows上运行程序包时为null
- JS测试运行程序html与生产html