cloud-ui.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*******************************************************************************
  2. ηMatrix - a browser extension to black/white list requests.
  3. Copyright (C) 2015-2019 Raymond Hill
  4. Copyright (C) 2019 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. /* global uDom */
  19. 'use strict';
  20. /******************************************************************************/
  21. (function() {
  22. /******************************************************************************/
  23. self.cloud = {
  24. options: {},
  25. datakey: '',
  26. data: undefined,
  27. onPush: null,
  28. onPull: null
  29. };
  30. /******************************************************************************/
  31. var widget = uDom.nodeFromId('cloudWidget');
  32. if ( widget === null ) {
  33. return;
  34. }
  35. self.cloud.datakey = widget.getAttribute('data-cloud-entry') || '';
  36. if ( self.cloud.datakey === '' ) {
  37. return;
  38. }
  39. /******************************************************************************/
  40. var onCloudDataReceived = function(entry) {
  41. if ( typeof entry !== 'object' || entry === null ) {
  42. return;
  43. }
  44. self.cloud.data = entry.data;
  45. uDom.nodeFromId('cloudPull').removeAttribute('disabled');
  46. uDom.nodeFromId('cloudPullAndMerge').removeAttribute('disabled');
  47. var timeOptions = {
  48. weekday: 'short',
  49. year: 'numeric',
  50. month: 'short',
  51. day: 'numeric',
  52. hour: 'numeric',
  53. minute: 'numeric',
  54. second: 'numeric',
  55. timeZoneName: 'short'
  56. };
  57. var time = new Date(entry.tstamp);
  58. widget.querySelector('span').textContent =
  59. entry.source + '\n' +
  60. time.toLocaleString('fullwide', timeOptions);
  61. };
  62. /******************************************************************************/
  63. var fetchCloudData = function() {
  64. vAPI.messaging.send(
  65. 'cloud-ui.js',
  66. {
  67. what: 'cloudPull',
  68. datakey: self.cloud.datakey
  69. },
  70. onCloudDataReceived
  71. );
  72. };
  73. /******************************************************************************/
  74. var pushData = function() {
  75. if ( typeof self.cloud.onPush !== 'function' ) {
  76. return;
  77. }
  78. vAPI.messaging.send(
  79. 'cloud-ui.js',
  80. {
  81. what: 'cloudPush',
  82. datakey: self.cloud.datakey,
  83. data: self.cloud.onPush()
  84. },
  85. fetchCloudData
  86. );
  87. };
  88. /******************************************************************************/
  89. var pullData = function(ev) {
  90. if ( typeof self.cloud.onPull === 'function' ) {
  91. self.cloud.onPull(self.cloud.data, ev.shiftKey);
  92. }
  93. };
  94. /******************************************************************************/
  95. var pullAndMergeData = function() {
  96. if ( typeof self.cloud.onPull === 'function' ) {
  97. self.cloud.onPull(self.cloud.data, true);
  98. }
  99. };
  100. /******************************************************************************/
  101. var openOptions = function() {
  102. var input = uDom.nodeFromId('cloudDeviceName');
  103. input.value = self.cloud.options.deviceName;
  104. input.setAttribute('placeholder', self.cloud.options.defaultDeviceName);
  105. uDom.nodeFromId('cloudOptions').classList.add('show');
  106. };
  107. /******************************************************************************/
  108. var closeOptions = function(ev) {
  109. var root = uDom.nodeFromId('cloudOptions');
  110. if ( ev.target !== root ) {
  111. return;
  112. }
  113. root.classList.remove('show');
  114. };
  115. /******************************************************************************/
  116. var submitOptions = function() {
  117. var onOptions = function(options) {
  118. if ( typeof options !== 'object' || options === null ) {
  119. return;
  120. }
  121. self.cloud.options = options;
  122. };
  123. vAPI.messaging.send('cloud-ui.js', {
  124. what: 'cloudSetOptions',
  125. options: {
  126. deviceName: uDom.nodeFromId('cloudDeviceName').value
  127. }
  128. }, onOptions);
  129. uDom.nodeFromId('cloudOptions').classList.remove('show');
  130. };
  131. /******************************************************************************/
  132. var onInitialize = function(options) {
  133. if ( typeof options !== 'object' || options === null ) {
  134. return;
  135. }
  136. if ( !options.enabled ) {
  137. return;
  138. }
  139. self.cloud.options = options;
  140. var xhr = new XMLHttpRequest();
  141. xhr.open('GET', 'cloud-ui.html', true);
  142. xhr.overrideMimeType('text/html;charset=utf-8');
  143. xhr.responseType = 'text';
  144. xhr.onload = function() {
  145. this.onload = null;
  146. var parser = new DOMParser(),
  147. parsed = parser.parseFromString(this.responseText, 'text/html'),
  148. fromParent = parsed.body;
  149. while ( fromParent.firstElementChild !== null ) {
  150. widget.appendChild(
  151. document.adoptNode(fromParent.firstElementChild)
  152. );
  153. }
  154. vAPI.i18n.render(widget);
  155. widget.classList.remove('hide');
  156. uDom('#cloudPush').on('click', pushData);
  157. uDom('#cloudPull').on('click', pullData);
  158. uDom('#cloudPullAndMerge').on('click', pullAndMergeData);
  159. uDom('#cloudCog').on('click', openOptions);
  160. uDom('#cloudOptions').on('click', closeOptions);
  161. uDom('#cloudOptionsSubmit').on('click', submitOptions);
  162. fetchCloudData();
  163. };
  164. xhr.send();
  165. };
  166. vAPI.messaging.send('cloud-ui.js', { what: 'cloudGetOptions' }, onInitialize);
  167. /******************************************************************************/
  168. // https://www.youtube.com/watch?v=aQFp67VoiDA
  169. })();