Breaking JavaScript execution always when cookie is set

前端 未结 4 2032
生来不讨喜
生来不讨喜 2020-12-15 10:13

Is it possible to break javascript execution in FireBug or in some other web developer tool always when cookie is set (without setting JS breakpoints explicitly)?

<         


        
相关标签:
4条回答
  • 2020-12-15 10:32

    In Chrome dev-tools, you can right click on a cookie in the application cookies and select 'show request with this cookie'

    so it's not an interception, but if your goal is to identify where a cookie comes from then it's a good way.

    0 讨论(0)
  • 2020-12-15 10:39

    Try setting it in a If statement.

    if(document.cookie.indexOf('...') >= 0){
      debugger;
    }
    

    note: when using firefox your console has to be open. in chrome this is not the case.

    0 讨论(0)
  • 2020-12-15 10:42

    The answer below does not seem to work in Chrome. Adding this snippet in the beginning of an html → head block works fine:

    <script type="text/javascript">
        function debugAccess(obj, prop, debugGet){
            var origValue = obj[prop];
            Object.defineProperty(obj, prop, {
                get: function () {
                    if ( debugGet )
                        debugger;
                    return origValue;
                },
                set: function(val) {
                    debugger;
                    return origValue = val;
                }
            });
        };
        debugAccess(document, 'cookie');
    </script>
    

    See this Angular University page for more information.

    0 讨论(0)
  • 2020-12-15 10:46

    This should work (run it in a console):

    origDescriptor = Object.getOwnPropertyDescriptor(HTMLDocument.prototype, 'cookie');
    Object.defineProperty(document, 'cookie', {
      get() {
        return origDescriptor.get.call(this);
      },
      set(value) {
        debugger;
        return origDescriptor.set.call(this, value);
      },
      enumerable: true,
      configurable: true
    });
    
    0 讨论(0)
提交回复
热议问题