New Research: Supply Chain Attack on Axios Pulls Malicious Dependency from npm.Details →
Socket
Book a DemoSign in
Socket

extend-function

Package Overview
Dependencies
Maintainers
1
Versions
5
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

extend-function

extendFunction.js =================

latest
npmnpm
Version
0.0.6
Version published
Weekly downloads
3
-50%
Maintainers
1
Weekly downloads
 
Created
Source

extendFunction.js

The easiest way to overwrite other functions with additional functionality

Example: Let's modify alert to keep a history array of the logs:

window.alertHistory = [];
extendFunction('alert', function(args) {
  var message = args[0];
  alertHistory.push(message);
});

Test it:

alert('a message');
if (alertHistory[0] === 'a message') {
  alert('oh geez this function is powerful!');
}

Now let's add ' from DevinRhode2' to every alert message

extendFunction('alert', function(args, nativeAlert) {
  //...
  nativeAlert(message + ' from DevinRhode2')
});

Works for methods too:

extendFunction('console.log', function(args, nativeConsoleLog) {
  //omg console.log was called!
});

For non-global functions, you assign back like this:

localFunction = extendFunction(localFunction, function(args, originalLocalFunction){
  //your magic here!
});

Modify return values:

extendFunction('strangeModule.strangeMethod', function(args, prevFunc) {
  var returnValue = prevFunc.apply(this, args);

  returnValue.extraInfo = 'idk';
  return returnValue;
});

Or promises:

extendFunction('$.ajax', function(args, prevFunc) {
  var stackOnSend = new Error().stack;

  //prevFunc is the original $.ajax
  //call that and store the value to return
  var ret = prevFunc.apply(this, args);
  ret.fail = extendFunction(ret.fail, function(args) {
    if (offlineArgs(args)) {
      //ignore, failure b/c of being offline
    } else {
      //report
      var e = new Error(args[0]);
      e.stack = stackOnSend;
      onuncaughtException(e);
    }

    //nothing returned, so extendFunction calls
    //the original fail function and returns
    //the value returned from it
  });
  return ret;
});

MIT licensed

FAQs

Package last updated on 07 Mar 2013

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts