Advertisement
msangel

javascript templates

Jan 18th, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.  
  3. // programmers day
  4. if ("100"===Math.ceil(((d=new Date())-(new Date(d.getFullYear(),0,1)))/864e+5).toString(16)){
  5. console.log("party hard");
  6. }else {
  7. // do nothing
  8. }
  9.  
  10. // xPath javascript helper
  11. function xPathFunct(xPathExp){
  12. var resXPath = document.evaluate(xPathExp, document, null, XPathResult.ANY_TYPE, null);
  13. var result=resXPath.iterateNext();
  14. var resArray = [];
  15. while (result){
  16. resArray.push(result);
  17. result=resXPath.iterateNext();
  18. }
  19. return resArray;
  20. }
  21.  
  22.  
  23.  
  24. // delete all records from vkontakte responce panel by specific user
  25. i=1;xPathFunct("//div[@class='post_like_wrap fl_r']").forEach(function(d){if(/85555558/.test(d.outerHTML)){i++;setTimeout("d.onclick.apply(d)",1000*i);}})
  26.  
  27. // define property for quick access
  28. Object.defineProperty(Element.prototype,"_",{ get : function(){return this.childNodes;} })
  29.  
  30. // another way to iterate
  31. for (var i= element.childNodes.length-1; i>=0;i--) {
  32. for (var i= element.childNodes.length; i-->0;) {
  33.  
  34. // collect vk friend to one list (here html class property handle correct)
  35. (function collect(){var d={};xPathFunct("//div[contains(@class, 'user_block') and contains(@class, 'clear_fix')]/div[1]/div[2]/a[1]").forEach(function(a){console.log(a); d[a.href]=a.innerHTML;}); return d;})()
  36.  
  37.  
  38. // return unique properties of two object
  39. function comp(ob1, ob2){
  40. for(a in ob1){
  41. if(a in ob1 && a in ob2){
  42. delete ob2[a];
  43. delete ob1[a];
  44. }
  45. }
  46. return [ob1,ob2];
  47. }
  48. comp({a:1,b:2},{a:2,c:4})
  49.  
  50.  
  51. // scriptlet clicker by conditions
  52. Array.prototype.slice.call(document.getElementsByTagName('span')).filter(function(a){return /^exlink/.test(a.id);}).forEach(function(a){var link = a.getElementsByTagName('a')[0]; setTimeout(function(){link.onclick.apply(link)},100)});
  53.  
  54. // e-mail regexp
  55. [a-zA-Z0-9_.-]+@[a-zA-Z0-9_-]+\.[a-zA-Z].
  56.  
  57. // javascript string from char codes
  58. String.fromCharCode(1087).concat(String.fromCharCode(1088)).concat(String.fromCharCode(1080)).concat(String.fromCharCode(1074)).concat(String.fromCharCode(1077)).concat(String.fromCharCode(1090));
  59.  
  60.  
  61.  
  62.  
  63. // css positions demo
  64. http://jsfiddle.net/zYZRC/
  65.  
  66. // some xss vector
  67. <meta http-equiv="refresh" content="0;url='http://google.com/search?q=qweqwe'">
  68. <META HTTP-EQUIV="refresh" CONTENT="0;url=javascript:alert('XSS');">
  69.  
  70.  
  71. // valid XML
  72. <script>
  73. /*<![CDATA[*/
  74. function ololo(ip,sel,sz){
  75.  
  76. }
  77. /*]]>*/
  78. </script>
  79.  
  80.  
  81. // xml parser
  82. parser=new DOMParser();
  83. str = '<?xml version="1.0" ?> <note><to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Dont forget me this weekend!</body> </note>';
  84. xmlDoc=parser.parseFromString(str,"text/xml");
  85.  
  86. // array average
  87. Array.prototype.avg = function() {
  88. var av = 0;
  89. var cnt = 0;
  90. var len = this.length;
  91. for (var i = 0; i < len; i++) {
  92. var e = this[i];
  93. if (isFinite(e)){
  94. av += e;
  95. cnt++;
  96. }
  97. }
  98. if(cnt==0) return;
  99. return av/cnt;
  100. }
  101.  
  102. // Enums in javascript
  103. function Enum() {
  104. for (var i in arguments) {
  105. var cur = arguments[i];
  106. this[cur] = cur;
  107. }
  108. Object.freeze(this)
  109. }
  110. var YesNo = new Enum('Yes', 'No');
  111.  
  112. // from string to charcodes
  113. (function(data){
  114. res = []
  115. for(i=0;i<data.length;i++){
  116. res.push(data[i].charCodeAt(0));
  117. }
  118. // usage: String.fromCharCode(60, 105);
  119. return res;
  120. })("http://")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement