Browsed by
Category: Scripting

Hiding JavaScript? Maybe….

Hiding JavaScript? Maybe….

As anyone around me knows (because I will not shut up about it) I have been working on a new project. Said project relies very heavily on JavaScript and revolves around an unusual use for a web browser that I do not want to advertise just yet. Because of this I have been looking for ways to hide my HTML, CSS, and JavaScript from the client. The short answer I discovered? You can not.

Or can you? Of course if any software is going to run code it will have to have a copy in one form or another. With a scripting language the code is presumably viewable to anyone, right? With JavaScript it is viewable in the View Source option of the users browser which makes everyone from a curious hacker to your grandmother your worse enemy (I love grandma unless she steals my stuff). You can perform obfuscation on your code but that really does not fix the problem; Anyone with half a brain could decode anything the browser can decode because all the tools they would need are already in front of them. What to do, what to do?

Although it does not solve the problem completely I am considering a new project. A project that might hide virtually everything but still allow the browser to render properly. What if this method was inherently cross-platform and completely transparent to the client? What if this method not only offered a developer a lot more security but also provided an API that made web applications stateful with any unmodified, off-the-shelf web server and a lot more efficient on bandwidth?

I may soon start running experiments to test feasibility but I do not foresee any reason my idea would not work. Perhaps this could even be a marketable product…

Working Around JavaScript Shortcomings

Working Around JavaScript Shortcomings

I am working on a real-time, JavaScript only project. I do not want to give too much away right now but I will say this: JavaScript was not designed for what I want it to do. The timers are not accurate enough and relying solely on synchronous or asynchronous communication between components simply will not work at all. What it comes down to is this is what C or C++ was meant for.

I have spend hours, today alone and not mentioning yesterday, just reading. Reading on tricks to make your own timers, how Internet Explorer on Windows or Firefox on Linux might react or whatever and how reliably. But I am determined; I am determined to make what I envision work as I envision it with nothing more than what everyones browsers already have. A friend just suggested I use Flash but Flash has way too many issues with performance and cross-compatibility (say what you want I am sticking to that). In two words? Fuck. Flash.

I have written about how anal I am in the past. Especially when it comes to things like this. I refuse to be beaten by a scripting language never-the-less a scripting language built into a God damned web browser. If I may bring my ego into this– too late– it would also be great to be “the guy” who pulled this off. The guy who people copy. The guy who starts a bunch of copycat projects.

I have learned a lot thus far. I am convinced this is very doable. It is all just going to require some research, cursing, work, and cursing. This is going to be great.

The Old vs The New

The Old vs The New

Back in the day computer programming was more of an ordeal than it is now.

A computer programmers job is to take an idea, turn it into a set of instructions, and write code in a programming language that tells the computer what those instructions are. The computer then takes the resulting program and does whatever it was told to do. All of these instructions contained in this program, at their lowest level, boil down to “stick these numbers into these memory locations, do some addition on them, stick the result in this memory location, rinse, repeat.” It often all appears to be much more than that but that is a computer programmers mission in life.

In most older languages– C comes to mind– a programmer had to first allocate the memory they wanted to use. After they were done using it they had to deallocate the memory. This was especially important because memory was expensive and, as a result, computers of the day did not have much of it. It was common for tricks, or hacks, to be used that were not ideal solutions because there was simply no other way with such a limited resource.

Fast forward to years ago when I first started with this discipline. The people who I had learned from– both in the form of teachers but much more commonly in the form of over-the-Internet searching– were the product of this era. As a result I have an almost religious obsession with managing resources myself. I tend to shy away from libraries that do a lot of the work for me simply because I can not see what they are doing, how they are doing it, and when they are doing it. In addition I do not control their resource usage which just irks the crap out of me. In an age when desktops with multi-gigabyte amounts of memory can be had for under $300 is this still a logical habit?

Many languages today have a feature called garbage collection. Very popular and common languages like PHP, Java, and the various .NET Framework languages all implement this. Not only do they implement it they nearly demand you rely on it. This always seems like the job of a programmer to me and not the machine. After all, the program needs to, in addition to whatever code I write, keep track of all memory allocated, variable types, scope of said variables, ect for garbage collection to work. It just seems like a lot of work being done using a lot of resources I could easily use somewhere else and a bad case of wag the dog. It is also worth mentioning that none of the above languages come anywhere near the speed and efficiency of C.

What brings all this up is a project I have had in the back of my mind for a year now. This project requires a lot of JavaScript. JavaScipt, for anyone who is unfamiliar, has a surprising amount of functionality missing from it. It can not, for example, trim excess whitespace from a string nor can it pad a string. These would be common programming tasks which you would have to write yourself.

I want to do something more complex than basic string manipulation. I want to create a scrolling, Google Maps-esk interface among other things. I could write all of the code myself (which I already did for some of another project) or I just could use a library like jQuery. jQuery has a lot of the smaller bits of what I need to do already done. Not only that but so many people who use it that one can safely assume that most of the bugs I would be likely to hit would already have been ironed out. It really seems like a win-win for both jQuery and myself.

Perhaps Jeff Atwood makes a good point when he says I am dangerous. Perhaps I have already answered my own question just by writing about it. The logical solution is to use the time-tested, rave-reviewed jQuery but there is just something inside me that does not want to give up the control. It just seems… wrong. I should be able to control every aspect of my software as to make it as efficient as possible. I should put in the work and have everything the way I know it should be without any questions about the guts or inner workings of something. On the other hand if I were a mechanic I would not be building my own combustion engine just because I could…

… I should stop being such an anal control freak.

Missing JavaScript Functionality

Missing JavaScript Functionality

JavaScript does not offer a lot of things one would imagine would be included in such a seasoned scripting language. Below are a few functions I wrote to fill some gaps.

Dump variable contents

function var_dump(arr, level) {
if ( !level )
level = 0;

var dumped_text = “”;
var level_padding = “”;
for ( var j = 0; j < = level; j++ ) level_padding += "\t"; if ( typeof(arr) == "object" ) { for ( var item in arr ) { var value = arr[item]; if ( typeof(value) == "object" ) { dumped_text += level_padding + "\"" + item + "\" =>\n”;
dumped_text += var_dump(value, level + 1);
}
else
dumped_text += level_padding + “\”” + item + “\” => \”” + value + “\”\n”;
}
}
else
dumped_text = “===>” + arr + “< ===(" + typeof(arr) + ")"; return dumped_text; };

Trimming the whitespace off both sides of a string

function trim(str) {
return str.replace(/^\s\s*/, "").replace(/\s\s*$/, "");
};

Padding the start of a string

function pad(str, len, padding) {
str += "";

if ( str.length <= len ) {
var str_new = "";
for ( i = 0; i < len – str.length; i++ )
str_new += padding;

return str_new + str;
}

return str;
};

Getting the mouse position

function mouse_position() {
if ( document.all && !window.opera )
return [event.clientX + document.documentElement.scrollLeft, event.clientY + document.documentElement.scrollTop];

return [e.pageX, e.pageY];
};

Getting an elements absolute position

function element_position(element) {
var x = y = 0;

if ( element.offsetParent ) {
x = element.offsetLeft;
y = element.offsetTop;
while ( element = element.offsetParent ) {
x += element.offsetLeft;
y += element.offsetTop;
}
}

return [x, y];
};

All of these were tested in Microsoft Internet Explorer 8, Mozilla FireFox 3.5.8, Google Chrome 5.0.307.9 beta, and Apple Safari 4.0.4.

Please note I have not touched JavaScript in years and I am a bit rusty.