设置cookie时总是中断JavaScript执行

hab*_*713 5 javascript cookies firebug web-developer-toolbar

设置cookie(不显式设置JS断点)时,是否总是有可能中断FireBug(或其他Web开发工具)中的javascript执行?

document.cookie = '...';
Run Code Online (Sandbox Code Playgroud)

哈里

ffl*_*ent 7

这应该可以工作(在控制台中运行它):

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
});
Run Code Online (Sandbox Code Playgroud)


hab*_*713 7

/sf/answers/2887342181/ 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>
Run Code Online (Sandbox Code Playgroud)

See this Angular University page for more information.

  • 在 Chrome 中运行良好。1.在index的第一行js添加断点。2. 重新加载页面直到断点触发。3. 将以上代码粘贴到脚本标签之间到控制台。4. 单步执行新触发的 js 断点并读取调试器中的 Call Stack 和 Scope 以确定脚本来源和 cookie name/val 。 (2认同)