Greasemonkey检测隐私浏览模式?

时间:2014-02-18 00:47:11

标签: javascript greasemonkey

如果当前在Firefox私密浏览窗口中运行,我需要使我的Greasemonkey脚本的行为有所不同。是否可以从Greasemonkey中检测到这一点?如果没有,那么是否可以在私密浏览模式下完全不运行?

编辑:我想做的一个原因是,通常脚本会发出AJAX请求,其中包括有关被访问页面的信息,服务器端可能会存储该信息(在正常模式下浏览时可以)。但是,如果用户处于私人浏览状态,我不希望服务器端拥有用户正在访问该页面的信息,因此我希望在这种情况下不要提出这些请求。

2 个答案:

答案 0 :(得分:1)

在Firefox的插件中,您可以检测浏览器是否处于私密浏览模式,以下代码取自Mozilla's developer docs。但是,这只是一个内部API,只能从插件中访问,而不能从网站或第三方脚本中访问。

没有保证可以帮助您,因为我不确定Grease Monkey是否在Firefox中公开了Components API以便在GM脚本中使用。初步搜索似乎没有出现这种情况。

try {
    var pbs = Components.classes["@mozilla.org/privatebrowsing;1"]
                    .getService(Components.interfaces.nsIPrivateBrowsingService);

    var inPrivateBrowsingMode = pbs.privateBrowsingEnabled;

    if (!inPrivateBrowsingMode) {
    /* save private information */
    }
} catch(e){
  alert("Error!");
}  

答案 1 :(得分:0)

此功能自Greasemonkey 3.8 - https://github.com/greasemonkey/greasemonkey/issues/2199

实施

您可以使用GM_info.isPrivate检查用户脚本是否在私有模式窗口中运行。