electronotes.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /* --- Imports --- */
  2. // Dependencies
  3. const {ipcRenderer, remote} = require('electron');
  4. const $ = require('jquery');
  5. const marked = require('marked');
  6. const DOMPurify = require('dompurify');
  7. // Services
  8. const RouterService = require('../../service/router');
  9. const DataService = require('../../service/data');
  10. const UserService = require('../../service/user');
  11. /* --- Override object --- */
  12. /**
  13. * Transform markdown string to html
  14. */
  15. String.prototype.toHTML = function() {
  16. return DOMPurify.sanitize(
  17. marked(this.toString())
  18. .replace(/<pre>/g, '<pre class="prettyprint">')
  19. );
  20. }
  21. /**
  22. * Remove item in array
  23. */
  24. Array.prototype.removeItem = function(item) {
  25. const index = this.indexOf(item);
  26. if (index !== -1) {
  27. this.splice(index, 1);
  28. }
  29. return this;
  30. }
  31. /* --- Loader --- */
  32. function loader () {
  33. if ($('#page-loader').hasClass('hide')) {
  34. $('#page-loader').removeClass('hide');
  35. $('#navbar').addClass('hide');
  36. if ($('#float-add-btn')) {
  37. $('#float-add-btn').addClass('hide');
  38. }
  39. } else {
  40. if ($('#float-add-btn')) {
  41. $('#float-add-btn').removeClass('hide');
  42. }
  43. $('#navbar').removeClass('hide');
  44. $('#page-loader').addClass('hide');
  45. }
  46. };
  47. /* --- Cookie --- */
  48. function getCookie(key) {
  49. const split = document.cookie.split(';');
  50. let cookies = {};
  51. split.forEach(elt => {
  52. const val = elt.trim().split('=');
  53. cookies[val[0]] = val[1];
  54. });
  55. if(key !== undefined) {
  56. return cookies[key];
  57. }
  58. return cookies;
  59. }
  60. function removeCookie(key) {
  61. if (Array.isArray(key)) {
  62. key.forEach(elt => {
  63. removeCookie(elt);
  64. });
  65. } else {
  66. document.cookie = `${key}=; expires=Thu, 01 Jan 1970 00:00:01 GMT;`;
  67. }
  68. }
  69. /* --- URL Parameter --- */
  70. function parseURLParameter() {
  71. const data = {};
  72. const get = location.search.substr(1).split("&");
  73. for (elt of get) {
  74. const split = elt.split("=");
  75. data[split[0]] = decodeURIComponent(split[1]);
  76. }
  77. return data;
  78. }
  79. function getURLParameter(key) {
  80. const get = parseURLParameter();
  81. return get[key];
  82. }