Talk:Content Script Injection

From GreaseSpot Wiki
Revision as of 20:13, 27 December 2010 by John Hawkinson (talk | contribs) (→‎Patching a javascript)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigationJump to search

Patching JavaScript

This is great. I recently found myself needing to patch the JavaScript in an existing page, and had a lot of trouble. But I was pointed at this page, and realized I could do it like this:

oldBuildSessions = unsafeWindow.BuildSessions.toString();
newBuildSessions = oldBuildSessions.replace("history.go(0);",
  'var commented_out_by_userscript = "$&";');
contentEval("BuildSessions = "+newBuildSessions);

the original JavaScript being edited was:

function BuildSessions(num)
{ 
...
if (navigator.appName == "Netscape")
 history.go(0);
}

Note that we comment out by assigning to a string variable, rather than using a //, because the // comments get removed by the tokenizer. John Hawkinson 20:13, 27 December 2010 (UTC)