javascript - getter/setter for src attribute in js? -
are there way can set getters , setters of src attribute of htmlsourceelements? i'm thinking using security measures web app uses js other websites. "setters of src attribute of htmlsourceelements", mean setter should called on code like: somevideoelement.src = "/static/somevideo.mp4"
so far, i've tried:
htmlelement.prototype.__definegetter__("src", function () { console.log("getter called!"); debugger; }); htmlelement.prototype.__definesetter__("src", function (val) { debugger; }); //tested @ chrome, didn't yield logs (getters , setters not called)
and
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 @ chrome. called codes like: somevidelem.setattribute("src",someurl)
are there way this? or impossible? : )
__definegetter__
, __definesetter__
deprecated , possibly obsolete. object.defineproperty(parentobject, 'propname', {})
new way.
i couldn't work, maybe else can?
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); } });
edit: after bit of experimentation, should work if delete
src
property of every element need to. bit hacky, best do.
edit2: this, user still theoretically overwrite get/set functions. stop that, try removing configurable: true
(it default false). i'm not sure, past experience, seems can't redefine on instance.
Comments
Post a Comment