Kodewerx https://www.kodewerx.org/forum/ |
|
Javascript Snippets https://www.kodewerx.org/forum/viewtopic.php?f=24&t=7414 |
Page 1 of 1 |
Author: | James0x57 [ Sun Oct 17, 2010 5:23 pm ] |
Post subject: | Javascript Snippets |
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. |
Author: | Parasyte [ Sun Oct 17, 2010 10:43 pm ] |
Post subject: | Re: Javascript Snippets |
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. |
Author: | James0x57 [ Mon Oct 18, 2010 7:20 pm ] |
Post subject: | Re: Javascript Snippets |
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! |
Author: | Hextator [ Tue Jan 25, 2011 1:35 am ] |
Post subject: | Re: Javascript Snippets |
Shouldn't it be "JavaScript"- |
Author: | Parasyte [ Mon Feb 07, 2011 1:40 am ] |
Post subject: | Re: Javascript Snippets |
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. |
Author: | James0x57 [ Mon Feb 07, 2011 7:54 pm ] |
Post subject: | Re: Javascript Snippets |
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. |
Author: | Parasyte [ Mon Feb 07, 2011 10:23 pm ] |
Post subject: | Re: Javascript Snippets |
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. |
Author: | James0x57 [ Wed Feb 09, 2011 10:06 pm ] |
Post subject: | Re: Javascript Snippets |
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 |
Author: | Parasyte [ Fri Mar 18, 2011 10:20 pm ] |
Post subject: | Re: Javascript Snippets |
The server is back up! http://www.doodleearth.com/ http://parasyte.kodewerx.org/userscripts/ |
Author: | James0x57 [ Sat Mar 19, 2011 7:40 am ] |
Post subject: | Re: Javascript Snippets |
That Syntax Highlighter works in that it parses it, but the html just prints to my screen. D= |
Author: | Parasyte [ Sat Mar 19, 2011 12:59 pm ] |
Post subject: | Re: Javascript Snippets |
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. |
Author: | DarkLegend [ Sun Mar 20, 2011 12:59 pm ] |
Post subject: | Re: Javascript Snippets |
Do you know when it's comming out of beta? I'm using it too. |
Author: | oohway [ Tue Mar 22, 2011 8:09 pm ] |
Post subject: | Re: Javascript Snippets |
Chrome is actually a nice browser, too bad it doesn't work on it. |
Author: | James0x57 [ Tue Mar 22, 2011 8:58 pm ] |
Post subject: | Re: Javascript Snippets |
Chrome's support for greasemonkey scripts is shit. I would consider switching if it matched FF. |
Author: | Parasyte [ Tue Mar 22, 2011 9:49 pm ] |
Post subject: | Re: Javascript Snippets |
Firefox 4 was released today. |
Author: | James0x57 [ Tue Jul 12, 2011 6:35 pm ] |
Post subject: | Re: Javascript Snippets |
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 |
Author: | James0x57 [ Mon Aug 04, 2014 3:58 am ] |
Post subject: | Re: Javascript Snippets |
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); }); }; |
Author: | Parasyte [ Thu Aug 21, 2014 11:30 am ] |
Post subject: | Re: Javascript Snippets |
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"); }; |
Page 1 of 1 | All times are UTC - 8 hours [ DST ] |
Powered by phpBB® Forum Software © phpBB Group https://www.phpbb.com/ |