common.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550
  1. /**
  2. * Copyright 2021, 2024 5 Mode
  3. *
  4. * This file is part of SnipSwap.
  5. *
  6. * SnipSwap is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * SnipSwap is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with SnipSwap. If not, see <https://www.gnu.org/licenses/>.
  18. *
  19. * commons.js
  20. *
  21. * Common constants and functions
  22. *
  23. * @author Daniele Bonini <my25mb@aol.com>
  24. * @copyrights (c) 2016, 2024, 5 Mode
  25. */
  26. var blinkTimeoutID = 0;
  27. bFullImageVisible = false;
  28. /**
  29. * Manage the Back/Forward/Refresh button press event is some specific
  30. * conditions such as:
  31. * - inside fullscreen image views
  32. *
  33. * @param {Event} e The window beforeUnoad event object
  34. * @returns {String} the message to show to the user
  35. */
  36. function beforeUnload(e) {
  37. if (bFullImageVisible) {
  38. var confMsg = "\o/";
  39. e.returnValue = confMsg;
  40. e.preventDefault();
  41. return confMsg;
  42. }
  43. }
  44. /**
  45. * Blink the the given text
  46. *
  47. * @param {string} tagID the tag ID containing the text to blink
  48. * @param {string} color the color of the text
  49. * @param {int} interval the interval of the "flashing"
  50. * @returns {void}
  51. */
  52. function blinkMe(tagID, color, interval) {
  53. if ( $(tagID) ) {
  54. if ($(tagID).css("color") !== "transparent") {
  55. color = $(tagID).css("color");
  56. $(tagID).css("color", "transparent");
  57. } else {
  58. $(tagID).css("color", color.toString());
  59. }
  60. }
  61. setTimeout("blinkMe()", interval, tagID, color, interval);
  62. }
  63. /**
  64. * Blink the the given text
  65. *
  66. * @param {string} tagID the tag ID containing the text to blink
  67. * @param {int} interval the interval of the "flashing"
  68. * @returns {void}
  69. */
  70. function blinkMe2(tagID, interval) {
  71. if (blinkTimeoutID!==0) {
  72. clearTimeout(blinkTimeoutID);
  73. }
  74. if ( $(tagID) ) {
  75. if ($(tagID).css("visibility") !== "hidden") {
  76. $(tagID).css("visibility", "hidden");
  77. } else {
  78. $(tagID).css("visibility", "visible");
  79. }
  80. }
  81. blinkTimeoutID = setTimeout(blinkMe2, interval, tagID, interval);
  82. }
  83. /**
  84. * Copy the text of the given control to the clipboard
  85. *
  86. * @param {string} selector the selector of the control
  87. * @returns {none}
  88. */
  89. function copyTextToClipboard(selector) {
  90. var control = $(selector);
  91. control.select();
  92. try {
  93. document.execCommand("copy");
  94. } catch (e) {
  95. }
  96. }
  97. /**
  98. * Count items into a selector collection
  99. *
  100. * @param {string} selector the selector for the collection
  101. * @returns {integer}
  102. */
  103. function countItemsBySelector(selector) {
  104. i=0;
  105. $(selector).each(function() {
  106. i=i+1;
  107. });
  108. return i;
  109. }
  110. /**
  111. * Encrypt the given string
  112. *
  113. * @param {string} string - The string to encrypt
  114. * @returns {string} the encrypted string
  115. */
  116. function encryptSha2(string) {
  117. var jsSHAo = new jsSHA("SHA-256", "TEXT", 1);
  118. jsSHAo.update(string);
  119. return jsSHAo.getHash("HEX");
  120. }
  121. /**
  122. * Delay the runtime execution for the given interval of time
  123. *
  124. * @param {int} interval -the interval of time of the delay
  125. * @returns {void}
  126. */
  127. function delay(interval) {
  128. var d = new Date();
  129. startTime = d.getTime();
  130. endTime = startTime;
  131. while((endTime - startTime) < interval) {
  132. d = new Date();
  133. endTime = d.getTime();
  134. //console.log(endTime - startTime);
  135. }
  136. }
  137. /**
  138. * Filter the keys of the given search query input box
  139. *
  140. * @param {object} this1 - The search query input box to filter
  141. * @returns {none}
  142. */
  143. function filterKeysQ(this1) {
  144. var value = $(this1).val();
  145. //var re = /[^\w\-: ]/gui;
  146. var re = new RegExp(/[^\w\u31C0-\u31EF\u3300-\u33FF\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF\uFE30-\uFE4F\-: ]/, "gui");
  147. if (re.test(value)) {
  148. $(this1).val(value.replace(re, ""));
  149. }
  150. }
  151. /**
  152. * Filter the keys of the given email input box
  153. *
  154. * @param {object} this1 - The email input box to filter
  155. * @returns {none}
  156. */
  157. function filterKeysEmail(this1) {
  158. var value = $(this1).val();
  159. var re = new RegExp(/[^A-Za-z0-9-_@.]/, "g");
  160. //$(this1).val(value.replace(/[^A-Za-z0-9-_@.]/g, ""));
  161. if (re.test(value)) {
  162. $(this1).val(value.replace(re, ""));
  163. }
  164. }
  165. /**
  166. * Filter the keys of the given image name input box
  167. *
  168. * @param {object} this1 - The name input box to filter
  169. * @returns {none}
  170. */
  171. function filterKeysImageName(this1) {
  172. var value = $(this1).val();
  173. //$(this1).val(value.replace(/[^A-Za-z0-9-_\s]/, ""));
  174. //var re = new RegExp('/[^A-Za-z0-9\u31C0-\u31EF\u3300-\u33FF\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF\uFE30-\uFE4F\-\s]/gui');
  175. //re.compile();
  176. //if (re.test(value)) {
  177. $(this1).val(value.replace(/[^A-Za-z0-9\u31C0-\u31EF\u3300-\u33FF\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF\uFE30-\uFE4F\-\s]/gi, ""));
  178. //}
  179. }
  180. /**
  181. * Filter the keys of the given image tags input box
  182. *
  183. * @param {object} this1 - The tags input box to filter
  184. * @returns {none}
  185. */
  186. function filterKeysImageTags(this1) {
  187. var value = $(this1).val();
  188. //$(this1).val(value.replace(/[^A-Za-z0-9-_\s]/, ""));
  189. //var re = new RegExp(/[^\w\u31C0-\u31EF\u3300-\u33FF\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF\uFE30-\uFE4F\-\s]/, "gui");
  190. //if (re.test(value)) {
  191. $(this1).val(value.replace(/[^\w\u31C0-\u31EF\u3300-\u33FF\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF\uFE30-\uFE4F\-\s]/gi, ""));
  192. //}
  193. }
  194. /**
  195. * Filter the keys of the given image url input box
  196. *
  197. * @param {object} this1 - The url input box to filter
  198. * @returns {none}
  199. */
  200. function filterKeysImageUrl(this1) {
  201. var value = $(this1).val();
  202. //$(this1).val(value.replace(/[^A-Za-z0-9-_]/, ""));
  203. //var re = new RegExp('/[^\w\u31C0-\u31EF\u3300-\u33FF\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF\uFE30-\uFE4F\-]/gui');
  204. //re.compile();
  205. //if (re.test(value)) {
  206. $(this1).val(value.replace(/[^\w\u31C0-\u31EF\u3300-\u33FF\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF\uFE30-\uFE4F\-]/gi, ""));
  207. //}
  208. }
  209. /**
  210. * Filter the keys of the given email input box
  211. *
  212. * @param {object} this1 - The email input box to filter
  213. * @returns {none}
  214. */
  215. function filterKeysUsername(this1) {
  216. var value = $(this1).val();
  217. //$(this1).val(value.replace(/[^A-Za-z0-9-_]/, ""));
  218. var re = new RegExp(/[^\w\-]/, "gui");
  219. if (re.test(value)) {
  220. $(this1).val(value.replace(re, ""));
  221. }
  222. }
  223. /**
  224. * Return the array key of a given value
  225. *
  226. * @param {array} array - The array to search for the value
  227. * @param {string} value - The value to search
  228. * @returns {string} The key corrisponding to the value, if exists
  229. */
  230. function getArrayKeyByValue(array, value) {
  231. for (var key in array) {
  232. if (key === 'length' || !array.hasOwnProperty(key)) continue;
  233. if (array[key]===value) {
  234. return key;
  235. }
  236. }
  237. return "";
  238. }
  239. /**
  240. * Get the height of the whole document
  241. *
  242. * @param {none}
  243. * @returns {int} the document height
  244. */
  245. function getDocHeight() {
  246. var D = document;
  247. return Math.max(
  248. D.body.scrollHeight, D.documentElement.scrollHeight,
  249. D.body.offsetHeight, D.documentElement.offsetHeight,
  250. D.body.clientHeight, D.documentElement.clientHeight
  251. );
  252. }
  253. function getDocHeight2() {
  254. var D = document;
  255. var scrollMaxY;
  256. if (window.scrollMaxY) {
  257. scrollMaxY = window.scrollMaxY;
  258. } else {
  259. scrollMaxY = D.documentElement.scrollHeight;
  260. }
  261. var height = Math.max(
  262. D.body.scrollHeight, scrollMaxY,
  263. D.body.offsetHeight, D.documentElement.offsetHeight,
  264. D.body.clientHeight, D.documentElement.clientHeight
  265. );
  266. return height;
  267. }
  268. /**
  269. * Get the width of the whole document
  270. *
  271. * @param {none}
  272. * @returns {int} the document width
  273. */
  274. function getDocWidth() {
  275. var D = document;
  276. return Math.max(
  277. D.body.scrollWidth, D.documentElement.scrollWidth,
  278. D.body.offsetWidth, D.documentElement.offsetWidth,
  279. D.body.clientWidth, D.documentElement.clientWidth
  280. );
  281. }
  282. function getDocWidth2() {
  283. var D = document;
  284. var scrollMaxX;
  285. if (window.scrollMaxX) {
  286. scrollMaxX = window.scrollMaxX;
  287. } else {
  288. scrollMaxX = D.documentElement.scrollWidth;
  289. }
  290. return Math.max(
  291. D.body.scrollWidth, scrollMaxX,
  292. D.body.offsetWidth, D.documentElement.offsetWidth,
  293. D.body.clientWidth, D.documentElement.clientWidth
  294. );
  295. }
  296. function getTimestampInSec() {
  297. var d = new Date();
  298. timestamp = parseInt(d.getTime() / 1000);
  299. return timestamp;
  300. }
  301. function getWindowScrollX() {
  302. var supportPageOffset = window.pageXOffset !== undefined;
  303. var isCSS1Compat = ((document.compatMode || "") === "CSS1Compat");
  304. return supportPageOffset ? window.pageXOffset : isCSS1Compat ? document.documentElement.scrollLeft : document.body.scrollLeft;
  305. }
  306. function getWindowScrollY() {
  307. var supportPageOffset = window.pageYOffset !== undefined;
  308. var isCSS1Compat = ((document.compatMode || "") === "CSS1Compat");
  309. return supportPageOffset ? window.pageYOffset : isCSS1Compat ? document.documentElement.scrollTop : document.body.scrollTop;
  310. }
  311. /**
  312. * Check if it is a valid username
  313. *
  314. * @param {string} s - The string to check
  315. * @returns {bool} if it is a valid username, true/false
  316. */
  317. function isUsername(s) {
  318. //var usernameRegEx = /^[a-zA-Z0-9_-]+?$/;
  319. //var usernameRegEx = /^[\w\-]+?$/;
  320. //var re = new RegExp(/^[\w\-]+$/, "i");
  321. var re = /^[\w\-]+$/i;
  322. return (re.test(s) && s.length>=3 && s.length<=20);
  323. }
  324. /**
  325. * Check if it is a valid email
  326. *
  327. * @param {string} s - The string to check
  328. * @returns {bool} if it is a valid email, true/false
  329. */
  330. function isEmail(s) {
  331. //var re = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
  332. var re = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
  333. return (re.test(s) && s.length>=8 && s.length<=255);
  334. }
  335. /**
  336. * Check if it is a valid image name
  337. *
  338. * @param {string} s - The string to check
  339. * @returns {bool} if it is a valid image name, true/false
  340. */
  341. function isImageName(s) {
  342. //var imageNameRegEx = /^[a-zA-Z0-9_-\s]+?$/;
  343. //var imageNameRegEx = /^[\w\-\s]+?$/;
  344. var re = new RegExp('/^[A-Za-z0-9\u31C0-\u31EF\u3300-\u33FF\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF\uFE30-\uFE4F\-\s]+?$/ui');
  345. re.compile();
  346. return (re.test(s) && s.length>=3 && s.length<=50);
  347. }
  348. /**
  349. * Check if it is a valid image url
  350. *
  351. * @param {string} s - The string to check
  352. * @returns {bool} if it is a valid image url, true/false
  353. */
  354. function isImageUrl(s) {
  355. //var imageUrlRegEx = /^[a-zA-Z0-9_-]+?$/;
  356. //var imageUrlRegEx = /^[\w\-]+?$/;
  357. var re = new RegExp('/^[\w\u31C0-\u31EF\u3300-\u33FF\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF\uFE30-\uFE4F\-]+?$/ui');
  358. re.compile();
  359. return (re.test(s) && s.length>=3 && s.length<=255);
  360. }
  361. /**
  362. * Check if it is a latin lang string
  363. *
  364. * @param {string} s - The string to check
  365. * @returns {bool} if it is a latin lang string, true/false
  366. */
  367. function isLatinLang(s) {
  368. var re = new RegExp(/^[\u31C0-\u31EF\u3300-\u33FF\u3400-\u4DBF\u4E00-\u9FFF\uF900-\uFAFF\uFE30-\uFE4F]+?$/, "gui");
  369. return (!re.test(s));
  370. }
  371. /**
  372. * Load the footer content
  373. *
  374. * @param {string} scriptName the script name
  375. * @returns {none}
  376. */
  377. function loadPageFooter(scriptName) {
  378. $("div.footer").load("/footercontent?SCRIPT_NAME=" + scriptName + "&v=" + rnd(50000, 99999));
  379. }
  380. /**
  381. * Load the header content
  382. *
  383. * @param {string} scriptName the script name
  384. * @param {string} q the query string
  385. * @returns {none}
  386. */
  387. function loadPageHeader(scriptName, q, catMaskedPath, platform) {
  388. $("div.header").load("/headercontent?SCRIPT_NAME=" + scriptName + "&q=" + q + "&catMaskedPath=" + catMaskedPath + "&platform=" + platform);
  389. }
  390. /**
  391. * Open a link from any event handler
  392. *
  393. * @param {string} href the link to open
  394. * @param {string} target the frame target
  395. * @returns {none}
  396. */
  397. function openLink(href, target) {
  398. window.open(href, target);
  399. }
  400. function rnd(min, max) {
  401. min = Math.ceil(min);
  402. max = Math.floor(max);
  403. return Math.floor(Math.random() * (max - min +1)) + min;
  404. }
  405. /**
  406. * Retain the focus at the given control
  407. *
  408. * @param {object} self - the given control
  409. * @returns {none} none
  410. */
  411. function retainFocus(self) {
  412. setTimeout(function() { self.focus(); }, 10);
  413. }
  414. /**
  415. * Position a div box to the middle of the screen
  416. *
  417. * @param {object} box div box to position
  418. * @returns {none}
  419. */
  420. function setBoxToMiddle(box) {
  421. boxHeight = parseInt(box.height());
  422. box.css("top", "-" + (parseInt(screen.availHeight - boxHeight)) + "px");
  423. }
  424. /**
  425. * Position a div box to the top of the screen
  426. *
  427. * @param {jQuery} box div box to position
  428. * @returns {none}
  429. */
  430. function setBoxToTop(box) {
  431. //undo last change (and remove all the following)
  432. //// boxHeight = parseInt(box.height());
  433. //// box.css("top", "-" + (parseInt((getDocHeight2()-boxHeight)/2)) + "px");
  434. // box.css("position", "relative");
  435. // box.css("top", "20px");
  436. // end undo
  437. boxWidth = parseInt(box.width());
  438. docWidth = parseInt(box.parent().width()); //getDocWidth2();
  439. box.css("position", "absolute");
  440. box.css("top", (window.scrollY + 20) + "px");
  441. box.css("left", parseInt(((docWidth - boxWidth) / 2)) + "px");
  442. }
  443. /**
  444. * Resize a div container to the doc height
  445. *
  446. * @param {object} container div container to resize
  447. * @returns {none}
  448. */
  449. function setContToDocHeight(container) {
  450. container.css("height", getDocHeight() + "px");
  451. }
  452. function setFooterPos() {
  453. if (document.getElementById("footerCont")) {
  454. tollerance = 25;
  455. $("#footerCont").css("top", parseInt( window.innerHeight - $("#footerCont").height() - tollerance ) + "px");
  456. $("#footer").css("top", parseInt( window.innerHeight - $("#footer").height() - tollerance ) + "px");
  457. }
  458. }
  459. function setCookieBannerPos() {
  460. if (document.getElementById("bannerCookies")) {
  461. tollerance = 30;
  462. bodyRect = document.body.getBoundingClientRect();
  463. docHeight = parseInt(getDocHeight2());
  464. $("#bannerCookies").css("top", parseInt(window.scrollY + window.innerHeight - ($("#bannerCookies").height() + tollerance)));
  465. $("#bannerCookies").css("left", parseInt(bodyRect.width - ($("#bannerCookies").width() + tollerance)));
  466. }
  467. }
  468. // Global window resize event handler..
  469. window.addEventListener("resize", function() {
  470. setTimeout("setFooterPos()", 3000);
  471. bodyRect = document.body.getBoundingClientRect();
  472. bodyRect.width = window.innerWidth;
  473. $(".body-area").css("width", parseInt(window.innerWidth)+"px");
  474. $(".footer").css("width", parseInt(window.innerWidth)+"px");
  475. $("#footerCont").css("width", parseInt(window.innerWidth)+"px");
  476. //$("#bannerCookies").attr("top", "-3000px");
  477. //setTimeout("setCookieBannerPos()", 4500);
  478. }, true);
  479. // Global window load event handler..
  480. window.addEventListener("load", function() {
  481. setTimeout("setFooterPos()", 3000);
  482. bodyRect = document.body.getBoundingClientRect();
  483. bodyRect.width = window.innerWidth;
  484. $(".body-area").css("width", parseInt(window.innerWidth)+"px");
  485. $(".footer").css("width", parseInt(window.innerWidth)+"px");
  486. $("#footerCont").css("width", parseInt(window.innerWidth)+"px");
  487. //$("#bannerCookies").attr("top", "-3000px");
  488. //setTimeout("setCookieBannerPos()", 4500);
  489. }, true);