Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use external messaging to avoid reload after installation #9

Merged
merged 5 commits into from May 18, 2015
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file or symbol
Failed to load files and symbols.

Always

Just for now

@@ -1,8 +1,6 @@
This is an example app which implements the extension-side of [getscreenmedia](https://github.com/henrikjoreteg/getscreenmedia).

It basically uses window event messages to communicate between the website javascript and the content script (content.js).
The content script in turn uses channel messaging to talk to the backend script which calls [chooseDesktopMedia](https://developer.chrome.com/extensions/desktopCapture)
and returns the sourceId of the chosen window. This sourceId has to be passed back to getUserMedia.
The browser useѕ [chrome.runtime.sendMessage](https://developer.chrome.com/extensions/runtime#method-sendMessage) to talk to the backend script which calls [chooseDesktopMedia](https://developer.chrome.com/extensions/desktopCapture) and returns the sourceId of the chosen window. This sourceId has to be passed back to getUserMedia.

See also [the tutorial for using inline installation](https://developer.chrome.com/webstore/inline_installation).

@@ -1,25 +1,20 @@
/* background page, responsible for actually choosing media */
chrome.runtime.onConnect.addListener(function (channel) {
channel.onMessage.addListener(function (message) {
switch(message.type) {
chrome.runtime.onMessageExternal.addListener(function (message, sender, callback) {
switch(message.type) {
case 'getScreen':
var pending = chrome.desktopCapture.chooseDesktopMedia(message.options || ['screen', 'window'],
channel.sender.tab, function (streamid) {
var pending = 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;
channel.postMessage(message);
callback(message);
return false;
});
// let the app know that it can cancel the timeout
message.type = 'getScreenPending';
message.request = pending;
channel.postMessage(message);
break;
return true; // retain callback for chooseDesktopMedia result
case 'cancelGetScreen':
chrome.desktopCapture.cancelChooseDesktopMedia(message.request);
message.type = 'canceledGetScreen';
channel.postMessage(message);
break;
}
});
callback(message);
return false; //dispose callback
}
});
@@ -1,14 +1 @@
/* the chrome content script which can listen to the page dom events */
var channel = chrome.runtime.connect();
channel.onMessage.addListener(function (message) {
console.log('channel message', message);
window.postMessage(message, '*');
});

window.addEventListener('message', function (event) {
if (event.source != window)
return;
if (!event.data && (event.data.type == 'getScreen' || event.data.type == 'cancelGetScreen'))
return;
channel.postMessage(event.data);
});
sessionStorage.getScreenMediaJSExtensionId = chrome.runtime.id;
@@ -15,5 +15,10 @@
"content_scripts": [ {
"js": [ "content.js" ],
"matches": [ "https://simplewebrtc.com/*" ]
}]
}],
"externally_connectable": {
"matches": [
"https://simplewebrtc.com/*"
]
}
}
@@ -21,7 +21,38 @@ module.exports = function (constraints, cb) {
var isCef = !window.chrome.webstore;
// "known" crash in chrome 34 and 35 on linux
if (window.navigator.userAgent.match('Linux')) maxver = 35;
if (isCef || (chromever >= 26 && chromever <= maxver)) {

// check that the extension is installed by looking for a
// sessionStorage variable that contains the extension id
// this has to be set after installation unless the contest
// script does that
if (sessionStorage.getScreenMediaJSExtensionId) {
chrome.runtime.sendMessage(sessionStorage.getScreenMediaJSExtensionId,
{type:'getScreen', id: 1}, null,
function (data) {
if (data.sourceId === '') { // user canceled
var error = new Error('NavigatorUserMediaError');
error.name = 'PERMISSION_DENIED';
callback(error);
} else {
var constraints = constraints || {audio: false, video: {
mandatory: {
chromeMediaSource: 'desktop',
maxWidth: window.screen.width,
maxHeight: window.screen.height,
maxFrameRate: 3
},
optional: [
{googLeakyBucket: true},
{googTemporalLayeredScreencast: true}
]
}};
constraints.video.mandatory.chromeMediaSourceId = data.sourceId;
getUserMedia(constraints, callback);
}
}
);
} else if (isCef || (chromever >= 26 && chromever <= maxver)) {
// chrome 26 - chrome 33 way to do it -- requires bad chrome://flags
// note: this is basically in maintenance mode and will go away soon
constraints = (hasConstraints && constraints) || {
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.