Skip to content

Replaces specified global variables and their properties in javascript code

Notifications You must be signed in to change notification settings

alexjesp/global-replacer

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Global Replacer

A script that replaces specified global variables and their properties in a javascript code string.

Features

  • Can replace global variables, i.e. variables that haven't occurred in any local scopes
  • Can replace global variables' properties
  • Can replace both in one e.g. window.location -> _window._l_ocation
  • Can't currently specify a global change and a corresponding global property change in separate replacement options e.g.
{
  'window': '_window',
  'window.location': 'window._l_ocation'
}
  • The number of object parts must be equal to the replaced version
    i.e. ['window', 'location'] and ['window', '__location'] both have two parts

Examples

Replacing globals

var replacer = require('global-replacer');
var replacedString = replacer('var loc = window.location.href.toString();', {
  replacements: {
    'window': '_window'
  }
});
console.log(replacedString); // var loc = _window.location.href.toString();

Replacing global properties

var replacer = require('global-replacer');
var replacedString = replacer('var loc = window.location.href.toString();', {
  replacements: {
    'window.location': 'window._l_o_c_ation'
  }
});
console.log(replacedString); // var loc = window._l_o_c_ation.href.toString();

The replacements will also work when globals are passed into functions.
For example, with the above options:

(function (w) {
  w.location.href = 'http://google.com';
})(window);

will become

(function (w) {
  w._l_o_c_ation.href = 'http://google.com';
})(window);

About

Replaces specified global variables and their properties in javascript code

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published