Files
chrometophone/third_party/firefox_sendtophone/chrome/content/overlay.js

229 lines
6.1 KiB
JavaScript

/*
Copyright 2010 Alfonso Martínez de Lizarrondo & Patrick O'Reilly
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
// Core functions
Components.utils.import("resource://sendtophone/sendtophone.js");
// Protocol handlers
Components.utils.import("resource://sendtophone/protocolHandlers.js");
var sendtophone = {
init: function()
{
// Each app will implement its specific initialization
},
onLoad: function()
{
var me = sendtophone;
me.strings = document.getElementById("sendtophone-strings");
me.prefs = Components.classes["@mozilla.org/preferences-service;1"]
.getService(Ci.nsIPrefService)
.getBranch("extensions.sendtophone.") ;
me.init();
},
// Detect images of QR codes generated by the Google Charts API
detectQR: function( url )
{
var match = url.match(/^http:\/\/chart.apis.google.com\/chart\?(.*)/i);
if (!match)
return false;
var chartLink=/^chl=/;
var qrArray = match[0].split("&");
for(var qrI=0; qrI<qrArray.length; qrI++){
if(chartLink.test(qrArray[qrI])){
//Decode any data encoded in the QR Image Link
return decodeURIComponent(qrArray[qrI].replace(chartLink, ''));
}
}
},
onMenuItemCommand: function(e, type)
{
var title, url, selection = '';
switch(type)
{
case 'link':
title = gContextMenu.linkText();
url = gContextMenu.linkURL;
break;
case 'image':
title = gContextMenu.target.title || gContextMenu.target.alt;
url = gContextMenu.imageURL;
break;
case 'qr':
title = gContextMenu.target.title || gContextMenu.target.alt;
url = gContextMenu.imageURL;
var data = this.detectQR(url);
if (this.validURI(data))
url = data;
else
selection = data;
//If the QR Image has no title text, give it one.
if (!title)
title=this.strings.getString("qrTitle");
break;
case 'text':
title = "Selection";
url = 'http://google.com/';
var input = gContextMenu.target;
if (gContextMenu.onTextInput && input && input.value)
{
selection = input.value.substring(input.selectionStart, input.selectionEnd);
}
else
{
// Get the selection from the correct iframe
var focusedWindow = document.commandDispatcher.focusedWindow;
selection = focusedWindow.getSelection().toString();
}
break;
case 'pageButton':
// Check if there's a single image with a QR code in the contents
var images = gBrowser.contentDocument.getElementsByTagName( "img" );
var QRs = [];
for( var i=0; i<images.length; i++)
{
var data = this.detectQR( images[i].src );
if (data)
QRs.push({data: data, img: images[i]});
}
if (QRs.length==1)
{
var data = QRs[0].data;
var url = data.substring(0, 80);
var question = this.strings.getString("ConfirmQR").replace("%s", url) ;
if (this.confirm( question ) )
{
title = QRs[0].img.title || QRs[0].img.alt;
if (this.validURI(data))
url = data;
else
selection = data;
if (!title)
title=this.strings.getString("qrTitle");
// We got the data, break out of the select
break;
}
}
// fall through
case 'page':
default:
var info = this.getInfo();
title = info.title;
url = info.url;
selection = info.selection;
break;
}
sendtophoneCore.send(title, url, selection);
},
// Shows a message in a modal alert
alert: function(text)
{
var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
.getService(Ci.nsIPromptService);
promptService.alert(window, this.strings.getString("SendToPhoneTitle"),
text);
},
// Shows a message in a modal confirm
confirm: function(text)
{
var promptService = Components.classes["@mozilla.org/embedcomp/prompt-service;1"]
.getService(Ci.nsIPromptService);
// https://developer.mozilla.org/en/XPCOM_Interface_Reference/nsIPromptService#confirmEx
var check = {value: true};
// confirmEx returns the pressed button, and Yes it's the first one.
return (0 == promptService.confirmEx(window, this.strings.getString("SendToPhoneTitle"),
text, promptService.STD_YES_NO_BUTTONS, "", "", "", null, check));
},
onToolbarButtonCommand: function(e) {
// just reuse the function above.
sendtophone.onMenuItemCommand(e, 'pageButton');
},
getInfo: function() {
var doc = gBrowser.contentDocument,
win = doc.defaultView;
var href = doc.location.href;
// Is it the Google Maps page?
if (this.isMapsURL(href))
{
// Then try to send the current view:
var link = doc.getElementById('link');
if (link && link.href)
href = link.href;
}
return {
"title": doc.title,
"url": href,
"selection": win.getSelection().toString()
};
},
isMapsURL: function(url)
{
return url.match("http://maps\\.google\\.[a-z]{2,3}(\\.[a-z]{2})?[/?].*") || url.match("http://www\\.google\\.[a-z]{2,3}(\\.[a-z]{2})?/maps.*");
},
validURI: function(uri)
{
return (/^(https?|market|tel|sms(to)?|mms(to)?|mailto|ftp):/i).test( uri );
},
initPopup: function()
{
var fileServerUrl = this.prefs.getCharPref( "fileServerUrl" );
if (!fileServerUrl)
{
document.getElementById("sendtophoneContextMenuSendFiles").hidden = true;
document.getElementById("sendtophoneContextMenuSendFolder").hidden = true;
}
// returning true will make the popup show
return true;
},
logout: function()
{
sendtophoneCore.logout();
}
};
window.addEventListener("load", sendtophone.onLoad, false);