有什么方法可以设置所有HTMLSourceElements的getter和src属性的setter?我正在考虑将此作为我的Web应用程序的额外安全措施,该Web应用程序使用其他网站的JS。通过“所有HTMLSourceElement的src属性的设置者”,我的意思是应该在类似以下代码的代码上调用setter:SomeVideoElement.src =“ /static/somevideo.mp4”
到目前为止,我已经尝试过:
HTMLElement.prototype.__defineGetter__("src", function () {
console.log("getter called!");
debugger;
});
HTMLElement.prototype.__defineSetter__("src", function (val) {
debugger;
});
//tested at chrome, didn't yield any logs (getters and setters not called)
和
HTMLSourceElement.prototype._setAttribute = HTMLSourceElement.prototype.setAttribute;
HTMLSourceElement.prototype._getAttribute = HTMLSourceElement.prototype.getAttribute;
HTMLSourceElement.prototype.setAttribute = function(){
console.log("HTMLSourceElement.setAttribute called!");
debugger;
HTMLSourceElement.prototype._setAttribute.apply(this, arguments);
}
//tested at chrome. Called only for codes like: SomeVidElem.setAttribute("src",someurl)
有什么办法吗?还是这根本不可能?谢谢 : )
__defineGetter__
并且__defineSetter__
已过时,并且可能已过时。Object.defineProperty(parentObject, 'propName', {})
是新方法。
我无法使其正常工作,但也许其他人可以?
Object.defineProperty(HTMLSourceElement.prototype, 'src', {
enumerable: true,
configurable: true,
get: function(){
return this.getAttribute('src')
},
set: function(newval){
console.log('being set');
this.setAttribute('src',newval);
}
});
编辑:经过一些试验,如果您然后需要的每个元素delete
的src
属性,这应该工作。有点hacky,但我能做的最好。
EDIT2:这样,用户在理论上仍然可以覆盖您的get / set函数。要停止此操作,请尝试删除configurable: true
(默认为false)。我不确定,但是根据过去的经验,似乎他们甚至无法在实例上重新定义它。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句