strattonn
strattonn

Reputation: 2002

Can I modify a Javascript library at runtime?

I use a web based development environment for data entry forms. The environment lets me create rules that are triggered by form events. These events run in js in the browser but there is almost no support for debugging, which makes problem solving a nightmare.

The code in the browser has a central event handler, which has a logging feature but the quantity of information produced by it is so large, it makes finding what you need difficult. Think of info level logging gone mad. Plus you have to open a separate window to access the log.

I need to be able to log certain events to the console, or trigger breakpoints at specified rules. Is there a way to modify the environment's code below to allow it to call my debugger instead of (or in addition) to SFLog?

function handleEvent(n,t,q,r,u,f,e,o,s,h,c,l){
    if(eventsCancelled!==!0){
        SFLog({type:3,source:"handleEvent",category:"Events",
            message:"{2} event fired from {1} - {0}",parameters:[n,t,q]});
            var b="Events/Event[@SourceID='"+n+"'][@SourceType='"+t+"'][Name/text()="+q.xpathValueEncode()+"]";
            //Rest of the event handler...

function SFLog(n){
    if(checkExists(_debug)){var s=translateDebugLevel(n.type);
    if(s>=_debug){
        varu=n.type,e=n.source,r=n.category,q=n.message,h=n.parameters,o=checkExists(n.exception)? WriteExceptionXml(n.exception):null,t=n.data,l=checkExists(n.humanateData)?
        n.humanateData:!0,f=(new Date).format("yyyy-MM-ddTHH:mm:ss:fff");
        checkExists(t)&&(dataString=t.xml,checkExists(dataString)||(dataString=t),l===!0&&(dataString=Humanate(dataString)));               
        //more code for SFLog...

Cleaned Up Code

function handleEvent(n, t, q, r, u, f, e, o, s, h, c, l) {
  if (eventsCancelled !== !0) {
    SFLog({
      type: 3,
      source: "handleEvent",
      category: "Events",
      message: "{2} event fired from {1} - {0}",
      parameters: [n, t, q]
    });
    var b = "Events/Event[@SourceID='" + n + "'][@SourceType='" + t + "'][Name/text()=" + q.xpathValueEncode() + "]";
    //Rest of the event handler...
  }
}

function SFLog(n) {
  if (checkExists(_debug)) {
    var s = translateDebugLevel(n.type);
    if (s >= _debug) 
    {
      varu = n.type;
      e = n.source;
      r = n.category;
      q = n.message;
      h = n.parameters;
      o = checkExists(n.exception) ? 
        WriteExceptionXml(n.exception) :  
        null;
      t = n.data;
      l = checkExists(n.humanateData) ?
        n.humanateData : 
        !0;
        
      f = (new Date).format("yyyy-MM-ddTHH:mm:ss:fff");
      checkExists(t) && 
      (dataString = t.xml, checkExists(dataString) || 
      (dataString = t), l === !0 && (dataString = Humanate(dataString)));
      //more code for SFLog.

Upvotes: 0

Views: 1273

Answers (1)

I agree with @Eddie but one solution could be to wrap the logger function and and override it, and only log the events you care about. e.g.:

function SFLog(n){
  //old code
}
//run on the console, the first line, and then the second.
var oldLoggger = SFLog;
function SFLog(n) {
  if(/*some criteria*/) {
    oldLogger(n);
  }
} 

This way you can run the default logger with different conditions, but it probably would be best if you could modify the logger code itself to accept certain criteria, like, event type to log, or targetElement's ID, class etc.

PD: If you need to modify the eventHandler itself, you should:

  1. remove the event handler first.
  2. create your wrapper function.
  3. add the wrapper function as event handler

Upvotes: 1

Related Questions