Kodewerx

Our culture has advanced beyond all that you could possibly comprehend with one hundred percent of your brain.
It is currently Thu Dec 05, 2024 9:43 pm

All times are UTC - 8 hours [ DST ]




Post new topic Reply to topic  [ 18 posts ] 
Author Message
 Post subject: Javascript Snippets
PostPosted: Sun Oct 17, 2010 5:23 pm 
Offline
Krew (Admin)
Krew (Admin)
User avatar

Joined: Sun Oct 01, 2006 9:46 pm
Posts: 2331
Location: *poof*
Title: The Mad Hacker
These are just a few JS functions I wrote to make things easier at work. Couldn't find good ones on google so I wrote them myself.

A Bookmarkable 'reset forms' link:
Code:
javascript:for(x in document.getElementsByTagName("form")){document.getElementsByTagName("form")[x].reset();};




Print price from float or string (2 digits after decimal, truncate instead of rounding):
Code:
function PriceString(x){
       var cost = parseFloat(x).toString();
       return ((cost.indexOf(".")==-1?(cost+"."):(cost))+"00").replace(/^([0-9]*)\.([0-9]{2}).*/,
"$$$1.$2");
}




Substring Count
Code:
/* String prototype to use with either below */
String.prototype.ssc = function(needle) {
   return substr_count(this, needle);
}

/* Recursive substr_count */
function substr_count(haystack, needle) {
   return haystack.indexOf(needle)==-1?0:substr_count(haystack.substr(haystack.indexOf(needle)+1), needle)+1;
}

/*Non-recursive version*/
function substr_count(haystack, needle) {
   var count=0;
   for(var x=0; haystack.indexOf(needle, x)!=-1; count++)
   {
      x=haystack.indexOf(needle, x)+1;
   }
   return count;
}




Random Alpha Numeric String of length x
Code:
function randomString(x) {
   var randomstring = '';
   for (var i=0; i<x; i++)
      randomstring += "0123456789abcdefghiklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXTZ".substr(Math.floor(Math.random() * 61),1);
   return randomstring;
}





Hopefully you can use them or, even better, offer better solutions.

Please feel free to post other useful javascript functions in here too.

_________________
Image


Top
 Profile  
Reply with quote  
 Post subject: Re: Javascript Snippets
PostPosted: Sun Oct 17, 2010 10:43 pm 
Offline
Krew (Admin)
Krew (Admin)
User avatar

Joined: Sun Oct 01, 2006 9:26 pm
Posts: 3768
Title: All in a day's work.
Isn't JavaScript great? My only suggestion is on the first one; use an anonymous self-executing function (you'll see this pattern a lot to deal with namespacing) and define document.getElementsByTagName in a variable:

Code:
javascript:(function(){var $=document.getElementsByTagName;for(x in $("form")){$("form")[x].reset();};})();


I have a couple of useful snippets like these, too. Some are at work on the LAN, so I can't reach them from here. I started developing some jQuery plugins, as well. I'll post them here when they are ready.

_________________
I have to return some video tapes.

Feed me a stray cat.


Top
 Profile  
Reply with quote  
 Post subject: Re: Javascript Snippets
PostPosted: Mon Oct 18, 2010 7:20 pm 
Offline
Krew (Admin)
Krew (Admin)
User avatar

Joined: Sun Oct 01, 2006 9:46 pm
Posts: 2331
Location: *poof*
Title: The Mad Hacker
Yeah; I have fun with it. =)

I'll continue to post more as I write them. I wrote a basic structure for dealing with click/shift+click/ctrl+click/shift+ctrl+click for selecting lists of items and some cross-browser-friendly shift/ctrl detection. Maybe I'll post that when I bring it home.


I look forward to seeing your snippets and plugins!

_________________
Image


Top
 Profile  
Reply with quote  
 Post subject: Re: Javascript Snippets
PostPosted: Tue Jan 25, 2011 1:35 am 
Offline
Komrade
Komrade

Joined: Tue Mar 27, 2007 10:18 am
Posts: 1328
Shouldn't it be "JavaScript"-

_________________
Image


Top
 Profile  
Reply with quote  
 Post subject: Re: Javascript Snippets
PostPosted: Mon Feb 07, 2011 1:40 am 
Offline
Krew (Admin)
Krew (Admin)
User avatar

Joined: Sun Oct 01, 2006 9:26 pm
Posts: 3768
Title: All in a day's work.
Two resources:

http://www.crockford.com/javascript/
http://inimino.org/~inimino/blog/javascript_semicolons

Combining ideas these articles, I wrote DoodleEarth*. And then I ran it through Google's JavaScript Closure Compiler, which complained that IE doesn't support getters. *sigh* I used getters as a portable means of creating true constants. Not that IE even matters for this project, since it requires HTML canvas. Apparently, it also throws a fit when a variable is defined twice. That was an easy fix.


* As of February 7, 2011, the server hosting this project is still offline. When I get it back up, I suggest checking out the source. It's really different from most JavaScript, but kind of sexy in its own way.

_________________
I have to return some video tapes.

Feed me a stray cat.


Top
 Profile  
Reply with quote  
 Post subject: Re: Javascript Snippets
PostPosted: Mon Feb 07, 2011 7:54 pm 
Offline
Krew (Admin)
Krew (Admin)
User avatar

Joined: Sun Oct 01, 2006 9:46 pm
Posts: 2331
Location: *poof*
Title: The Mad Hacker
Please post again when the server is back up; I would be interested in checking it out. =)





A dirty (programatically) script to skip Ads and "Queues" on http://www.king.com/
Code:
window.location="javascript:(function(){numQueue=0;goToTarget();})();"

My mom plays games there all the time. When I saw a game lasted 30 seconds and that there was completely false 1min "queues" and/or ads in between each round, I almost vomited.

If you had greasemonkey on firefox, you can set it to run on "http://www.king.com/*" and it will automatically skip the ads and queues.

OR you can set the code above as a bookmark location and click the bookmark between rounds.

_________________
Image


Top
 Profile  
Reply with quote  
 Post subject: Re: Javascript Snippets
PostPosted: Mon Feb 07, 2011 10:23 pm 
Offline
Krew (Admin)
Krew (Admin)
User avatar

Joined: Sun Oct 01, 2006 9:26 pm
Posts: 3768
Title: All in a day's work.
Greasemonkey is made of love. http://userscripts.org/users/49085 The SyntaxHighlighter userscript is fantastic, but I have an updated version on the server that's down. :x

_________________
I have to return some video tapes.

Feed me a stray cat.


Top
 Profile  
Reply with quote  
 Post subject: Re: Javascript Snippets
PostPosted: Wed Feb 09, 2011 10:06 pm 
Offline
Krew (Admin)
Krew (Admin)
User avatar

Joined: Sun Oct 01, 2006 9:46 pm
Posts: 2331
Location: *poof*
Title: The Mad Hacker
Sexy indeed.

So is userscripts.org - I just signed up.

and wrote a script:


RainyVolume
Volume control is frequently requested for http://RainyMood.com/ and this greasemonkey script I just wrote will let you have it:

Code:
// ==UserScript==
// @name           RainyVolume
// @namespace      http://kodewerx.org/
// @include        http://www.rainymood.com/
// ==/UserScript==
document.getElementsByTagNameNS("*","embed")[0].parentNode.innerHTML='' +
'<object width="300" height="16">' +
'<embed src="http://www.rainymood.com/audio/RainyMood.mp3" autostart="true" loop="true" width="300" height="16" ' +
'controller="true"></embed>' +
'</object>';


Easy install available: http://userscripts.org/scripts/show/96567

_________________
Image


Top
 Profile  
Reply with quote  
 Post subject: Re: Javascript Snippets
PostPosted: Fri Mar 18, 2011 10:20 pm 
Offline
Krew (Admin)
Krew (Admin)
User avatar

Joined: Sun Oct 01, 2006 9:26 pm
Posts: 3768
Title: All in a day's work.
The server is back up!

http://www.doodleearth.com/
http://parasyte.kodewerx.org/userscripts/

_________________
I have to return some video tapes.

Feed me a stray cat.


Top
 Profile  
Reply with quote  
 Post subject: Re: Javascript Snippets
PostPosted: Sat Mar 19, 2011 7:40 am 
Offline
Krew (Admin)
Krew (Admin)
User avatar

Joined: Sun Oct 01, 2006 9:46 pm
Posts: 2331
Location: *poof*
Title: The Mad Hacker
That Syntax Highlighter works in that it parses it, but the html just prints to my screen. D=

_________________
Image


Top
 Profile  
Reply with quote  
 Post subject: Re: Javascript Snippets
PostPosted: Sat Mar 19, 2011 12:59 pm 
Offline
Krew (Admin)
Krew (Admin)
User avatar

Joined: Sun Oct 01, 2006 9:26 pm
Posts: 3768
Title: All in a day's work.
lulz

It worked in Firefox 4.0 beta. Never did try it in other versions. And it definitely does not work in Chrome, even though it can be installed.

_________________
I have to return some video tapes.

Feed me a stray cat.


Top
 Profile  
Reply with quote  
 Post subject: Re: Javascript Snippets
PostPosted: Sun Mar 20, 2011 12:59 pm 
Offline
Kommunist
Kommunist
User avatar

Joined: Mon Jun 09, 2008 12:25 pm
Posts: 217
Location: Earth, I think
Do you know when it's comming out of beta? I'm using it too.

_________________
DO NOT CLICK HERE. YOU HAVE BEEN WARNED
Got a PS3? PM me your PSN.


Top
 Profile  
Reply with quote  
 Post subject: Re: Javascript Snippets
PostPosted: Tue Mar 22, 2011 8:09 pm 
Offline
Kommunist
Kommunist

Joined: Thu Nov 08, 2007 7:44 pm
Posts: 198
Chrome is actually a nice browser, too bad it doesn't work on it.


Top
 Profile  
Reply with quote  
 Post subject: Re: Javascript Snippets
PostPosted: Tue Mar 22, 2011 8:58 pm 
Offline
Krew (Admin)
Krew (Admin)
User avatar

Joined: Sun Oct 01, 2006 9:46 pm
Posts: 2331
Location: *poof*
Title: The Mad Hacker
Chrome's support for greasemonkey scripts is shit. I would consider switching if it matched FF.

_________________
Image


Top
 Profile  
Reply with quote  
 Post subject: Re: Javascript Snippets
PostPosted: Tue Mar 22, 2011 9:49 pm 
Offline
Krew (Admin)
Krew (Admin)
User avatar

Joined: Sun Oct 01, 2006 9:26 pm
Posts: 3768
Title: All in a day's work.
Firefox 4 was released today.

_________________
I have to return some video tapes.

Feed me a stray cat.


Top
 Profile  
Reply with quote  
 Post subject: Re: Javascript Snippets
PostPosted: Tue Jul 12, 2011 6:35 pm 
Offline
Krew (Admin)
Krew (Admin)
User avatar

Joined: Sun Oct 01, 2006 9:46 pm
Posts: 2331
Location: *poof*
Title: The Mad Hacker
I wrote a quick konami code thing a few months ago:

Code:
<script type="text/javascript">
<!--
var kkr="65663937393740403838";
var curst="";   
document.onkeyup = function(e){
    e = e || window.event;
    k = e.keyCode || e.which;
    curst=(k.toString()+curst).substr(0,20);
    if(curst==kkr) {
        //javascript to execute when konami code is entered
    }
    return true;};
//-->
</script>


I meant to post it sooner but I forgot

_________________
Image


Top
 Profile  
Reply with quote  
 Post subject: Re: Javascript Snippets
PostPosted: Mon Aug 04, 2014 3:58 am 
Offline
Krew (Admin)
Krew (Admin)
User avatar

Joined: Sun Oct 01, 2006 9:46 pm
Posts: 2331
Location: *poof*
Title: The Mad Hacker
Just wrote these sexy beasts:
Code:
   var byteReverse = function(hexByteStr) {
      return hexByteStr.match(/../g).reverse().join("");
   };
   byteReverse("38384040373937396665");

   var endianConvert = function(hexByteStr, wordSize) {
      var re = new RegExp("("+(new Array(~~((wordSize||32)/8)+1)).join("..")+")", "g");
      return hexByteStr.replace(re, function(s, m) { return byteReverse(m); });
   };

_________________
Image


Top
 Profile  
Reply with quote  
 Post subject: Re: Javascript Snippets
PostPosted: Thu Aug 21, 2014 11:30 am 
Offline
Krew (Admin)
Krew (Admin)
User avatar

Joined: Sun Oct 01, 2006 9:26 pm
Posts: 3768
Title: All in a day's work.
That reminds me of a few utility functions I wrote for something amazing:

Code:
/**
 * Pad a string on the left to specified width using given substring
 * @param {String} str Input string
 * @param {Number} width Minimum string width
 * @param {String} [pad=" "] Padding substring
 * @return {String} Padded string
 * @name padLeft
 * @method
 * @memberOf util
 */
exports.padLeft = function padLeft(str, width, pad) {
    pad = pad || " ";
    while (str.length < width) {
        str = pad + str;
    }
    return str;
};

/**
 * Pad a string on the right to specified width using given substring
 * @param {String} str Input string
 * @param {Number} width Minimum string width
 * @param {String} [pad=" "] Padding substring
 * @return {String} Padded string
 * @name padRight
 * @method
 * @memberOf util
 */
exports.padRight = function padRight(str, width, pad) {
    pad = pad || " ";
    while (str.length < width) {
        str += pad;
    }
    return str;
};

/**
 * Convert a number into hexadecimal notation
 * @param {Number} num Input number
 * @param {Number} [width=1] zero-padding width
 * @param {String} [prefix="0x"] Hex prefix
 * @return {String} Hexadecimal representation of input number
 * @name hex
 * @method
 * @memberOf util
 */
exports.hex = function hex(num, width, prefix) {
    return (typeof(prefix) === "undefined" ? "0x" : prefix) +
        exports.padLeft(num.toString(16), width || 1, "0");
};

_________________
I have to return some video tapes.

Feed me a stray cat.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 18 posts ] 

All times are UTC - 8 hours [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group