Replace function don't work on dynamic page

in Script development
Subscribe to Replace function don't work on dynamic page 7 posts, 4 voices



jonas18z User
ChromeWindows

I doing:

unsafeWindow.check_key function (e) {
alert("replaced!!");
}

I can get it to work on a own page simple page that's calls the same function.

But on the real page grecmonkey don't replace the function.

Sorry but I can't post the page it's an internal system

 
Jefferson Scher Scriptwright
FirefoxWindows

If check_key is created by another script, your change might be too early. You could try adding it after a delay using window.setTimeout.

 
Couchy Scriptwright
ChromeX11

Well, you forgot the equals sign for one thing. Aside from that, the preferred way of replacing functions on the page is to tack on a script element instead of using unsafeWindow.

function injectScript(scriptContent)
{
var script = document.createElement("script");
script.setAttribute("type", "text/javascript");
script.appendChild(document.createTextNode("("+scriptContent.toString()+")();"));
document.getElementsByTagName("head")[0].appendChild(script);
}

var replacementFunction = function(){check_key = function(e){alert("replaced!");};};
injectScript(replacementFunction);

 
jonas18z User
FirefoxWindows

Thanks for all help I tried with delay, and the tips from Couchy. The equals was just a typo here.

Maybe I doing some totally wrong

Here is an example for the problem, I can't replace check_key but I can replace the function preLoad.

html+ javascript


<html><head><meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script language="JavaScript1.2">


ns4 = (document.layers)? true:false
ie4 = (document.all)? true:false

var oldkeyhandler;

function nullkeyhandler(e)
{
}

if (ns4) document.captureEvents(Event.KEYPRESS);

if(document.onkeypress)
	oldkeyhandler = document.onkeypress; 
else
	oldkeyhandler = nullkeyhandler; 
 
document.onkeypress = check_key;


  function check_key(e) {
//   whichASC = (ns4) ? e.which : event.keyCode;
    alert(whichASC = e.which );
   
  }

  function preLoad() { 
  
  alert("loaded");
  }
  
//-->

</script>


 

</head>
<body onLoad="javascript:preLoad();">





	<input class="tid" type="text" size="1" name="falt" onFocus="setCurFoc('falt')">
 

</body></html>

</code>

 
Iskaral_Pust User
FirefoxX11

Why is it, that your way of replacing a function works but deleting the offending script tag from the original document does not?

 
jonas18z User
ChromeMacintosh

Sorry but really I don't understand what you mean? But the script I was posted above is part from the original site, that's an intranet.

And I removed/modified some function and code that I can't post and.

Here is the example from my code with the problem function.

http://pastehtml.com/view/br8az8jw2.html

I can't replace the function check_key with either unsafeWindow or Couchys method.

Maybe greacemonkey can't replace this kind of function or what do I do wrong?

 
Iskaral_Pust User
FirefoxX11

I just found this splendid userscript example which uses the @run-at directive to replace any script on a website. It's quite magical! http://userscripts.org/scripts/show/125936