mirror of
https://github.com/fergalmoran/chrometophone.git
synced 2025-12-22 09:41:51 +00:00
Phone to chrome support
This commit is contained in:
@@ -14,9 +14,26 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
<script src="http://chrometophone.appspot.com/_ah/channel/jsapi"/>
|
||||
<script src="send_logic.js"/>
|
||||
|
||||
<script>
|
||||
|
||||
// TODO: Remove when out of beta
|
||||
function maybeActivateBrowserChannel() {
|
||||
var activateBrowserChannel = localStorage['activateBrowserChannel'];
|
||||
if (activateBrowserChannel == undefined && window.navigator.language == 'en-US') {
|
||||
if (Math.random() < 0.1) {
|
||||
localStorage['activateBrowserChannel'] = 'true';
|
||||
} else {
|
||||
localStorage['activateBrowserChannel'] = 'false';
|
||||
}
|
||||
}
|
||||
if (localStorage['activateBrowserChannel'] == 'true') {
|
||||
initializeBrowserChannel();
|
||||
}
|
||||
}
|
||||
|
||||
function onClickHandler(info, tab) {
|
||||
var url = info.srcUrl;
|
||||
if (url == undefined) url = info.linkUrl;
|
||||
@@ -42,4 +59,8 @@ if (chrome.contextMenus) {
|
||||
'onclick': onClickHandler,
|
||||
'contexts': ['link', 'selection', 'image', 'video', 'audio']});
|
||||
}
|
||||
|
||||
// TODO: Replace with initializeBrowserChannel() when out of beta
|
||||
maybeActivateBrowserChannel();
|
||||
|
||||
</script>
|
||||
|
||||
@@ -29,16 +29,34 @@ body,td {
|
||||
</style>
|
||||
|
||||
<script type="text/javascript">
|
||||
function signInAction() {
|
||||
function loadHandler() {
|
||||
if (document.location.hash == '#signed_in') {
|
||||
var bg = chrome.extension.getBackgroundPage();
|
||||
bg.maybeActivateBrowserChannel(); // TODO: Replace with initializeBrowserChannel() when out of beta
|
||||
|
||||
document.getElementById('signed_in_div').innerHTML = '<p><b><font color="#00A000">' +
|
||||
chrome.i18n.getMessage('signed_in_message') + '</font></b></p>';
|
||||
}
|
||||
|
||||
// TODO: Remove when out of beta
|
||||
if (window.navigator.language == 'en-US') {
|
||||
document.getElementById('experimental_div').style.visibility = 'visible';
|
||||
document.getElementById('p2c_checkbox').checked = (localStorage['activateBrowserChannel'] == 'true');
|
||||
}
|
||||
}
|
||||
|
||||
// TODO: Remove when out of beta
|
||||
function checkBoxHandler() {
|
||||
document.getElementById('p2c_checkbox').checked ?
|
||||
localStorage['activateBrowserChannel'] = 'true' :
|
||||
localStorage['activateBrowserChannel'] = 'false';
|
||||
var bg = chrome.extension.getBackgroundPage();
|
||||
bg.initializeBrowserChannel();
|
||||
}
|
||||
</script>
|
||||
</head>
|
||||
|
||||
<body onload="signInAction()">
|
||||
<body onload="loadHandler()">
|
||||
<h1><img src="icon_128.png" width="64" height="64" valign="bottom"> Google Chrome to Phone Extension</h1>
|
||||
|
||||
<div id="signed_in_div"></div>
|
||||
@@ -79,7 +97,12 @@ body,td {
|
||||
</tr>
|
||||
</table>
|
||||
<br>
|
||||
<div align="center"><br>©2010 Google - <a href="http://www.google.com/intl/en/about.html"><script>document.write(chrome.i18n.getMessage('about_google_message'));</script></a> -
|
||||
|
||||
|
||||
<!-- TODO: Remove when out of beta -->
|
||||
<div id="experimental_div" style="visibility: hidden;" align="center"><input id="p2c_checkbox" type="checkbox" onclick="checkBoxHandler()"> Enable Phone to Chrome Feature (Beta)</div>
|
||||
|
||||
<div align="center">©2010 Google - <a href="http://www.google.com/intl/en/about.html"><script>document.write(chrome.i18n.getMessage('about_google_message'));</script></a> -
|
||||
<a href="http://www.google.com/privacy.html"><script>document.write(chrome.i18n.getMessage('privacy_message'));</script></a> -
|
||||
<script>document.write('<a href="http://chrome.google.com/extensions/intl/' + window.navigator.language.substring(0, 2) + '/gallery_tos.html">' + chrome.i18n.getMessage('tos_message') + '</a>');</script></div>
|
||||
|
||||
|
||||
@@ -21,12 +21,17 @@ var signInUrl = baseUrl + '/signin?extret=' +
|
||||
encodeURIComponent(chrome.extension.getURL('help.html')) + '%23signed_in&ver=' + apiVersion;
|
||||
var signOutUrl = baseUrl + '/signout?extret=' +
|
||||
encodeURIComponent(chrome.extension.getURL('signed_out.html')) + '&ver=' + apiVersion;
|
||||
var channelUrl = baseUrl + '/browserchannel?ver=' + apiVersion;
|
||||
|
||||
var STATUS_SUCCESS = 'success';
|
||||
var STATUS_LOGIN_REQUIRED = 'login_required';
|
||||
var STATUS_DEVICE_NOT_REGISTERED = 'device_not_registered';
|
||||
var STATUS_GENERAL_ERROR = 'general_error';
|
||||
|
||||
var BROWSER_CHANNEL_RETRY_INTERVAL_MS = 10000 * (1 + Math.random() - 0.5);
|
||||
|
||||
var channel;
|
||||
var socket;
|
||||
var req = new XMLHttpRequest();
|
||||
|
||||
function sendToPhone(title, url, msgType, selection, listener) {
|
||||
@@ -49,10 +54,46 @@ function sendToPhone(title, url, msgType, selection, listener) {
|
||||
listener(STATUS_GENERAL_ERROR);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
var data = 'title=' + encodeURIComponent(title) + '&url=' + encodeURIComponent(url) +
|
||||
'&sel=' + encodeURIComponent(selection) + '&type=' + encodeURIComponent(msgType);
|
||||
req.send(data);
|
||||
}
|
||||
|
||||
function initializeBrowserChannel() {
|
||||
console.log('Initializing browser channel');
|
||||
req.open('POST', channelUrl, true);
|
||||
req.setRequestHeader('X-Same-Domain', 'true'); // XSRF protector
|
||||
req.onreadystatechange = function() {
|
||||
if (this.readyState == 4) {
|
||||
if (req.status == 200) {
|
||||
var channelId = req.responseText;
|
||||
channel = new goog.appengine.Channel(channelId);
|
||||
socket = channel.open();
|
||||
socket.onopen = function() {
|
||||
console.log('Browser channel initialized');
|
||||
}
|
||||
socket.onerror = function() {
|
||||
console.log('Browser channel not initialized - retrying in ' + BROWSER_CHANNEL_RETRY_INTERVAL_MS + 'ms');
|
||||
setTimeout('initializeBrowserChannel()', BROWSER_CHANNEL_RETRY_INTERVAL_MS);
|
||||
}
|
||||
socket.onmessage = function(evt) {
|
||||
var url = evt.data;
|
||||
var regex = /http[s]?:\/\//;
|
||||
if (regex.test(url)) {
|
||||
chrome.tabs.create({url: url})
|
||||
}
|
||||
}
|
||||
} else if (req.status == 400) {
|
||||
if (req.responseText == 'LOGIN_REQUIRED') {
|
||||
console.log('Not initializing browser channel because user not logged in');
|
||||
}
|
||||
} else {
|
||||
setTimeout('initializeBrowserChannel()', BROWSER_CHANNEL_RETRY_INTERVAL_MS);
|
||||
}
|
||||
}
|
||||
};
|
||||
req.send();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user