-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

basicn00b
  • 27
  • 7
  • 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.... – NewToJS Jan 13 '17 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... – NewToJS Jan 13 '17 at 20:40
  • @NewToJS just changed the script really doesn't matter whats in the function when i can access it – basicn00b Jan 13 '17 at 20:54
  • 1
    Possible duplicate of [Injecting JS functions into the page from a Greasemonkey script on Chrome](http://stackoverflow.com/questions/2303147/injecting-js-functions-into-the-page-from-a-greasemonkey-script-on-chrome) – Patrick Gunderson Jan 13 '17 at 21:43

1 Answers1

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();
}
Patrick Gunderson
  • 3,263
  • 17
  • 28
  • 1
    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 – basicn00b Jan 13 '17 at 21:14
  • 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 http://stackoverflow.com/questions/2303147/injecting-js-functions-into-the-page-from-a-greasemonkey-script-on-chrome – Patrick Gunderson Jan 13 '17 at 21:41
  • it is working now adding "// @run-at document-start" thank you! – basicn00b Jan 13 '17 at 21:55