Chrome Extension – JavaScript Injector

Recently I have wanted to be able to inject JavaScript into pages that I visit for work purposes. At first I was using a bookmarks with JavaScript URLs to execute the code every time. Still, I wanted to have a way to inject the JavaScript code automatically on specific sites. You can do the following if you want to download and install the Chrome extension:

  1. Click here to download the zipped up extension.
  2. Unzip the contained file (this will be a CRX file).
  3. In Chrome go to the extensions page (chrome://extensions).
  4. Drag and drop the unzipped JSInjector.crx file onto this page to install the extension.

Source Code

If you would like to see the source code (either because you dont trust it or you are just curious) you can see the Github project here.

JavaScript – String.prototype.split Fix

In this post I proved that the String.prototype.split function can behave differently in IE8 and below than it does in other browsers. For that reason, I wrote the following code that will fix the issue so that split should work the same in all browsers:

(function(split, toString, slice) {
  String.prototype.split = ','.split(/(()(,)())/, 5).join('|') == '|,||,|'
    ? split
    : function(delim, limit) {
      var me = this;
      if (toString.call(delim).slice(8, -1) == 'String') {
        return split.apply(me, arguments);
      }

      var end, start = 0, ret = [];
      me.replace(
        new RegExp(delim.source,
          'g' + (delim.ignoreCase ? 'i' : '') + (delim.multiline ? 'm' : '')),
        function(delim) {
          var arr = slice.call(arguments, 1, -1);
          end = arr.splice(-1)[0];
          ret.push.apply(ret, [me.slice(start, end)].concat(arr));
          start = end + delim.length;
          return delim;
        }
      );
      ret.push(me.slice(start));
      return limit > 0 ? ret.slice(0, limit) : ret;
    };
})(''.split, {}.toString, [].slice);

It is important to note that there is a way to improve the efficiency of this function if you plan on using it on big strings, but will also be setting the limit every time. On the other hand, most people probably don’t use the limit parameter of the split function so there is no reason to change the code in most cases.

JavaScript – Browser Differences When Splitting With RegExp

At times I use the String#split function in order to create an array from a string based on a RegExp delimiter. Unfortunately, today I realized that some browsers act differently than others. Let’s consider the following example:

var splitWithRegExpWorks = ','.split(/,/).length == 2;

In most browsers, the value of the above variable would be true which is what you would probably expect. Unfortunately, I found that the value in IE8 is false.

In fact, all of the following in most browsers will alert true, but in IE8 (and perhaps other strange browsers) false is alerted:

You can test any of the above snippets by simply click on the code block. FYI, you will not be able to run the code snippets as is in the console on this site because i’m using SyntaxHighlighter which seems to redefine the split function (thusly fixing the issue 😀 here).

In conclusion, it is important to realize that depending on the JavaScript engine being used, String.prototype.split can return different results if using a regular expression as the delimiter.