Basically, what you're asking for is a way to
hook a function, namely a standard Blizzard function.
Hooking functions insecurely is usually a bad idea, specially if they are Blizzard ones. You will cause taint, which will spread to other parts of the UI (both Blizzard and other addons' elements), which will trigger events like ADDON_ACTION_BLOCKED or ADDON_ACTION_FORBIDDEN (and I'm talking about actions from sources
other than your addon (Blizzard or other addons), even though the culprit will be your own hook.
Still, for the sake of understanding, I'll show you how to do an insecure hook first, and then link to the secure hooking functons the Blizzard API provides.
#1: Insecure hook on function "standardFunction" (
full replacement):
Code:
myFunction = function()
-- do stuff
end
standardFunction = myFunction;
-- Any calls to standardFunction() (from Blizzard or another addon) will now call myFunction() instead. The original standardFunction no longer "exists" (there are no available references to it)
#2: Insecure hook on function "standardFunction" (
prehook, i.e. with callback to the original function when done):
Code:
oldFunction = standardFunction;
myFunction = function()
-- do stuff
oldFunction();
end
standardFunction = myFunction;
-- Any calls to standardFunction() (from Blizzard or another addon) will now call myFunction() instead. myFunction does its thing and then calls the original standardFunction() (now accessible as oldFunction())[/
A full replacement or a prehook of a standard Blizzard function should never be done (well, there is an exception or two where you actually can't get around it; e.g. changing the color of a given text output to the standard ChatFrame).
As a rule of thumb, you should only fully replace or prehook a function of your own (in the same addon of yours or a global function from another addon of yours).
#3: Insecure hook on function "standardFunction" (
posthook, i.e. with callback to the original function first):
Code:
oldFunction = standardFunction;
myFunction = function()
oldFunction();
-- do stuff
end
standardFunction = myFunction;
-- Any calls to standardFunction() (from Blizzard or another addon) will now call myFunction() instead. myFunction calls the original standardFunction() (now accessible as oldFunction()) and then does its thing.[/
All three hooks above are insecure. They will cause taint, the standard UI will notice and mark anything that later calls the hooked function (this is also valid for standard variables you may have changed) as insecure, thus restricting its usability, namely but not exclusively, during combat.
The standard API provides two functions (and a (big) little something else) to securely posthook functions (both Blizzard's and other addons'). For more details please see:
API hooksecurefunc
API Frame HookScript
You can also look at
RestrictedEnvironment,
SecureHandlers and the
SecureStateDriver (as starting points to the whole thing).