0

I want to log all jquery calls like:

Html

<div id="element"></div>

Jquery

$("#element").addClass("yourClass");
$("#element").hide();

The result should be:

<div id="element" data-js="1,2"></div>

* The "data-js" attribute added by the base function to let me know in which line the original code.

basically, I need to create base plugin in jquery or callback to each plugin that I can register it somewhere.

Mosh Feu
  • 28,354
  • 16
  • 88
  • 135
  • 1
    What are `1` and `2` supposed to mean? Otherwise related: [Record methods and parameters called in jQuery](http://stackoverflow.com/q/13377326/464709). – Frédéric Hamidi Mar 03 '14 at 08:19

1 Answers1

2

I'm not aware of any jQuery method "hook" capability built into all jQuery methods so I think the only way you could know when all jQuery methods are called from your own javascript is to dynamically install a hook for every jQuery method. You could loop through all the methods in jQuery.fn (after all plug-ins are installed) and install a shim that would do your logging and then call the original method being careful to preserve this, arguments and return values.

The shim would look something like this:

function installShims() {
    for (var method in jQuery.fn) {
        if (jQuery.fn.hasOwnProperty(method) && 
          typeof jQuery.fn[method] === "function" &&
          method !== "init") {
            (function(oldMethod) {
                 jQuery.fn[method] = function() {
                     // do your logging here
                     // you have access to this and to arguments
                     return oldMethod.apply(this, arguments)
                 };
            })(jQuery.fn[method])
        }
    }
}

Working demo: http://jsfiddle.net/jfriend00/24nvd/

jfriend00
  • 683,504
  • 96
  • 985
  • 979
  • Can you please add a simple example? – Mosh Feu Mar 03 '14 at 08:18
  • 1
    @mosh - to get this to work, I had to update the code to exclude the "init" method. I'm not exactly sure, but I presume it has something to do with the initial construction of the jQuery object. If this was going to be used for anything serious, one should probably figure out why the "init" method caused a problem here to understand it completely. – jfriend00 Mar 03 '14 at 08:31
  • 1
    @mosh - I added to the jsFiddle, the ability to log the method name too. – jfriend00 Mar 03 '14 at 08:34
  • There is way to add row's number to the attribute? – Mosh Feu Mar 03 '14 at 09:13
  • @mosh - I have no idea what you're asking about row numbers? What are row numbers? – jfriend00 Mar 03 '14 at 09:14
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/48850/discussion-between-mosh-and-jfriend00) – Mosh Feu Mar 03 '14 at 10:01
  • @mosh - getting javascript code line numbers is messy (not standard between browsers). You can read about it here: http://stackoverflow.com/questions/1340872/how-to-get-javascript-caller-function-line-number-how-to-get-javascript-caller – jfriend00 Mar 03 '14 at 11:07