Welcome to mirror list, hosted at ThFree Co, Russian Federation.

framescript.js « chrome « extension - github.com/nextcloud/spreed-screensharing-firefox-addon.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 29ed3e31dfbfb5a1ed83b228a0826736a57fd2c1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/**
 * Nextcloud Firefox extension.
 *
 * Script that will be loaded in all frames (i.e. tabs and windows).
 *
 * @author Joachim Bauch <bauch@struktur.de>
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

(function(cfmm) {

	var START_SCREENSHARING_MESSAGE = 'webrtcStartScreensharing';
	var STOP_SCREENSHARING_MESSAGE = 'webrtcStopScreensharing';

	var NAMESPACE = 'nextcloud-spreed';

	var initialized = false;
	var unique_id;

	var extensionData = {
		id: null,
		version: null
	};

	var logMessage = function() {
		sendSyncMessage(NAMESPACE + ':log', Array.prototype.slice.call(arguments));
	};

	(function(data) {
		var d = data && data[0];
		if (d && d.version && d.id) {
			extensionData.id = d.id;
			extensionData.version = d.version;
		} else {
			throw new Error('Extension information not found.');
		}
	})(cfmm.sendSyncMessage(NAMESPACE + ':ExtensionDataSync'));

	var hasClass = function(element, cls) {
		return (' ' + element.className + ' ').indexOf(' ' + cls + ' ') > -1;
	};

	var isNextcloudSpreedApp = function(document) {
		if (!document) {
			return false;
		}

		var nc = document.getElementById('app');
		if (!nc || !hasClass(nc, 'nc-enable-screensharing-extension')) {
			return false;
		}

		return true;
	};

	var messageHandler = function(event) {
		var window = cfmm.content;
		if (!initialized || event.origin != window.location.origin || !event.data) {
			return;
		}

		var host = window.location.hostname;
		var data = event.data;
		switch (data.type) {
			case 'webrtcStartScreensharing':
				logMessage("start screensharing", host);
				sendSyncMessage(NAMESPACE + ':' + START_SCREENSHARING_MESSAGE, {"host": host});
				window.postMessage({'type': 'webrtcScreensharingWhitelisted', 'id': data.id}, event.origin);
				break;

			case 'webrtcStopScreensharing':
				logMessage("stop screensharing", host);
				sendSyncMessage(NAMESPACE + ':' + STOP_SCREENSHARING_MESSAGE, {"host": host});
				break;
		}
	};

	var removeHandlers = function(window, document) {
		if (!initialized) {
			return;
		}

		window.removeEventListener('message', messageHandler);
		initialized = false;
	};

	var showAvailability = function(document) {
		var nc = document.getElementById('app');
		nc.setAttribute('data-firefoxExtensionData', JSON.stringify(extensionData));
	};

	var installHandlers = function(window, document) {
		// Only add extension if the page is running the Nextcloud Spreed -app.
		if (!isNextcloudSpreedApp(document)) {
			removeHandlers(document);
			return;
		}

		removeHandlers(document);
		logMessage('Detected Nextcloud Spreed app.');
		window.addEventListener('message', messageHandler);
		showAvailability(document);
		initialized = true;
	};

	var loaded = function() {
		if (!cfmm.content) {
			return;
		}
		installHandlers(cfmm.content, cfmm.content.document);
	};

	var init = function(msg) {
		if (msg.data) {
			unique_id = msg.data.unique_id;
		}
		loaded();
		cfmm.removeMessageListener(NAMESPACE + ':startup', init);
	};

	var teardown = function(msg) {
		if (unique_id !== msg.data.unique_id) {
			return;
		}

		cfmm.removeMessageListener(NAMESPACE + ':startup', init);
		cfmm.removeMessageListener(NAMESPACE + ':teardown', teardown);
		removeHandlers(cfmm.content, cfmm.content.document);
		unique_id = null;
	};

	cfmm.addEventListener('DOMContentLoaded', loaded);
	cfmm.addEventListener('load', loaded);
	cfmm.addMessageListener(NAMESPACE + ':startup', init);
	cfmm.addMessageListener(NAMESPACE + ':teardown', teardown);

	// logMessage('Loaded framescript.js');
})(this);