stopBefore(document, 'getElementById')
stopBefore('document.getElementById') // the same as the previous
stopBefore(Element.prototype, 'removeChild')
stopAfter(Element.prototype, 'querySelectorAll')
Element.prototype.querySelectorAll.removeBreakpoint()
Last active
January 7, 2025 08:28
-
-
Save NV/5376464 to your computer and use it in GitHub Desktop.
Prepend the debugger statement to a function as easy as stopBefore('Element.prototype.removeChild'). Works in Chrome DevTools and Safari Inspector, doesn’t work in Firebug‘s and Firefox Developer Tools‘ console (I don’t know why). Works as a standalone script everywhere.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function() { | |
window.stopBefore = function stopBefore(object, methodName) { | |
// stopBefore('Element.prototype.removeChild') | |
if (typeof methodName == 'undefined' && typeof object == 'string') { | |
var temp = resolvePath(object); | |
object = temp.object; | |
methodName = temp.methodName; | |
} | |
var originalMethod = object[methodName]; | |
object[methodName] = function patched() { | |
debugger; | |
var result = originalMethod.apply(this, arguments); | |
return result; | |
}; | |
object[methodName].removeBreakpoint = function() { | |
object[methodName] = originalMethod; | |
}; | |
object[methodName].__original = originalMethod; | |
}; | |
window.stopAfter = function stopAfter(object, methodName) { | |
// stopAfter('jQuery.fn.html') | |
if (typeof methodName == 'undefined') { | |
var temp = resolvePath(object); | |
object = temp.object; | |
methodName = temp.methodName; | |
} | |
var originalMethod = object[methodName]; | |
object[methodName] = function patched() { | |
var result = originalMethod.apply(this, arguments); | |
debugger; | |
return result; | |
}; | |
object[methodName].removeBreakpoint = function() { | |
object[methodName] = originalMethod; | |
}; | |
object[methodName].__original = originalMethod; | |
}; | |
function resolvePath(path) { | |
var object = this; | |
var parts = path.split('.'); | |
for (var i = 0, ii = parts.length - 1; i < ii; i++) { | |
object = object[parts[i]]; | |
} | |
return { | |
object: object, | |
methodName: parts[ii] | |
} | |
} | |
})(); |
@azproduction good to know, thanks!
Great idea! I consolidated the code a bit and added logBefore
, logAfter
, logAround
and logCount
too: https://gist.github.com/johan/5436827
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It works fine as "normal script" http://jsfiddle.net/4RGfa/ but in Firebug console it is broken as Firebug console itself.