luciopaiva / witchcraft

Inject Javascript and CSS right from your file system. Think GreaseMonkey for more advanced users.
https://luciopaiva.com/witchcraft
MIT License
254 stars 18 forks source link

getOwnPropertyDescriptor() not working only from within witchcraft #14

Closed nickmeldrum closed 4 years ago

nickmeldrum commented 5 years ago

any idea why the following javascript:

Object.getOwnPropertyDescriptor(inputElement, 'value')

returns null when run from a witchcraft script, but returns the valid property descriptor when run in the console window?

luciopaiva commented 5 years ago

How are you obtaining inputElement? Can you post the rest of the code?

nickmeldrum commented 5 years ago

It's a good question.

When I use the following code to try to reproduce this on a public website (e.g. https://google.co.uk) it works both in the console AND in witchcraft:

setTimeout(() => {
    const inputElement = document.getElementsByClassName('gLFyf')[0];
    const valueSetter = Object.getOwnPropertyDescriptor(inputElement.constructor.prototype, 'value').set;
    console.log('hello world', valueSetter);
}, 500);

NOTE HOWEVER: instead of using inputElement by itself, I had to use inputElement.constructor.prototype in both cases to make it work.

What is weird is that the same is not true for the (not publicly available) website that I am trying the same code on.

In the console I can just use inputElement directly instead of having to use the prototype.

My personal issue is fixed, as I can just use the prototype in my witchcraft script, but I am still stumped as to why in this particular website, it does seem to act differently still. I will try to get a publicly available repro case if I can.

luciopaiva commented 5 years ago

Very weird behavior indeed. Sure, let me know of any further findings. I can take a look as well when I find some time. I'll leave this ticket open until then.

luciopaiva commented 4 years ago

So I played with getOwnPropertyDescriptor() a bit and my conclusion is that value is really owned by the input's prototype, so it makes sense that the return value for your input element is undefined. Not sure how you got it to be something else than that, even when run in the console window. I always get undefined.

I created pen to play with it: https://codepen.io/luciopaiva/pen/GRKPjBP?editors=1010

Let me know if you are able to reproduce what you saw there and then we can further discuss it. For now, I'm closing this issue since it's most likely not related to Witchcraft.