I'm trying to build a Tampermonkey user script that modifies the functionality of an event applied to an element.
Let's say the source website have this:
<div id="item_1">…</div>
And within the source website's Javascript:
document.getElementById("item_1").addeventlistener("click", function(){
// Source website's lines of code for the function to be applied on click event occurrence.
}
I know that I can easily overwrite the event functionality by using:
document.getElementById("item_1").addeventlistener("click", function(){
// My lines of code here
}
But what I'm looking for is to first copy the anonymous function
that was previously passed-in to the addEventListener
method. I want to copy it to a temporary variable so that I can retrieve the original Event Listener attached function back.
There's another question having an answer telling that copying functions of Event Listeners is impossible. I believe this may be possible somehow as I think that the passed-in anonymous function is stored within the heap with some reference. If I'm able to get that reference, I will be able to get a copy for the code to another variable.
I don't have access to edit the source's script. I know that I could have refactored the code to handle event population. Also, I know I can do some manipulation on the source's JavaScript, but I don't want to go this way as my user script code will get too complicated and if the source code was changed a little, my code will be broken easily. I want to easily copy the Event Listener function to a variable I have.