-2

i am trying to change a js function (within the HTML) or add an event listener on one when it is used (from other parts from the site).

replaceing the js object by changing the innerHTML won't work even when i set it to "" i still can access the function. Eventlistener seems not to work with these either.

Is there any way to change that?

the script i want to change:

   <head><title></title>
<script language="JavaScript">
    function add(message) {
            alert(message);
    }
</script></head>

edit:changed the script

4
  • Where is the source code to go with your question? "(within the HTML)" What HTML? You haven't displayed any source code so nothing to work with/debug.... Commented Jan 13, 2017 at 20:27
  • Now you have added some source code.. what do you want to change that function to? Where are the other relevant elements? What is triggering/calling that function? What are message, scrolltag and where is the container element... Commented Jan 13, 2017 at 20:40
  • @NewToJS just changed the script really doesn't matter whats in the function when i can access it Commented Jan 13, 2017 at 20:54
  • 1
    Possible duplicate of Injecting JS functions into the page from a Greasemonkey script on Chrome Commented Jan 13, 2017 at 21:43

1 Answer 1

0

you could wrap and redefine the original function name. For instance to affect the code you posted above you would do something like:

var _add = add;
add = function (){
    // do other stuff
    _add();
}
Sign up to request clarification or add additional context in comments.

3 Comments

that is working when i am using the add function from the console but it isn't working when the site itselfs opens the function
can greasemonkey access the page context of the host page? AFAIK, extensions have a separate "window" object from the host page and need to be accessed through a different var like stackoverflow.com/questions/2303147/…
it is working now adding "// @run-at document-start" thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.