0

我在 Firefox 扩展和页面内容之间进行了一些事件调度/侦听。页面内容代码被注入到扩展的 on-content-page-load 处理程序中。

有没有办法可以检查当前代码运行的模式(特权模式或非特权模式)?

4

2 回答 2

2

典型的方法是检查您是否可以访问Components.classes

try {
  Components.classes;
  alert("Yay! Privileged code.");
}
catch (e) {
  alert("Oops... Content privileges only.");
}
于 2013-05-24T06:44:11.930 回答
0

取决于环境:

在 addon-sdk 插件中,您可以检查requireor exports

if(typeof(require)!=="undefined" && typeof(exports)!=="undefined"){
  //privileged
}

(注意:typeof 用于在检查变量是否存在时防止错误。)


否则按照弗拉基米尔的建议检查Components(or )。Cc, Ci, Cu


甚至检查 xul:window 元素是否存在:

var xulWindow=document.querySelector("window#main-window[windowtype='navigator:browser']");
if(xulWindow){
  //privileged. in this case looking specifically for "navigator:browser" windowtype.
}
于 2013-05-25T21:11:46.180 回答