cloud-ui.js 6.0 KB

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