uBlock/src/js/cloud-ui.js

228 lines
6.3 KiB
JavaScript
Raw Normal View History

2015-08-12 00:49:36 +02:00
/*******************************************************************************
uBlock Origin - a browser extension to block requests.
2017-09-21 17:46:15 +02:00
Copyright (C) 2015-2017 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
*/
/* global uDom */
/******************************************************************************/
(function() {
2016-03-06 16:51:06 +01:00
'use strict';
2015-08-12 00:49:36 +02:00
/******************************************************************************/
self.cloud = {
options: {},
datakey: '',
data: undefined,
onPush: null,
onPull: null
};
/******************************************************************************/
var widget = uDom.nodeFromId('cloudWidget');
if ( widget === null ) {
return;
}
self.cloud.datakey = widget.getAttribute('data-cloud-entry') || '';
if ( self.cloud.datakey === '' ) {
return;
}
2016-03-06 16:51:06 +01:00
var messaging = vAPI.messaging;
2015-08-12 00:49:36 +02:00
/******************************************************************************/
var onCloudDataReceived = function(entry) {
2017-09-21 17:46:15 +02:00
if ( entry instanceof Object === false ) {
2015-08-12 00:49:36 +02:00
return;
}
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
var timeOptions = {
weekday: 'short',
year: 'numeric',
month: 'short',
day: 'numeric',
hour: 'numeric',
minute: 'numeric',
second: 'numeric',
timeZoneName: 'short'
};
var time = new Date(entry.tstamp);
widget.querySelector('span').textContent =
entry.source + '\n' +
time.toLocaleString('fullwide', timeOptions);
};
/******************************************************************************/
var fetchCloudData = function() {
2016-03-06 16:51:06 +01:00
messaging.send(
'cloudWidget',
2015-08-12 00:49:36 +02:00
{
what: 'cloudPull',
datakey: self.cloud.datakey
},
onCloudDataReceived
);
};
/******************************************************************************/
var pushData = function() {
if ( typeof self.cloud.onPush !== 'function' ) {
return;
}
2016-03-06 16:51:06 +01:00
messaging.send(
'cloudWidget',
2015-08-12 00:49:36 +02:00
{
what: 'cloudPush',
datakey: self.cloud.datakey,
data: self.cloud.onPush()
},
2017-09-21 17:46:15 +02:00
function(error) {
var failed = typeof error === 'string';
document.getElementById('cloudPush')
.classList
.toggle('error', failed);
document.querySelector('#cloudError > span')
.textContent = failed ? error : '';
fetchCloudData();
}
2015-08-12 00:49:36 +02:00
);
};
/******************************************************************************/
2016-01-08 17:08:53 +01:00
var 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);
}
};
/******************************************************************************/
var pullAndMergeData = function() {
if ( typeof self.cloud.onPull === 'function' ) {
self.cloud.onPull(self.cloud.data, true);
2015-08-12 00:49:36 +02:00
}
};
/******************************************************************************/
var openOptions = function() {
var input = uDom.nodeFromId('cloudDeviceName');
input.value = self.cloud.options.deviceName;
input.setAttribute('placeholder', self.cloud.options.defaultDeviceName);
uDom.nodeFromId('cloudOptions').classList.add('show');
};
/******************************************************************************/
var closeOptions = function(ev) {
var root = uDom.nodeFromId('cloudOptions');
if ( ev.target !== root ) {
return;
}
root.classList.remove('show');
};
/******************************************************************************/
var submitOptions = function() {
var onOptions = function(options) {
2017-09-21 17:46:15 +02:00
if ( options instanceof Object === false ) {
2015-08-12 00:49:36 +02:00
return;
}
self.cloud.options = options;
};
2016-03-06 16:51:06 +01:00
messaging.send(
'cloudWidget',
{
what: 'cloudSetOptions',
options: {
deviceName: uDom.nodeFromId('cloudDeviceName').value
}
},
onOptions
);
2015-08-12 00:49:36 +02:00
uDom.nodeFromId('cloudOptions').classList.remove('show');
};
/******************************************************************************/
var onInitialize = function(options) {
if ( typeof options !== 'object' || options === null ) {
return;
}
if ( !options.enabled ) {
return;
}
self.cloud.options = options;
fetchCloudData();
var xhr = new XMLHttpRequest();
xhr.open('GET', 'cloud-ui.html', true);
xhr.overrideMimeType('text/html;charset=utf-8');
xhr.responseType = 'text';
xhr.onload = function() {
this.onload = null;
var parser = new DOMParser(),
parsed = parser.parseFromString(this.responseText, 'text/html'),
fromParent = parsed.body;
while ( fromParent.firstElementChild !== null ) {
widget.appendChild(
document.adoptNode(fromParent.firstElementChild)
);
}
vAPI.i18n.render(widget);
widget.classList.remove('hide');
uDom('#cloudPush').on('click', pushData);
uDom('#cloudPull').on('click', pullData);
uDom('#cloudPullAndMerge').on('click', pullAndMergeData);
uDom('#cloudCog').on('click', openOptions);
uDom('#cloudOptions').on('click', closeOptions);
uDom('#cloudOptionsSubmit').on('click', submitOptions);
};
xhr.send();
2015-08-12 00:49:36 +02:00
};
2016-03-06 16:51:06 +01:00
messaging.send('cloudWidget', { what: 'cloudGetOptions' }, onInitialize);
2015-08-12 00:49:36 +02:00
/******************************************************************************/
// https://www.youtube.com/watch?v=aQFp67VoiDA
2015-08-12 00:49:36 +02:00
})();