防止xulrunner中的浏览器位置更改
Preventing Browser Location Change in xulrunner
我一直在阅读和破解https://developer.mozilla.org/en/XUL_School/Intercepting_Page_Loads但似乎可以做我需要的事。
我正在开发Chromeless,试图防止主xulbrowser元素被导航离开,例如,链接不应该工作,window.location.href="http://www.example.com/"
也不应该工作。
我假设我可以通过browser.webProgress.addProgressListener
做到这一点,然后收听onProgressChange
,但我不知道如何区分资源请求和browser
更改位置(似乎onLocationChange
太晚了,因为文档已经被卸载)。
browser.webProgress.addProgressListener({
onLocationChange: function(){},
onStatusChange: function(){},
onStateChange: function(){},
onSecurityChange: function(){},
onProgressChange: function(){
aRequest.QueryInterface(Components.interfaces.nsIHttpChannel)
if( /* need to check if the object triggering the event is the xulbrowser */ ){
aRequest.cancel(Components.results.NS_BINDING_ABORTED);
}
},
QueryInterface: xpcom.utils.generateQI([Ci.nsIWebProgressListener, Ci.nsISupportsWeakReference])
}, wo._browser.webProgress.NOTIFY_ALL);
另一个听起来很有前景的选项是nsIContentPolicy.shouldLoad()
方法,但我真的不知道如何"创建一个扩展nsIContentPolicy的XPCOM组件,并使用nsICategoryManager将其注册到"内容策略"类别。"
有什么想法吗?
我从mozilla的#xulrunner irc频道得到了帮助。
得到的解决方案如下。
注意:这是Mozilla Chromeless中使用的模块,require("chrome")
和require("xpcom")
位在正常情况下不可用
const {Cc, Ci, Cu, Cm, Cr} = require("chrome");
const xpcom = require("xpcom");
/***********************************************************
class definition
***********************************************************/
var description = "Chromeless Policy XPCOM Component";
/* UID generated by http://www.famkruithof.net/uuid/uuidgen */
var classID = Components.ID("{2e946f14-72d5-42f3-95b7-4907c676cf2b}");
// I just made this up. Don't know if I'm supposed to do that.
var contractID = "@mozilla.org/chromeless-policy;1";
//class constructor
function ChromelessPolicy() {
//this.wrappedJSObject = this;
}
// class definition
var ChromelessPolicy = {
// properties required for XPCOM registration:
classDescription: description,
classID: classID,
contractID: contractID,
xpcom_categories: ["content-policy"],
// QueryInterface implementation
QueryInterface: xpcom.utils.generateQI([Ci.nsIContentPolicy,
Ci.nsIFactory, Ci.nsISupportsWeakReference]),
// ...component implementation...
shouldLoad : function(aContentType, aContentLocation, aRequestOrigin, aContext, aMimeTypeGuess, aExtra) {
let result = Ci.nsIContentPolicy.ACCEPT;
// only filter DOCUMENTs (not SUB_DOCUMENTs, like iframes)
if( aContentType === Ci.nsIContentPolicy["TYPE_DOCUMENT"]
// block http(s) protocols...
&& /^http(s):/.test(aContentLocation.spec) ){
// make sure we deny the request now
result = Ci.nsIContentPolicy.REJECT_REQUEST;
}
// continue loading...
return result;
},
createInstance: function(outer, iid) {
if (outer)
throw Cr.NS_ERROR_NO_AGGREGATION;
return this.QueryInterface(iid);
}
};
let registrar = Cm.QueryInterface(Ci.nsIComponentRegistrar);
try
{
Cm.nsIComponentRegistrar.registerFactory(classID, description, contractID, ChromelessPolicy);
}
catch (e) {
// Don't stop on errors - the factory might already be registered
Cu.reportError(e);
}
const categoryManager = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager);
for each (let category in ChromelessPolicy.xpcom_categories) {
categoryManager.addCategoryEntry(category, ChromelessPolicy.classDescription, ChromelessPolicy.contractID, false, true);
}
为感兴趣的人在github上请求:https://github.com/mozilla/chromeless/pull/114
相关文章:
- 当浏览器位置更改时重新渲染React.js
- 如何在浏览器重新加载时保存位置哈希状态
- Jquery Draggable-在浏览器刷新时保持位置
- 如何在调整浏览器大小时将图像保持在适当位置
- 浏览器持续滚动位置
- 如何防止JavaScript无意中更改浏览器位置
- 根据浏览器位置,用JavaScript将输入的日期转换为UTC
- 控件的浏览器视点位置
- 滚动位置 (scrollTop) 硬件是否在浏览器中加速
- 移动浏览器、地理位置和自动填充地址
- Javascript或浏览器以不同的方式设置位置
- 如何使用Google Places Javascript API搜索附近的位置(自动从浏览器获取坐标)
- 在火狐浏览器的画布上获取鼠标位置
- Android默认浏览器:谷歌地图制作“;位置:相对;元素在页面顶部滚动
- 为什么浏览器可以'在`.中找不到js文件/lib/`(相对)位置
- 允许HTML5 web应用程序在浏览器关闭时访问位置'以t为中心(iOS)
- 固定位置高度比浏览器窗口大百分比
- 阻止浏览器在按下后退按钮时捕捉到上一个滚动位置
- 有没有办法防止浏览器缓存滚动位置和缩放级别等值
- 自动完成列表固定位置浏览器调整大小