2015-08-12 00:49:36 +02:00
|
|
|
/*******************************************************************************
|
|
|
|
|
|
|
|
uBlock Origin - a browser extension to block requests.
|
2018-04-10 17:21:56 +02:00
|
|
|
Copyright (C) 2015-2018 Raymond Hill
|
2015-08-12 00:49:36 +02:00
|
|
|
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program. If not, see {http://www.gnu.org/licenses/}.
|
|
|
|
|
|
|
|
Home: https://github.com/gorhill/uBlock
|
|
|
|
*/
|
|
|
|
|
2020-04-11 00:17:12 +02:00
|
|
|
/* global uDom, faIconsInit */
|
2015-08-12 00:49:36 +02:00
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
'use strict';
|
2015-08-12 00:49:36 +02:00
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
/******************************************************************************/
|
2015-08-12 00:49:36 +02:00
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
(( ) => {
|
2016-03-06 16:51:06 +01:00
|
|
|
|
2015-08-12 00:49:36 +02:00
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
self.cloud = {
|
|
|
|
options: {},
|
|
|
|
datakey: '',
|
|
|
|
data: undefined,
|
|
|
|
onPush: null,
|
|
|
|
onPull: null
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
const widget = uDom.nodeFromId('cloudWidget');
|
|
|
|
if ( widget === null ) { return; }
|
2015-08-12 00:49:36 +02:00
|
|
|
|
|
|
|
self.cloud.datakey = widget.getAttribute('data-cloud-entry') || '';
|
2019-09-17 21:15:01 +02:00
|
|
|
if ( self.cloud.datakey === '' ) { return; }
|
2015-08-12 00:49:36 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
const fetchCloudData = async function() {
|
|
|
|
const entry = await vAPI.messaging.send('cloudWidget', {
|
|
|
|
what: 'cloudPull',
|
|
|
|
datakey: self.cloud.datakey,
|
|
|
|
});
|
2020-08-10 14:30:52 +02:00
|
|
|
if ( entry instanceof Object === false ) { return entry; }
|
2015-08-12 00:49:36 +02:00
|
|
|
|
|
|
|
self.cloud.data = entry.data;
|
|
|
|
|
|
|
|
uDom.nodeFromId('cloudPull').removeAttribute('disabled');
|
2016-01-08 17:08:53 +01:00
|
|
|
uDom.nodeFromId('cloudPullAndMerge').removeAttribute('disabled');
|
2015-08-12 00:49:36 +02:00
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
const timeOptions = {
|
2015-08-12 00:49:36 +02:00
|
|
|
weekday: 'short',
|
|
|
|
year: 'numeric',
|
|
|
|
month: 'short',
|
|
|
|
day: 'numeric',
|
|
|
|
hour: 'numeric',
|
|
|
|
minute: 'numeric',
|
|
|
|
second: 'numeric',
|
|
|
|
timeZoneName: 'short'
|
|
|
|
};
|
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
const time = new Date(entry.tstamp);
|
2018-04-10 17:21:56 +02:00
|
|
|
widget.querySelector('#cloudInfo').textContent =
|
2015-08-12 00:49:36 +02:00
|
|
|
entry.source + '\n' +
|
|
|
|
time.toLocaleString('fullwide', timeOptions);
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
const pushData = async function() {
|
|
|
|
if ( typeof self.cloud.onPush !== 'function' ) { return; }
|
|
|
|
|
|
|
|
const error = await vAPI.messaging.send('cloudWidget', {
|
|
|
|
what: 'cloudPush',
|
|
|
|
datakey: self.cloud.datakey,
|
|
|
|
data: self.cloud.onPush(),
|
|
|
|
});
|
|
|
|
const failed = typeof error === 'string';
|
|
|
|
document.getElementById('cloudPush')
|
|
|
|
.classList
|
|
|
|
.toggle('error', failed);
|
|
|
|
document.querySelector('#cloudError')
|
|
|
|
.textContent = failed ? error : '';
|
2020-08-10 14:30:52 +02:00
|
|
|
if ( failed ) { return; }
|
2019-09-17 21:15:01 +02:00
|
|
|
fetchCloudData();
|
2015-08-12 00:49:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2020-08-10 14:30:52 +02:00
|
|
|
const pullData = function() {
|
2015-08-12 00:49:36 +02:00
|
|
|
if ( typeof self.cloud.onPull === 'function' ) {
|
2016-01-08 17:08:53 +01:00
|
|
|
self.cloud.onPull(self.cloud.data, false);
|
|
|
|
}
|
2020-08-10 14:30:52 +02:00
|
|
|
document.getElementById('cloudPush').classList.remove('error');
|
|
|
|
document.querySelector('#cloudError').textContent = '';
|
2016-01-08 17:08:53 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2020-08-10 14:30:52 +02:00
|
|
|
const pullAndMergeData = function() {
|
2016-01-08 17:08:53 +01:00
|
|
|
if ( typeof self.cloud.onPull === 'function' ) {
|
|
|
|
self.cloud.onPull(self.cloud.data, true);
|
2015-08-12 00:49:36 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2020-08-10 14:30:52 +02:00
|
|
|
const openOptions = function() {
|
|
|
|
const input = uDom.nodeFromId('cloudDeviceName');
|
2015-08-12 00:49:36 +02:00
|
|
|
input.value = self.cloud.options.deviceName;
|
|
|
|
input.setAttribute('placeholder', self.cloud.options.defaultDeviceName);
|
|
|
|
uDom.nodeFromId('cloudOptions').classList.add('show');
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2020-08-10 14:30:52 +02:00
|
|
|
const closeOptions = function(ev) {
|
|
|
|
const root = uDom.nodeFromId('cloudOptions');
|
|
|
|
if ( ev.target !== root ) { return; }
|
2015-08-12 00:49:36 +02:00
|
|
|
root.classList.remove('show');
|
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
const submitOptions = async function() {
|
|
|
|
uDom.nodeFromId('cloudOptions').classList.remove('show');
|
2015-08-12 00:49:36 +02:00
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
const options = await vAPI.messaging.send('cloudWidget', {
|
|
|
|
what: 'cloudSetOptions',
|
|
|
|
options: {
|
|
|
|
deviceName: uDom.nodeFromId('cloudDeviceName').value
|
2016-03-06 16:51:06 +01:00
|
|
|
},
|
2019-09-17 21:15:01 +02:00
|
|
|
});
|
|
|
|
if ( options instanceof Object ) {
|
|
|
|
self.cloud.options = options;
|
|
|
|
}
|
2015-08-12 00:49:36 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
const onInitialize = function(options) {
|
|
|
|
if ( options instanceof Object === false ) { return; }
|
|
|
|
if ( !options.enabled ) { return; }
|
2015-08-12 00:49:36 +02:00
|
|
|
self.cloud.options = options;
|
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
const xhr = new XMLHttpRequest();
|
2017-08-11 20:26:15 +02:00
|
|
|
xhr.open('GET', 'cloud-ui.html', true);
|
|
|
|
xhr.overrideMimeType('text/html;charset=utf-8');
|
|
|
|
xhr.responseType = 'text';
|
|
|
|
xhr.onload = function() {
|
|
|
|
this.onload = null;
|
2019-09-17 21:15:01 +02:00
|
|
|
const parser = new DOMParser(),
|
2017-08-11 20:26:15 +02:00
|
|
|
parsed = parser.parseFromString(this.responseText, 'text/html'),
|
|
|
|
fromParent = parsed.body;
|
|
|
|
while ( fromParent.firstElementChild !== null ) {
|
|
|
|
widget.appendChild(
|
|
|
|
document.adoptNode(fromParent.firstElementChild)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-11 00:17:12 +02:00
|
|
|
faIconsInit(widget);
|
|
|
|
|
2017-08-11 20:26:15 +02:00
|
|
|
vAPI.i18n.render(widget);
|
|
|
|
widget.classList.remove('hide');
|
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
uDom('#cloudPush').on('click', ( ) => { pushData(); });
|
2017-08-11 20:26:15 +02:00
|
|
|
uDom('#cloudPull').on('click', pullData);
|
|
|
|
uDom('#cloudPullAndMerge').on('click', pullAndMergeData);
|
|
|
|
uDom('#cloudCog').on('click', openOptions);
|
|
|
|
uDom('#cloudOptions').on('click', closeOptions);
|
2019-09-17 21:15:01 +02:00
|
|
|
uDom('#cloudOptionsSubmit').on('click', ( ) => { submitOptions(); });
|
2018-01-05 22:52:04 +01:00
|
|
|
|
2020-08-10 14:30:52 +02:00
|
|
|
fetchCloudData().then(result => {
|
|
|
|
if ( typeof result !== 'string' ) { return; }
|
|
|
|
document.getElementById('cloudPush').classList.add('error');
|
|
|
|
document.querySelector('#cloudError').textContent = result;
|
|
|
|
});
|
2017-08-11 20:26:15 +02:00
|
|
|
};
|
|
|
|
xhr.send();
|
2015-08-12 00:49:36 +02:00
|
|
|
};
|
|
|
|
|
2019-09-17 21:15:01 +02:00
|
|
|
vAPI.messaging.send('cloudWidget', {
|
|
|
|
what: 'cloudGetOptions',
|
|
|
|
}).then(options => {
|
|
|
|
onInitialize(options);
|
|
|
|
});
|
2015-08-12 00:49:36 +02:00
|
|
|
|
|
|
|
/******************************************************************************/
|
|
|
|
|
|
|
|
})();
|