Subversion Repositories oidplus

Rev

Rev 1173 | View as "text/javascript" | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*
  2.  * OIDplus 2.0
  3.  * Copyright 2019 - 2022 Daniel Marschall, ViaThinkSoft
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17.  
  18. /*jshint esversion: 6 */
  19.  
  20. /* Misc functions */
  21.  
  22. function htmlDecodeWithLineBreaks(html) {
  23.         // https://stackoverflow.com/questions/4502673/jquery-text-function-loses-line-breaks-in-ie
  24.         // Added ".replace(/ /g,'')" because of GitHub bug #3
  25.         var breakToken = '_______break_______';
  26.         var lineBreakedHtml = html.replace(/&nbsp;/g,' ').replace(/&#160;/g,' ').replace(/<br\s?\/?>/gi, breakToken).replace(/<p\.*?>(.*?)<\/p>/gi, breakToken + '$1' + breakToken);
  27.         return $('<div>').html(lineBreakedHtml).text().replace(new RegExp(breakToken, 'g'), '\n');
  28. }
  29.  
  30. function copyToClipboard(elem) {
  31.         // Source: https://stackoverflow.com/questions/22581345/click-button-copy-to-clipboard-using-jquery
  32.         // Modified (see below)
  33.  
  34.         // TODO: this function causes the page to scroll!
  35.  
  36.         // create hidden text element, if it doesn't already exist
  37.         var targetId = "_hiddenCopyText_";
  38.         var isInput = elem.tagName === "INPUT" || elem.tagName === "TEXTAREA";
  39.         var origSelectionStart, origSelectionEnd;
  40.         if (isInput) {
  41.                 // can just use the original source element for the selection and copy
  42.                 target = elem;
  43.                 origSelectionStart = elem.selectionStart;
  44.                 origSelectionEnd = elem.selectionEnd;
  45.         } else {
  46.                 // must use a temporary form element for the selection and copy
  47.                 target = document.getElementById(targetId);
  48.                 if (!target) {
  49.                         var target = document.createElement("textarea");
  50.                         target.style.position = "absolute";
  51.                         target.style.left = "-9999px";
  52.                         target.style.top = "0";
  53.                         target.id = targetId;
  54.                         document.body.appendChild(target);
  55.                 }
  56.  
  57.                 // Changed by Daniel Marschall, 3 Oct 2022
  58.                 // "textContent" will swallow the line breaks of a <div>xx<br>xx</div>
  59.                 // htmlDecodeWithLineBreaks convert <br> to linebreaks but strips the other
  60.                 // HTML tags.
  61.                 // target.textContent = elem.textContent;
  62.                 target.textContent = htmlDecodeWithLineBreaks(elem.innerHTML);
  63.         }
  64.         // select the content
  65.         var currentFocus = document.activeElement;
  66.         target.focus();
  67.         target.setSelectionRange(0, target.value.length);
  68.  
  69.         // copy the selection
  70.         var succeed;
  71.         try {
  72.                 succeed = document.execCommand("copy");
  73.         } catch(e) {
  74.                 succeed = false;
  75.         }
  76.         // restore original focus
  77.         if (currentFocus && typeof currentFocus.focus === "function") {
  78.                 currentFocus.focus();
  79.         }
  80.  
  81.         if (isInput) {
  82.                 // restore prior selection
  83.                 elem.setSelectionRange(origSelectionStart, origSelectionEnd);
  84.         } else {
  85.                 // clear temporary content
  86.                 target.textContent = "";
  87.         }
  88.         return succeed;
  89. }
  90.  
  91. function getMeta(metaName) {
  92.         const metas = $('meta[name='+metaName+']');
  93.         return (metas.length == 0) ? '' : metas[0].content;
  94. }
  95.  
  96. String.prototype.explode = function (separator, limit) {
  97.         // https://stackoverflow.com/questions/4514323/javascript-equivalent-to-php-explode
  98.         const array = this.split(separator);
  99.         if (limit !== undefined && array.length >= limit) {
  100.                 array.push(array.splice(limit - 1).join(separator));
  101.         }
  102.         return array;
  103. };
  104.  
  105. String.prototype.htmlentities = function () {
  106.         return this.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;');//"
  107. };
  108.  
  109. String.prototype.html_entity_decode = function () {
  110.         return $('<textarea />').html(this).text();
  111. };
  112.  
  113. if (!String.prototype.replaceAll) {
  114.         /**
  115.          * String.prototype.replaceAll() polyfill
  116.          * https://gomakethings.com/how-to-replace-a-section-of-a-string-with-another-one-with-vanilla-js/
  117.          * @author Chris Ferdinandi
  118.          * @license MIT
  119.          */
  120.         String.prototype.replaceAll = function(str, newStr){
  121.                 // If a regex pattern
  122.                 if (Object.prototype.toString.call(str).toLowerCase() === '[object regexp]') {
  123.                         return this.replace(str, newStr);
  124.                 }
  125.                 // If a string
  126.                 return this.replace(new RegExp(str, 'g'), newStr);
  127.         };
  128. }
  129.  
  130. if (!String.prototype.startsWith) {
  131.         // https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith#polyfill
  132.         Object.defineProperty(String.prototype, 'startsWith', {
  133.                 value: function(search, rawPos) {
  134.                         var pos = rawPos > 0 ? rawPos|0 : 0;
  135.                         return this.substring(pos, pos + search.length) === search;
  136.                 }
  137.         });
  138. }
  139.  
  140.  
  141.  
  142.  
  143. function jumpToAnchor(anchor) {
  144.         window.location.href = "#" + anchor;
  145. }
  146.  
  147. function getCookie(cname) {
  148.         // Source: https://www.w3schools.com/js/js_cookies.asp
  149.         var name = cname + "=";
  150.         var decodedCookie = decodeURIComponent(document.cookie);
  151.         var ca = decodedCookie.split(';');
  152.         for(var i = 0; i <ca.length; i++) {
  153.                 var c = ca[i];
  154.                 while (c.charAt(0) == ' ') {
  155.                         c = c.substring(1);
  156.                 }
  157.                 if (c.indexOf(name) == 0) {
  158.                         return c.substring(name.length, c.length);
  159.                 }
  160.         }
  161.         return undefined;
  162. }
  163.  
  164. function setCookie(cname, cvalue, exdays, path) {
  165.         var d = new Date();
  166.         d.setTime(d.getTime() + (exdays*24*60*60*1000));
  167.         var expires = exdays == 0 ? "" : "; expires="+d.toUTCString();
  168.         document.cookie = cname + "=" + cvalue + expires + ";path=" + path + ";SameSite=" + samesite_policy;
  169. }
  170.  
  171. function isNull(val, def) {
  172.         // Deprecated. Use "val??def" instead of isNull(val,def)
  173.         if (val == null) {
  174.                 // since null==undefined, this also works with undefined
  175.                 return def;
  176.         } else {
  177.                 return val;
  178.         }
  179. }
  180.  
  181. function btoa(bin) {
  182.         var tableStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  183.         var table = tableStr.split("");
  184.         for (var i = 0, j = 0, len = bin.length / 3, base64 = []; i < len; ++i) {
  185.                 var a = bin.charCodeAt(j++), b = bin.charCodeAt(j++), c = bin.charCodeAt(j++);
  186.                 if ((a | b | c) > 255) throw new Error(_L('String contains an invalid character'));
  187.                 base64[base64.length] = table[a >> 2] + table[((a << 4) & 63) | (b >> 4)] +
  188.                                        (isNaN(b) ? "=" : table[((b << 2) & 63) | (c >> 6)]) +
  189.                                        (isNaN(b + c) ? "=" : table[c & 63]);
  190.         }
  191.         return base64.join("");
  192. };
  193.  
  194. function hexToBase64(str) {
  195.         return btoa(String.fromCharCode.apply(null,
  196.                     str.replace(/\r|\n/g, "").replace(/([\da-fA-F]{2}) ?/g, "0x$1 ").replace(/ +$/, "").split(" ")));
  197. }
  198.  
  199. function _b64EncodeUnicode(str) {
  200.         if (str == "") {
  201.                 return "''";
  202.         } else {
  203.                 return "base64_decode('"+b64EncodeUnicode(str)+"')";
  204.         }
  205. }
  206.  
  207. function b64EncodeUnicode(str) {
  208.         // first we use encodeURIComponent to get percent-encoded UTF-8,
  209.         // then we convert the percent encodings into raw bytes which
  210.         // can be fed into btoa.
  211.         return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g,
  212.         function toSolidBytes(match, p1) {
  213.                 return String.fromCharCode('0x' + p1);
  214.         }));
  215. }
  216.  
  217. function generateRandomString(length) {
  218.         var charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789",
  219.         retVal = "";
  220.         for (var i = 0, n = charset.length; i < length; ++i) {
  221.                 retVal += charset.charAt(Math.floor(Math.random() * n));
  222.         }
  223.         return retVal;
  224. }
  225.  
  226. function RemoveLastDirectoryPartOf(the_url) {
  227.         var the_arr = the_url.split('/');
  228.         if (the_arr.pop() == '') the_arr.pop();
  229.         return( the_arr.join('/') );
  230. }
  231.  
  232. function jsString(str) {
  233.         return "'" + str.replace("'", "\\'") + "'";
  234. }
  235.