Crazy way to change third party scripts
Date : 2008 02 13 Category : Tech & DevelopmentPaul Irish posted a crazy method for tweaking third party scripts. His problem was that he was using MultiMap (online Mapping API) and wanted to internationalize the information, but it was hard coded in the JavaScript.
To get around the problem he does this:
PLAIN TEXT JAVASCRIPT:// WARNING!! This is such a massive hack. Oh-so-hackalicious
// Problem: Multimap doesnt allow internationalization of its buttons, etc.
// Solution: Redefine their JS functions to use variables that are internationalized.
// Assumption: That these internal function names stay the same.
// Risk: If function names change, this code will (probably) silently fail.
// The following statements change the right-click context menu items and the map/aerial/hybrid buttons.
// Instead of hard-coded strings, it will use a variable which we control.
// ON TO THE HACKS!!
// Hack 1: modify the mmjr() and mmfl() functions with funcName.toString().replace()
// Hack 2: use eval() to set the definition
// Hack 3: browser sniff because IE and FF handle toString()'d strings differently (single-quote vs double-quote)
var isIE = $.browser.msie; // jQuery browser sniff.
eval(
"mmki.prototype.mmjr = " +
mmki.prototype.mmjr
.toString()
.replace( isIE ? "'Move map to here'" : '"Move map to here"' , 'i18n.retailLocator.moveMapToHere')
.replace( isIE ? "'Zoom in to here'" : '"Zoom in to here"' , 'i18n.retailLocator.zoomInToHere')
.replace( isIE ? "'Zoom out from here'" : '"Zoom out from here"', 'i18n.retailLocator.zoomOutFromHere')
);
eval(
"MultimapViewer.prototype.mmfl = " +
MultimapViewer.prototype.mmfl
.toString()
.replace( isIE ? "'Map'" : '"Map"', 'i18n.retailLocator.map')
.replace( isIE ? "'Hybrid'" : '"Hybrid"', "i18n.retailLocator.hybrid")
.replace( isIE ? "'Aerial'" : '"Aerial"', 'i18n.retailLocator.aerial')
);
Yowser :)