Sign in to follow this  
Followers 0
Kreative

How to expose and isolate the getters/functions that define window innerWidth and innerHeight in Chrome?

4 posts in this topic

Hello,

I would like to define non-replaceable references/getters to the innerWidth and innerHeight properties of window, or whatever they reference and are updated by. This is because currently, I can very easily re-assign, delete or re-define these properties to any other value making them unreliable if they were accidentally tampered with.

Research
In Mozilla, Object.getOwnPropertyDescriptor(window, x) where x is innerWidth or innerHeight, returns a descriptor of a configurable and enumerable property with a getter and setter containing functions that I can retrieve, bind to window and isolate.

However, in Chrome, the function returns a descriptor of a property that does not contain a getter or setter, but rather a value, that magically updates when the inner window is resized given that the property has never been manually re-assigned, re-defined or deleted.

Reading Mozilla Developer Network, I have found the specification that describes these two properties is that of the CSS Object Model ( link: https://drafts.csswg.org/cssom-view/#dom-window-innerwidth ). In this specification I have located the `partial interface Window` which defines these view-port properties as `[Replaceable] readonly attribute long` where the [Replaceable] means that the property can be re-assigned and will continue to indefinitely reference the new re-assigned value whilst keeping the same property name, despite it being read-only.

This explains the behavior I have seen in Chrome, however there seems to be no means to extract a function or generate a reference that I could potentially hide in a closure or a block and (not 100% but mostly) reliably use it as the true width and height of the inner-window in pixels.

Seeing as I can expose and isolate a getter in Mozilla, is there any (perhaps other) way to utilise a slightly safer means of getting the innerWidth and innerHeight of window? Because in terms of JavaScript, the two properties seem to be magical in how they update as doing something like Object.defineProperty(window, 'innerWidth', Object.assign(Object.getOwnPropertyDescriptor(window, 'innerWidth'), {writable: false, configurable: false}) does the same thing as re-assigning and then freezing the property afterward; nothing.

Kind Regards, Augustas.

Edited by Kreative

Share this post


Link to post
Share on other sites

It seems like Firefox doesn't treat it as a read-only property, despite the documentation. Chrome returns a value property without a getter and setter, which is what it sounds like it should do if it's a read-only non-accessor descriptor. Firefox returns a descriptor with a getter and setter but no value, which is the opposite. Their documentation doesn't seem that consistent either. On the MDN page for innerHeight here:

 

https://developer.mozilla.org/en-US/docs/Web/API/Window/innerHeight

 

Under the Value heading it does say that the property is read-only. But in the list of properties for the Window object here:

 

https://developer.mozilla.org/en-US/docs/Web/API/Window

 

Both innerHeight and innerWidth are not marked as read-only.

 

What problem are you trying to solve though? If you want to read the value of innerWidth or innerHeight for the window you can at any time, what else are you missing?

Share this post


Link to post
Share on other sites

Is it possible to generate a reference or obtain some function that I can hide, that gives innerWidth and innerHeight even if window.innerWidth and/or innerHeight have been replaced? Maybe this could be rephrased as: can the properties be made non-replaceable?

Share this post


Link to post
Share on other sites

If the browser doesn't natively do it, there's no way to protect the property. I would operate under the assumption that nobody's messing with window properties.

 

If it's your own code that's overriding the property then fix it. If you're dealing with somebody else's code that does that, you'll have to either rewrite it or stop using it.

 

I cannot imagine a situation where any developer would override these properties, if they're doing so, their code is not worth using.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0