Skip to content

Commit

Permalink
Updated Chrome app to no longer require domain to be known.
Browse files Browse the repository at this point in the history
Signed-off-by: Joachim Bauch <bauch@struktur.de>
  • Loading branch information
fancycode committed Jan 23, 2017
1 parent 2b71165 commit b8f9ca8
Show file tree
Hide file tree
Showing 4 changed files with 249 additions and 30 deletions.
161 changes: 143 additions & 18 deletions extensions/chrome/background.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,150 @@
/* global chrome */
/* global chrome, Promise */

/**
* @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() {

/* background page, responsible for actually choosing media */
chrome.runtime.onMessageExternal.addListener(function (message, sender, callback) {
switch(message.type) {
case 'getScreen':
chrome.desktopCapture.chooseDesktopMedia(message.options || ['screen', 'window'],
sender.tab, function (streamid) {
// communicate this string to the app so it can call getUserMedia with it
message.type = 'gotScreen';
message.sourceId = streamid;
callback(message);
return false;
var priority = 100;

chrome.runtime.onMessage.addListener(function(msg, sender, sendResponse) {
switch (msg.type) {
case 'init-nc-screensharing':
var data = msg.data;
var details = chrome.app.getDetails();
if (data.extension) {
// Existing extension data. Checking id.
if (details.id !== data.extension.id) {
// Different extension. Check prio.
if (data.extension.priority && data.extension.priority >= priority) {
// Existing extension has same or higher priority - skip ourselves.
console.log('Refusing to initialize as other extension with same or higher priority found.', data.extension);
return;
} else {
console.log('Refusing to initialize as other extension is already initialized', data.extension);
return;
}
}
}
console.log('Initializing screensharing extension ...', sender.tab.id, data);
sendResponse({
id: details.id,
version: details.version,
priority: priority
});
// Embed our script.
chrome.tabs.executeScript(sender.tab.id, {
file: 'content.js',
runAt: 'document_end'
});
break;
}
});

var ExtensionApi = function(port) {
this.port = port;
};

ExtensionApi.prototype.getScreen = function(resolve, reject, msg) {
var pending = chrome.desktopCapture.chooseDesktopMedia(['screen', 'window'], this.port.sender.tab, function(id) {
var response = {
'type': 'gotScreen',
'id': msg.id
};
if (chrome.runtime.lastError) {
response.sourceId = '';
response.error = chrome.runtime.lastError;
} else {
response.sourceId = id;
}
resolve(response);
});
if (!pending) {
return;
}

return {
'type': 'getScreenPending',
'id': msg.id
};
};

ExtensionApi.prototype.call = function(msg) {
var api = this;
var promise = new Promise(function(resolve, reject) {
var f = api[msg.type];
if (!f) {
reject('action ' + msg.type + ' not found');
return;
}
var response = f.bind(api)(resolve, reject, msg);
if (response) {
response.answer = true;
api.port.postMessage(response);
}
});
promise.then(function(result) {
result.answer = true;
api.port.postMessage(result);
}, function(err) {
if (typeof(err) === 'string') {
err = {
'error': err
};
}
api.port.postMessage(err);
});
};

chrome.runtime.onConnect.addListener(function(port) {
var tab = port.sender.tab;
var api = new ExtensionApi(port);
console.log('Extension connected:', tab.id);
port.onMessage.addListener(function(msg) {
if (msg.answer) {
return;
}

msg.answer = true;
api.call(msg);
});
});

// Bootstrap directly after install so pages need not be refreshed.
chrome.runtime.onInstalled.addListener(function(details) {
switch (details.reason) {
case 'install':
case 'update':
console.log('Extension updated or installed', details);
chrome.windows.getAll({populate: true}, function(windows) {
windows.forEach(function(window) {
window.tabs.forEach(function(tab) {
if (tab.url.match(/https:\/\//gi)) {
chrome.tabs.executeScript(tab.id, {
file: 'detector.js',
runAt: 'document_end'
});
}
});
});
return true; // retain callback for chooseDesktopMedia result
case 'cancelGetScreen':
chrome.desktopCapture.cancelChooseDesktopMedia(message.request);
message.type = 'canceledGetScreen';
callback(message);
return false; //dispose callback
});
}
});

Expand Down
35 changes: 34 additions & 1 deletion extensions/chrome/content.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,40 @@
/* global chrome */

/**
* @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() {

sessionStorage.getScreenMediaJSExtensionId = chrome.runtime.id;
var port = chrome.runtime.connect();

// Forward message from Nextcloud app to the extension.
window.addEventListener('message', function(event) {
var msg = event.data;
if (event.source === window && !msg.answer) {
port.postMessage(msg);
}
});

// Forward message from the extension to the Nextcloud app.
port.onMessage.addListener(function(msg) {
window.postMessage(msg, window.document.URL);
});

})();
57 changes: 57 additions & 0 deletions extensions/chrome/detector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/* global chrome */

/**
* @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(document) {

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

// Only add extension if the page is running the Nextcloud app.
var nc = document.getElementById('app');
if (!nc || !hasClass(nc, 'nc-enable-screensharing-extension')) {
return;
}

var extension = nc.getAttribute('data-chromExtensionData');
if (extension) {
try {
extension = JSON.parse(extension);
} catch (e) {
return;
}
}

console.log('Detected Nextcloud video calls app.');
var msg = {
type: 'init-nc-screensharing',
data: {
extension: extension || null
}
};

chrome.runtime.sendMessage(null, msg, null, function(data) {
console.log('Initialized extension', data);
nc.setAttribute('data-chromExtensionData', JSON.stringify(data));
});

}(document));
26 changes: 15 additions & 11 deletions extensions/chrome/manifest.json
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
{
"name": "Nextcloud Screensharing",
"description": "Screensharing utility for the Nextcloud video calls app.",
"author": "Joachim Bauch <bauch@struktur.de>"
"homepage_url": "https://nextcloud.com",
"version": "0.0.1",
"manifest_version": 2,
"minimum_chrome_version": "34",
"icons": {
},
"permissions": [
"desktopCapture"
"tabs",
"desktopCapture",
"https://*/*"
],
"background": {
"scripts": ["background.js"]
"scripts": ["background.js"],
"persistent": false
},
"content_scripts": [ {
"js": [ "content.js" ],
"matches": [ "https://replace-with-your-hostname/*" ]
}],
"externally_connectable": {
"matches": [
"https://replace-with-your-hostname/*"
]
}
"content_scripts": [
{
"js": ["detector.js"],
"matches": ["https://*/*"],
"run_at": "document_end",
"all_frames": true
}
]
}

0 comments on commit b8f9ca8

Please sign in to comment.