我在 Firefox 扩展和页面内容之间进行了一些事件调度/侦听。页面内容代码被注入到扩展的 on-content-page-load 处理程序中。
有没有办法可以检查当前代码运行的模式(特权模式或非特权模式)?
我在 Firefox 扩展和页面内容之间进行了一些事件调度/侦听。页面内容代码被注入到扩展的 on-content-page-load 处理程序中。
有没有办法可以检查当前代码运行的模式(特权模式或非特权模式)?
典型的方法是检查您是否可以访问Components.classes
:
try {
Components.classes;
alert("Yay! Privileged code.");
}
catch (e) {
alert("Oops... Content privileges only.");
}
取决于环境:
在 addon-sdk 插件中,您可以检查require
or 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.
}