Advertisement
erinx

kscript.js

Jan 25th, 2021
993
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function loadK(){
  2.  
  3.   var d, db, h, namespace, $, $$, version;
  4.  
  5.   d = document;
  6.   db = document.body;
  7.   h = document.getElementsByTagName('head')[0];
  8.  
  9.   namespace = "kScript";
  10.   version = 0.0;
  11.  
  12.   $ = function(s, p){
  13.     if(!p || p == null){
  14.       p = db;
  15.     }
  16.     return p.querySelector(s);
  17.   };
  18.  
  19.   $$ = function(s, p){
  20.     if(!p || p == null){
  21.       p = db;
  22.     }
  23.     return p.querySelectorAll(s);
  24.   };
  25.  
  26.   $.getVal = function(k, v){
  27.     if(typeof(Storage) !=="undefined"){
  28.       if(v == null){
  29.         if(localStorage.getItem(namespace + "." + k) != null){
  30.           return localStorage.getItem(namespace  + "." + k);
  31.         }else{
  32.           return 'undefined';
  33.         }
  34.       }else{
  35.         if(localStorage.getItem(namespace + "." + k) != null){
  36.           return localStorage.getItem(namespace + "." + k);
  37.         }else{
  38.           return v;
  39.         }
  40.       }
  41.     }else{
  42.       return 'storage unavailable';
  43.     }
  44.   };
  45.  
  46.   $.setVal = function(k, v){
  47.     if(typeof(Storage) !=="undefined"){
  48.       if(v == null){
  49.         return 'undefined';
  50.       }else{
  51.         return localStorage.setItem(namespace  + "." + k, v);
  52.       }
  53.     }else{
  54.       return 'storage unavailable';
  55.     }
  56.   };
  57.  
  58.   $.delVal = function(k){
  59.     return localStorage.removeItem(namespace + "." + k);
  60.   };
  61.  
  62.   $.elm = function(t, a, s){
  63.     var e = d.createElement(t);
  64.     if(a){
  65.       for (key in a){
  66.         e.setAttribute(key, a[key]);
  67.       }
  68.     }
  69.     if(s){
  70.       s.appendChild(e);
  71.     }
  72.     return e;
  73.   };
  74.  
  75.   $.htm = function(s, v){
  76.     if(v == null){
  77.       return s.innerHTML;
  78.     }else{
  79.       s.innerHTML = v;
  80.     }
  81.     return s;
  82.   };
  83.  
  84.   $.each = function(a, c, e){
  85.     for(var i = 0; i < a.length; i++){
  86.       c(a[i], i);
  87.       if(i == (a.length)-1){
  88.         if(e && e != null){
  89.           return e(a[i], i);
  90.         }else{
  91.           return a;
  92.         }
  93.       }
  94.     }
  95.   };
  96.  
  97.   $.xhr = function(t, u, i, c, p){
  98.     if(i != null){
  99.       if(t == 'POST'){
  100.         var xd = new FormData();
  101.         for (key in i){
  102.           xd.append(key, i[key]);
  103.         }
  104.       }else{
  105.         xd = '?';
  106.         for (key in i){
  107.           xd += key + '=' + i[key] + '&';
  108.         }
  109.         xd = xd.substring(0, (xd.length-1));
  110.         u += xd;
  111.       }
  112.     }
  113.     var x = new XMLHttpRequest();
  114.     x.open(t, u);
  115.     if(p != null){
  116.       for (key in p){
  117.         x.setRequestHeader(key, p[key]);
  118.       }
  119.     }
  120.     x.onreadystatechange = function(){
  121.       if (x.readyState == 4) {
  122.         return c(x);
  123.       }
  124.     }
  125.     if(t == 'POST' && i != null){
  126.       x.send(xd);
  127.     }else{
  128.       x.send();
  129.     }
  130.   };
  131.  
  132.   $.JSON = function(s){
  133.     if(typeof s == 'string'){
  134.       return JSON.parse(s);
  135.     }else{
  136.       return JSON.stringify(s);
  137.     }
  138.   };
  139.  
  140.   $.time = function(t, c, l){
  141.     if(c == false){
  142.       return clearInterval(t);
  143.     }else{
  144.       if(l == true){
  145.         return setInterval(function(){
  146.           c();
  147.         },t);
  148.       }else{
  149.         return setTimeout(function(){
  150.           c();
  151.         },t);
  152.       }
  153.     }
  154.   };
  155.  
  156.   $.val = function(s, v){
  157.     if(!v || v == null){
  158.       return s.value;
  159.     }else{
  160.       s.value = v;
  161.     }
  162.     return s;
  163.   };
  164.  
  165.   $.event = function(t, i, s){
  166.     if (i == null) {
  167.       i = {};
  168.     }
  169.     if (s == null) {
  170.       s = d;
  171.     }
  172.     return s.dispatchEvent(new CustomEvent(t, i));
  173.   };
  174.  
  175.   $.att = function(s, a, v) {
  176.     if (!v || v == null || v == false) {
  177.       if (v == false) {
  178.         return s.removeAttribute(a);
  179.       } else {
  180.         return s.getAttribute(a);
  181.       }
  182.     } else {
  183.       s.setAttribute(a, v);
  184.       return s;
  185.     }
  186.   };
  187.  
  188.   $.destroy = function(s) {
  189.     return s.parentNode.removeChild(s);
  190.   };
  191.  
  192.   $.css = function(s){
  193.     var e = d.createElement('style');
  194.     e.type = 'text/css';
  195.     if (e.styleSheet){
  196.       e.styleSheet.cssText = s;
  197.     } else {
  198.       e.appendChild(d.createTextNode(s));
  199.     }
  200.     h.appendChild(e);
  201.     return e;
  202.   };
  203.  
  204.   $.onCursor = function(myField, myValue){
  205.     if (document.selection) {
  206.       myField.focus();
  207.       sel = document.selection.createRange();
  208.       sel.text = myValue;
  209.     }else if(myField.selectionStart || myField.selectionStart == '0'){
  210.       var startPos = myField.selectionStart;
  211.       var endPos = myField.selectionEnd;
  212.       myField.value = myField.value.substring(0, startPos)
  213.       + myValue
  214.       + myField.value.substring(endPos, myField.value.length);
  215.     }else{
  216.       myField.value += myValue;
  217.     }
  218.   };
  219.  
  220.   $.after = function(e, s){
  221.     s.parentNode.insertBefore(e, s.nextSibling);
  222.     return e;
  223.   };
  224.  
  225.   $.before = function(e, s){
  226.     s.parentNode.insertBefore(e, s);
  227.     return e;
  228.   };
  229.  
  230.   $.getSelected = function(){
  231.     var text = "";
  232.     if (window.getSelection) {
  233.       text = window.getSelection().toString();
  234.     } else if (document.selection && document.selection.type != "Control") {
  235.       text = document.selection.createRange().text;
  236.     }
  237.     return text;
  238.   };
  239.  
  240.   $.parLines = function(s){
  241.     s = s.replace(/<\/p>/g, " ");
  242.     return s;    
  243.   };
  244.  
  245.   $.stripTags = function(inputtags, allowedtags){
  246.     allowedtags = (((allowedtags || '') + '')
  247.     .toLowerCase()
  248.     .match(/<[a-z][a-z0-9]*>/g) || [])
  249.     .join('');
  250.   var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi,
  251.     commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi;
  252.   return inputtags.replace(commentsAndPhpTags, '')
  253.     .replace(tags, function($0, $1) {
  254.       return allowedtags.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : '';
  255.     });
  256.   };
  257.  
  258.   $.stripMore = function(s){
  259.     s = s.replace(/&gt;/g, ">");
  260.     s = s.replace(/&lt;/g, "<");
  261.     s = s.replace(/&nbsp;/g, " ");
  262.     return s;
  263.   };
  264.  
  265.   $.anchorJump = function(h){
  266.     var url = location.href;
  267.     location.href = "#"+h;
  268.     history.replaceState(null,null,url);
  269.   };
  270.  
  271.   $.elmJump = function(s){
  272.     window.scrollTo(0, s.offsetTop);
  273.   };
  274.  
  275. }
  276. loadK();
  277.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement