Talk:Content Script Injection

From GreaseSpot Wiki
Jump to: navigation, 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")

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)