November Twenty-Four Two Thousand Eleven
Thanksgiving & Codes & Grover!Tweet
Happy Thanksgiving Interwebs! Happy Birthday Grover!
I'm thankful that programming makes sense to me!
I've got a couple snippets to share, One of is some PHP+AJAX to grab the title element from a given url, mostly just so I don't have to wait for a page to load to check a link.
See: The File
Code:
| 1 | <?php |
| 2 | error_reporting(!E_WARNING); |
| 3 | $target = $_POST['url'];//'http://tylerthomas.me'; |
| 4 | $pass = preg_match('#(http|ftp|https)://[w-_]+(.[w-_]+)+([w-.,@?^=%&:/~+#]*[w-@?^=%&/~+#])?#i',$target); |
| 5 | if($pass){ |
| 6 | $domdocument = new domdocument(); |
| 7 | $source = file_get_contents($target); |
| 8 | $domdocument->loadHTMLFile($target); |
| 9 | $title = $domdocument->getElementsByTagName('title'); |
| 10 | echo $title->item(0)->nodeValue; |
| 11 | }else{ |
| 12 | die('FAILURE - BAD URL: ' . $target); |
| 13 | } |
| 14 | ?> |
See: The File
Code:
| 1 | <html> |
| 2 | <head> |
| 3 | <title>Grab Title</title> |
| 4 | <script type="text/javascript"> |
| 5 | function AJAX(url, varString){ |
| 6 | var xmlHttp=null; |
| 7 | try |
| 8 | { |
| 9 | // Firefox, Opera 8.0+, Safari |
| 10 | xmlHttp=new XMLHttpRequest(); |
| 11 | } |
| 12 | catch (e) |
| 13 | { |
| 14 | // Internet Explorer |
| 15 | try |
| 16 | { |
| 17 | xmlHttp=new ActiveXObject("Msxml2.XMLHTTP"); |
| 18 | } |
| 19 | catch (e) |
| 20 | { |
| 21 | xmlHttp=new ActiveXObject("Microsoft.XMLHTTP"); |
| 22 | } |
| 23 | } |
| 24 | if (xmlHttp==null) |
| 25 | { |
| 26 | alert ("Your browser does not support AJAX!"); |
| 27 | return; |
| 28 | } |
| 29 | |
| 30 | xmlHttp.onreadystatechange = function() { |
| 31 | if (xmlHttp.readyState == 4 && xmlHttp.status==200){ |
| 32 | |
| 33 | document.getElementById('title').innerHTML = xmlHttp.responseText; //Do something iwth response if you like. |
| 34 | |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | xmlHttp.open("POST",url,true); |
| 39 | xmlHttp.setRequestHeader("Content-type","application/x-www-form-urlencoded"); |
| 40 | xmlHttp.send(varString); |
| 41 | } |
| 42 | function grabTitle(url){ |
| 43 | //alert('ha'); |
| 44 | AJAX('grabtitle.php','url='+url) |
| 45 | } |
| 46 | |
| 47 | </script> |
| 48 | </head> |
| 49 | <body onload="" onclick=""> |
| 50 | |
| 51 | <h3>Grab a page title</h3> |
| 52 | <p>enter a url starting with http://, hit enter</p> |
| 53 | <form onsubmit="grabTitle(document.getElementById('url').value);return false;"> |
| 54 | <input type="text" id="url" value="" /> |
| 55 | </form> |
| 56 | <div id="title"></div> |
| 57 | </body> |
| 58 | </html> |
And another is mostly just me playing in object oriented JS, but it's an interesting perspective on bandwidth.
See: The File
Code:
| 1 | |
| 2 | <html> |
| 3 | <head> |
| 4 | <script type="text/javascript"> |
| 5 | var DLDATA = function(){ |
| 6 | this.container = document.getElementById('container'); |
| 7 | this.first = false; |
| 8 | this.second = false; |
| 9 | this.rangeIDstr = 'ratesizetime';//used for the ranking of sliders to decide math, pretty sexy |
| 10 | |
| 11 | this.recalc = function(slider, reset){ |
| 12 | /*default params*/ |
| 13 | reset = reset || false; |
| 14 | /*end default params*/ |
| 15 | if(this.first && this.second && this.second != id){ |
| 16 | this.first = this.second; |
| 17 | this.second = false; |
| 18 | this.rangeIDstr = 'ratesizetime'; |
| 19 | this.rangeIDstr = this.removeStr(this.first, this.rangeIDstr); |
| 20 | return; |
| 21 | } |
| 22 | /*prelims*/ |
| 23 | rate = document.getElementById('rate'); |
| 24 | rateval = document.getElementById('rateval'); |
| 25 | raterank = document.getElementById('raterank'); |
| 26 | size = document.getElementById('size'); |
| 27 | sizeval = document.getElementById('sizeval'); |
| 28 | sizerank = document.getElementById('sizerank'); |
| 29 | time = document.getElementById('time'); |
| 30 | timeval = document.getElementById('timeval'); |
| 31 | timerank = document.getElementById('timerank'); |
| 32 | if(reset){//reset if done setting second slider, and attempting to move more |
| 33 | this.first = false; |
| 34 | this.second = false; |
| 35 | this.rangeIDstr = 'ratesizetime'; |
| 36 | raterank.innerHTML = ''; |
| 37 | rateval.innerHTML = ''; |
| 38 | sizerank.innerHTML = ''; |
| 39 | sizeval.innerHTML = ''; |
| 40 | timerank.innerHTML = ''; |
| 41 | timeval.innerHTML = ''; |
| 42 | size.value = "50%"; |
| 43 | time.value = "50%"; |
| 44 | rate.value = "50%"; |
| 45 | return; |
| 46 | }//else, as you were |
| 47 | |
| 48 | id = slider.getAttribute('id'); |
| 49 | |
| 50 | |
| 51 | switch(id){//self updates for sliders |
| 52 | case 'rate': |
| 53 | document.getElementById(id + 'val').innerHTML = this.formatSize(slider.value); |
| 54 | break; |
| 55 | case 'size': |
| 56 | document.getElementById(id + 'val').innerHTML = this.formatSize(slider.value); |
| 57 | break; |
| 58 | case 'time': |
| 59 | document.getElementById(id + 'val').innerHTML = this.formatTime(slider.value); |
| 60 | break; |
| 61 | default: |
| 62 | alert(id + ': wtffff'); |
| 63 | break |
| 64 | } |
| 65 | |
| 66 | |
| 67 | |
| 68 | if(!this.first && !this.second){//first slider, only update own value |
| 69 | this.first = id; |
| 70 | //document.getElementById(id + 'rank').innerHTML = 1;//debug |
| 71 | this.rangeIDstr = this.removeStr(id, this.rangeIDstr); |
| 72 | } |
| 73 | if(!this.second && this.first && this.first != id){ //second slider, third is known, begin updating both self and third |
| 74 | this.second = id; |
| 75 | //document.getElementById(id + 'rank').innerHTML = 2;//debug |
| 76 | this.rangeIDstr = this.removeStr(id, this.rangeIDstr); |
| 77 | } |
| 78 | if (this.first && this.second){ |
| 79 | //document.getElementById(this.rangeIDstr + 'rank').innerHTML = 3;//debug |
| 80 | switch (this.rangeIDstr){//choose leftover one, alter accordingly. |
| 81 | case 'rate': |
| 82 | rate.value = size.value / time.value //bytes yo. |
| 83 | rateval.innerHTML = this.formatSize(rate.value); |
| 84 | break; |
| 85 | case 'size': |
| 86 | size.value = (rate.value * time.value); |
| 87 | sizeval.innerHTML = this.formatSize(size.value); |
| 88 | break; |
| 89 | case 'time': |
| 90 | time.value = size.value / rate.value; |
| 91 | timeval.innerHTML = this.formatTime(time.value); |
| 92 | break; |
| 93 | default: |
| 94 | alert(this.rangeIDstr); |
| 95 | break; |
| 96 | } |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | this.addLeadingZero = function(digit){//mhm more bs |
| 101 | if(digit < 10){ |
| 102 | digit = "0"+digit |
| 103 | } |
| 104 | return Math.round(digit, 2); |
| 105 | } |
| 106 | |
| 107 | this.formatSize = function(INTbytes){// no more conversions please this is ridic |
| 108 | f_GB = Math.floor(INTbytes / Math.pow(1024,3)); |
| 109 | INTbytes -= (f_GB * Math.pow(1024,3)); |
| 110 | f_MB = Math.floor(INTbytes / Math.pow(1024,2)); |
| 111 | INTbytes -= (f_MB * Math.pow(1024,2)); |
| 112 | f_KB = Math.floor(INTbytes / (1024)); |
| 113 | INTbytes -= (f_KB * (1024)); |
| 114 | f_B = Math.floor(INTbytes / 1); |
| 115 | f_size = this.addLeadingZero(f_GB) + ' GB : ' + this.addLeadingZero(f_MB) + ' MB : ' + this.addLeadingZero(f_KB) + ' KB : ' + this.addLeadingZero(f_B) + ' B'; |
| 116 | |
| 117 | |
| 118 | return f_size; |
| 119 | |
| 120 | |
| 121 | } |
| 122 | |
| 123 | this.formatTime = function(INTseconds){//convert seconds into days/hrs/min/sec without that pesky time library? ugh. |
| 124 | f_days = Math.floor(INTseconds / (60*60*24)); |
| 125 | INTseconds -= (f_days * (60*60*24)); |
| 126 | f_hours = Math.floor(INTseconds / (60*60)); |
| 127 | INTseconds -= (f_hours * (60*60)); |
| 128 | f_minutes = Math.floor(INTseconds / (60)); |
| 129 | INTseconds -= (f_minutes * (60)); |
| 130 | f_seconds = Math.floor(INTseconds / 1); |
| 131 | f_time = this.addLeadingZero(f_days) + ' Days : ' + this.addLeadingZero(f_hours) + ' Hours : ' + this.addLeadingZero(f_minutes) + ' Minutes : ' + this.addLeadingZero(f_seconds) + ' Seconds'; |
| 132 | return f_time; |
| 133 | } |
| 134 | |
| 135 | this.removeStr = function(needle, haystack){//om nom nom nom |
| 136 | gage = needle.length; |
| 137 | firstHalf = haystack.substr(0, haystack.indexOf(needle)); |
| 138 | secondHalf = haystack.substr(haystack.indexOf(needle) + gage); |
| 139 | return firstHalf + secondHalf; |
| 140 | } |
| 141 | |
| 142 | this.debugInfo = function(variables){//fucking sexy to work with |
| 143 | debugDiv = document.getElementById('debugDiv'); |
| 144 | debugStr = ''; |
| 145 | for(var i in variables){ |
| 146 | debugStr += i + ': ' + variables[i] + '<br />'; |
| 147 | } |
| 148 | debugDiv.innerHTML = debugStr; |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | var DL = new DLDATA();//instantiate |
| 153 | </script> |
| 154 | <style> |
| 155 | input{ |
| 156 | width:100%; |
| 157 | } |
| 158 | *{ |
| 159 | font-size:24px; |
| 160 | } |
| 161 | </style> |
| 162 | </head> |
| 163 | <body onload="var DL = new DLDATA();" > |
| 164 | <h2>The Amazing Dial-Up Investigator Thing!</h2> |
| 165 | <button onclick="DL.recalc(false,true);">ReseT</button><br /> |
| 166 | <span id="raterank"></span>Rate: <span id="rateval"></span><input type="range" min="0" max="1048576" step="1024" value="50%" onchange="DL.recalc(this);" id="rate" /><br /> |
| 167 | <span id="sizerank"></span>Size: <span id="sizeval"></span><input type="range" min="0" max="1073741824" step="1048576" value="50%" onchange="DL.recalc(this);" id="size" /><br /> |
| 168 | <span id="timerank"></span>Days: <span id="timeval"></span><input type="range" min="0" max="259200" step="900" value="50%" onchange="DL.recalc(this);" id="time" /> |
| 169 | <div id="container"></div> |
| 170 | <div id="debugDiv"></div> |
| 171 | </body> |
| 172 | </html> |
'Sall I got for now, Gonna make butterscotch toffee for today's meal. Cannot wait to gorge. Peace webs.
Oh here's More!Tweet
I decided to do some animations, starting work on a little library for translations, resizes.
Code:
| 1 | <html> |
| 2 | <head> |
| 3 | <script type="text/javascript" src="smoother.js"> |
| 4 | |
| 5 | </script> |
| 6 | <style> |
| 7 | |
| 8 | </style> |
| 9 | </head> |
| 10 | <body> |
| 11 | <button onclick="toolkit.smooth('500px', 'width')">long</button> |
| 12 | <button onclick="toolkit.smooth('100px', 'width')">short</button> |
| 13 | <button onclick="toolkit.smooth('10px', 'height')">fat</button> |
| 14 | <button onclick="toolkit.smooth('1px', 'height')">thin</button> |
| 15 | <button onclick="toolkit.cfg.smooth.bln_stop = true;">Smoother.cfg.smooth.stop()</button><br /> |
| 16 | speed<input id="speed" type="range" onchange="updateSelf(this)" min="0" max="10" step="1" value="1" /><span id="speeddiv"></span> |
| 17 | <div id="test" style="width:20px;height:5px;background-color:#1c1c1c;" > </div> |
| 18 | <script type="text/javascript"> |
| 19 | hr = document.getElementById('test'); |
| 20 | var toolkit = new Smoother('toolkit', hr); |
| 21 | </script> |
| 22 | </body> |
| 23 | </html> |
and more
Code:
| 1 | |
| 2 | //smooth transition from x to y, various transitions. |
| 3 | var Smoother = function(varname, targetObject){ |
| 4 | /*construct*/ |
| 5 | this.cfg = {}; |
| 6 | this.cfg.smooth = { |
| 7 | "str_echo":null, |
| 8 | "int_target":0, |
| 9 | "int_fps":30, |
| 10 | "int_modifier":0, |
| 11 | "int_speed":1,//percentage, 1 = 100% = 1px/frame |
| 12 | "ref_interval":null, |
| 13 | "bln_stop":false, |
| 14 | "str_mode":'', |
| 15 | "bln_busy":false |
| 16 | }; |
| 17 | /*end construct*/ |
| 18 | |
| 19 | this.smooth = function(target, mode){ |
| 20 | |
| 21 | this.cfg.smooth.bln_stop = false; |
| 22 | |
| 23 | this.cfg.smooth.str_mode = mode; |
| 24 | this.cfg.smooth.str_echo = (mode == 'width') ? targetObject.style.width: targetObject.style.height; |
| 25 | this.cfg.smooth.int_target = target; |
| 26 | |
| 27 | if(parseInt(this.cfg.smooth.str_echo) < parseInt(this.cfg.smooth.int_target) && this.cfg.smooth.int_modifier == 0){ |
| 28 | this.cfg.smooth.int_modifier = 1; |
| 29 | }else if(parseInt(this.cfg.smooth.str_echo) > parseInt(this.cfg.smooth.int_target) && this.cfg.smooth.int_modifier == 0){ |
| 30 | this.cfg.smooth.int_modifier = -1; |
| 31 | } |
| 32 | |
| 33 | console.log(this.cfg.smooth.int_modifier); |
| 34 | fpsConvert = 1000/this.cfg.smooth.int_fps; |
| 35 | |
| 36 | if(!this.cfg.smooth.bln_busy){ |
| 37 | this.cfg.smooth.bln_busy = true; |
| 38 | this.cfg.smooth.ref_interval = setInterval(varname+".smoothLoop()", fpsConvert); |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | this.smoothLoop = function(){ |
| 43 | /*stop rules*/ |
| 44 | if(this.cfg.smooth.int_modifier == 1){ |
| 45 | if(parseInt(this.cfg.smooth.str_echo) >= parseInt(this.cfg.smooth.int_target)){ |
| 46 | this.cfg.smooth.bln_stop = true; |
| 47 | } |
| 48 | }else if(this.cfg.smooth.int_modifier == -1){ |
| 49 | if(parseInt(this.cfg.smooth.str_echo) <= parseInt(this.cfg.smooth.int_target)){//passed target? you done fucked up. |
| 50 | this.cfg.smooth.bln_stop = true; |
| 51 | } |
| 52 | } |
| 53 | /*end stop rules*/ |
| 54 | |
| 55 | if(this.cfg.smooth.bln_stop){//stop it if its flagged to be stopped, do shutdown |
| 56 | clearInterval(this.cfg.smooth.ref_interval); |
| 57 | this.cfg.smooth.bln_busy = false; |
| 58 | this.cfg.smooth.int_modifier = 0; |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | this.cfg.smooth.int_speed = document.getElementById('speed').value, |
| 63 | patt = /[a-zA-Z][^0-9]+/g; |
| 64 | suffix = patt.exec(this.cfg.smooth.str_echo); |
| 65 | this.cfg.smooth.str_echo = parseInt(this.cfg.smooth.str_echo) + this.cfg.smooth.int_modifier * this.cfg.smooth.int_speed;//change the output: x = x + (+/- modifier * px/frame) |
| 66 | this.cfg.smooth.str_echo = this.cfg.smooth.str_echo + suffix; |
| 67 | if(this.cfg.smooth.str_mode == 'width') { |
| 68 | targetObject.style.width = this.cfg.smooth.str_echo; |
| 69 | }else if(this.cfg.smooth.str_mode == 'height'){ |
| 70 | targetObject.style.height = this.cfg.smooth.str_echo; |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | function updateSelf(obj){ |
| 75 | div = obj.getAttribute('id').concat('div'); |
| 76 | document.getElementById(div).innerHTML = document.getElementById(obj.getAttribute('id')).value; |
| 77 | } |
| 78 |

