title: Replacing page functions via userscript in Chrome?
body: <p>I've created a Greasemonkey script which replaces a page function:</p>
  
  <pre><code>(function() {
      var oldFunc = func;
      func = function() {
          oldFunc();
          // other stuff
      };
  )();
  </code></pre>
  
  <p>I had always intended this script to work in both Gresemonkey and Chrome and had read that Chrome doesn't support <code>unsafeWindow</code>, so have been injecting the script into the page by converting the function to a string and using <code>setTimeout</code>:</p>
  
  <pre><code>setTimeout("(function(){var old=func;func=function(){old();/*other stuff*/}}())", 0);
  </code></pre>
  
  <p>This works perfectly well in Greasemonkey, but gives me absolutely nothing in Chrome.  It clearly isn't firing, but nothing appears in the log, either.  I have tried a number of other injection styles, but can't get anything to work.  (For testing, I also added an <code>alert()</code> to the top of the function.)</p>
  
  <ol>
  <li><code>setTimeout(..., 0)</code> — nothing</li>
  <li>Running the function in-scope, without injection into the page — no alert, but "func is not defined" in console</li>
  <li><code>document.href = "javascript:..."</code> — nothing</li>
  <li><code>document.body.addEventListener("load", ...)</code> — nothing</li>
  </ol>
  
  <p>All of these are with <code>@run-at document-end</code>.</p>
  
  <p>Unfortunately, this being my first Chrome userscript, I'm at a loss as to how to debug it, even after an hour or so of Googling and poking around SO.  Any help?</p>
  
  <p>Actual script, currently working in Greasemonkey: <a href="http://pastebin.com/HtLVjYHg" rel="nofollow noreferrer">http://pastebin.com/HtLVjYHg</a></p>