cloud-ui.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*******************************************************************************
  2. ηMatrix - a browser extension to black/white list requests.
  3. Copyright (C) 2015-2019 Raymond Hill
  4. Copyright (C) 2019-2022 Alessio Vanni
  5. This program is free software: you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation, either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see {http://www.gnu.org/licenses/}.
  15. Home: https://gitlab.com/vannilla/ematrix
  16. uMatrix Home: https://github.com/gorhill/uBlock
  17. */
  18. 'use strict';
  19. (function () {
  20. self.cloud = {
  21. options: {},
  22. datakey: '',
  23. data: undefined,
  24. onPush: null,
  25. onPull: null
  26. };
  27. let widget = uDom.nodeFromId('cloudWidget');
  28. if (widget === null) {
  29. return;
  30. }
  31. self.cloud.datakey = widget.getAttribute('data-cloud-entry') || '';
  32. if (self.cloud.datakey === '') {
  33. return;
  34. }
  35. let onCloudDataReceived = function (entry) {
  36. if (typeof entry !== 'object' || entry === null) {
  37. return;
  38. }
  39. self.cloud.data = entry.data;
  40. uDom.nodeFromId('cloudPull').removeAttribute('disabled');
  41. uDom.nodeFromId('cloudPullAndMerge').removeAttribute('disabled');
  42. let timeOptions = {
  43. weekday: 'short',
  44. year: 'numeric',
  45. month: 'short',
  46. day: 'numeric',
  47. hour: 'numeric',
  48. minute: 'numeric',
  49. second: 'numeric',
  50. timeZoneName: 'short',
  51. };
  52. let time = new Date(entry.tstamp);
  53. widget.querySelector('span').textContent = entry.source
  54. + '\n'
  55. + time.toLocaleString('fullwide', timeOptions);
  56. };
  57. let fetchCloudData = function () {
  58. vAPI.messaging.send('cloud-ui.js', {
  59. what: 'cloudPull',
  60. datakey: self.cloud.datakey
  61. }, onCloudDataReceived);
  62. };
  63. let pushData = function () {
  64. if (typeof self.cloud.onPush !== 'function') {
  65. return;
  66. }
  67. vAPI.messaging.send('cloud-ui.js', {
  68. what: 'cloudPush',
  69. datakey: self.cloud.datakey,
  70. data: self.cloud.onPush()
  71. }, fetchCloudData);
  72. };
  73. let pullData = function (ev) {
  74. if (typeof self.cloud.onPull === 'function') {
  75. self.cloud.onPull(self.cloud.data, ev.shiftKey);
  76. }
  77. };
  78. let pullAndMergeData = function () {
  79. if (typeof self.cloud.onPull === 'function') {
  80. self.cloud.onPull(self.cloud.data, true);
  81. }
  82. };
  83. let openOptions = function () {
  84. let input = uDom.nodeFromId('cloudDeviceName');
  85. input.value = self.cloud.options.deviceName;
  86. input.setAttribute('placeholder', self.cloud.options.defaultDeviceName);
  87. uDom.nodeFromId('cloudOptions').classList.add('show');
  88. };
  89. let closeOptions = function (ev) {
  90. let root = uDom.nodeFromId('cloudOptions');
  91. if (ev.target !== root) {
  92. return;
  93. }
  94. root.classList.remove('show');
  95. };
  96. let submitOptions = function () {
  97. let onOptions = function (options) {
  98. if (typeof options !== 'object' || options === null) {
  99. return;
  100. }
  101. self.cloud.options = options;
  102. };
  103. vAPI.messaging.send('cloud-ui.js', {
  104. what: 'cloudSetOptions',
  105. options: {
  106. deviceName: uDom.nodeFromId('cloudDeviceName').value
  107. }
  108. }, onOptions);
  109. uDom.nodeFromId('cloudOptions').classList.remove('show');
  110. };
  111. let onInitialize = function (options) {
  112. if (typeof options !== 'object' || options === null) {
  113. return;
  114. }
  115. if (!options.enabled) {
  116. return;
  117. }
  118. self.cloud.options = options;
  119. let xhr = new XMLHttpRequest();
  120. xhr.open('GET', 'cloud-ui.html', true);
  121. xhr.overrideMimeType('text/html;charset=utf-8');
  122. xhr.responseType = 'text';
  123. xhr.onload = function () {
  124. this.onload = null;
  125. let parser = new DOMParser();
  126. let parsed = parser.parseFromString(this.responseText, 'text/html');
  127. let fromParent = parsed.body;
  128. while (fromParent.firstElementChild !== null) {
  129. widget.appendChild(document
  130. .adoptNode(fromParent.firstElementChild));
  131. }
  132. vAPI.i18n.render(widget);
  133. widget.classList.remove('hide');
  134. uDom('#cloudPush').on('click', pushData);
  135. uDom('#cloudPull').on('click', pullData);
  136. uDom('#cloudPullAndMerge').on('click', pullAndMergeData);
  137. uDom('#cloudCog').on('click', openOptions);
  138. uDom('#cloudOptions').on('click', closeOptions);
  139. uDom('#cloudOptionsSubmit').on('click', submitOptions);
  140. fetchCloudData();
  141. };
  142. xhr.send();
  143. };
  144. vAPI.messaging.send('cloud-ui.js', {
  145. what: 'cloudGetOptions'
  146. }, onInitialize);
  147. // https://www.youtube.com/watch?v=aQFp67VoiDA
  148. })();