Find the secrets to infinite income, and automate it!
23 Feb
The examples here are taken from Function page of the Prototype JS api documentation. They have been re-written to reflect the Prototype XUL changes. Hope they help!
var fn = function(foo, bar) { return foo + bar; }; argumentNames(fn); //-> ['foo', 'bar']
var fn = function(foo, bar) { return foo + bar; }; argumentNames(fn); //-> ['foo', 'bar']
var obj = { name: 'A nice demo' }; function handler(e) { var tag = Event.element(e).tagName.toLowerCase(); var data = $A(arguments); data.shift(); alert(this.name + '\nClick on a ' + tag + '\nOther args: ' + data.join(', ')); } Event.observe(document, 'click', bindAsEventListener(obj, handler, 1, 2, 3));
String.prototype.splitOnSpaces = curry(String.prototype.split, " "); "foo bar baz thud".splitOnSpaces(); //-> ["foo", "bar", "baz", "thud"]
function hideNewElement() { $('inserted').hide(); }; function insertThenHide(markup) { $('container').insert(markup); // Prototype XUL defer(hideNewElement); } insertThenHide(" <p id="inserted">Lorem ipsum ");
// before: window.setTimeout(function() { Element.addClassName('foo', 'bar'); }, 1000); // after: delay(Element.addClassName, 1, 'foo', 'bar'); // clearing a timeout var id = delay(Element.hide, 5, 'foo'); window.clearTimeout(id);
// start off with a simple function that does an operation // on the target object: var fn = function(target, foo) { target.value = foo; }; var object = {}; // use the original function fn(object, 'bar'); object.value //-> 'bar' // if we methodize it and copy over to the object, it becomes // a method of the object and takes 1 argument less: object.fnMethodized = methodize(fn); object.fnMethodized('boom!'); object.value //-> 'boom!'
String.prototype.capitalize = wrap(String.prototype.capitalize, function(proceed, eachWord) { if (eachWord && this.include(" ")) { // capitalize each word in the string return this.split(" ").invoke("capitalize").join(" "); } else { // proceed using the original function return proceed(); } }); "hello world".capitalize() // "Hello world" "hello world".capitalize(true) // "Hello World"
23 Feb
For the most part the functionality of Prototype XUL works the same as normal prototype. The prototype documentation can be found here. The underlying changes are in the methods that extend Prototype.function. The functions that have been re-written are: argumentNames, bind, bindAsEventListener, curry, delay, defer, wrap and methodize. The core difference in use, is that these functions can not be extended from objects anymore and must be explicitly called.
argumentNames(someFunction) //-> Array
bind(thisObj, someFunction) //-> Function
bindAsEventListener(thisObj, someFunction) //-> Function
curry(someFunction, [args...]) //-> Function
curry(someFunction, [args...]) //-> Number
delay(someFunction, seconds, [args...]) //-> Number
delay(someFunction, [args...]) //-> Function
delay(wrapperFunction, someFunction, [args...]) //-> Function
21 Feb
While writing out the Prototype XUL documention I came across a nasty little bug that took me a few hours to fix. I will wrap up the documentation tomorrow. If you downloaded the Prototype XUL 1.6.0.2 file anytime before today, please upgrade (the version number has not change).
19 Feb
Drum roll please… After three attempts/failures, I present to you the newest version of the Prototype XUL library! The previous released version technically never worked. It was something I threw together for a Firefox project that only required the basic of functionality (Ajax.Request, utility functions, etc.). I was under the impression that converting the functions that failed due to the known XUL function bug would solve all the problems. Unfortunately this was not the case and under these assumptions more complex functionality ceased to work correctly. This version has gone through a finer examination, conversion and code update to (hopefully) be a fully functional representation of prototype. Hopefully Firefox 3 will have this bug corrected and I wont have to release many future updates. Please post any bug reports, questions or feedback in the comments section, this will help reduce duplication and make my life easier :). Enjoy!
WARNING: I have not fully tested this library!
Disclaimer: This modified version of the Prototye JavaScript framework is in no way affiliated, distributed or supported by Sam Stephenson or the Prototype library project. This library has been modified from its original intended version/use and I take no responsibility for any bugs, damages or problems this library may cause.
* Most up to date version as of 2/22/08
* Documentation
* Examples
6 Dec
I wrote a class to deal with preference handling in the PriceAdvance Firefox extension. I found the Mozilla documentation to be difficult to understand. So, I decided to share the class. Theres a little bug in Firefox that took me two days and borderline suicide to figure out… Extensions can have a default preference file that you can query using the preference interface. These preference files are “magically” loaded from a very specific file folder/location - “defaults\preferences\xxxxx.js”. To set a preference you simply put “pref(”branch_title.variable_name”, value);” in the file. If you use the class I made these preferences can be accessed like this: “Preference.getPref(”variable_name”)”. Now the bug I found is with the way you configure your extension. If the extension is bundled and installed “properly” it will work without a hitch. For development purposes I have my extension using a pointer from the extension folder to another folder location where I do all the work. Apparently when its setup as a pointer it wont load the preference files. Whats really annoying is theres no way to verify if the magical files have been loaded or not. Because of this I have avoided preference defaults and use my own variables class.
Preferences = { initialize: function() { this.prefs = Components.classes['@mozilla.org/preferences-service;1'] .getService(Components.interfaces.nsIPrefService) .getBranch('extensions.branch_title.'); this.prefs.QueryInterface(Components.interfaces.nsIPrefBranch2); }, getPref: function(name) { var type = this.prefs.getPrefType(name); if (type == this.prefs.PREF_STRING) return this.prefs.getCharPref(name); else if (type == this.prefs.PREF_INT) return this.prefs.getIntPref(name); else if (type == this.prefs.PREF_BOOL) return this.prefs.getBoolPref(name); }, setPref: function(name, value) { var type = this.prefs.getPrefType(name); if(type == 0) { if(typeof(value) == 'number') type = 64; else if(typeof(value) == 'string') type = 32; else if(typeof(value) == 'boolean') type = 128; } if (type == this.prefs.PREF_STRING) this.prefs.setCharPref(name, value); else if (type == this.prefs.PREF_INT) this.prefs.setIntPref(name, value); else if (type == this.prefs.PREF_BOOL) this.prefs.setBoolPref(name, value); } }; // USAGE: Preferences.initialize(); Preference.setPref('this_works', true); alert(Preference.getPref('this_works')); /******************/
To view preference variables that have been set, type “about:config” into the address bar and filter for your branch.
7 Nov
A few days ago I posted about a FireFox bug that affected Function.prototype in the Chrome environment. The two modified prototype libraries that I posted were starting to get a little outdated. I spent the night modifying the most recent version of Prototype 1.6.0_rc1 framework to work in Chrome. I have also removed as many references as I could find to browsers other then Mozilla/Gecko. If you find any bugs or make any changes, please let me know so I can update the posted code.
WARNING: I have not fully tested this library!
Disclaimer: This modified version of the Prototye JavaScript framework is in no way affiliated, distributed or supported by Sam Stephenson or the Prototype library project. This library has been modified from its original intended version/use and I take no responsibility for any bugs, damages or problems this library may cause.
3 Nov
One of the projects that I have been working on is a FireFox extension. I’m pretty new to the XUL/Extension space, but have a lot of JavaScript experience. My framework of choice has long standing been Prototype. I figured why not make life easy and utilize that functionality in the extension. I soon realized that almost none of the functionality worked in the Chrome environment. I spent a lot of time reinventing the wheel, re-writing many of the general prototype features. When I started writing my own bind function I realized that it was throwing erroneous errors. I know the code was perfect and successfully tested it in a front end environment. I started doing some research and found out theres a long standing bug in the FireFox Chrome environment that prevents extending Function.prototype. Some more research and I found that the reason most of the prototype functionality didn’t work was because the methods require use of the bind function. I found a hacked version of prototype 1.4 that has been re-written to work in Chrome and a bind patch for prototype 1.5.
Update: I modified the most recent version of Prototype (1.6.0 RC1) to work in Chrome. You can download the code here.