Added detection of QR when clicking the toolbar button, minor tweaks.

This commit is contained in:
amla70
2010-08-12 19:29:28 +00:00
parent a2e6fc5342
commit 43f94b6a7e
5 changed files with 97 additions and 29 deletions

View File

@@ -40,51 +40,54 @@ var 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;
var title, url, selection = '';
switch(type)
{
case 'link':
title = gContextMenu.linkText();
url = gContextMenu.linkURL;
selection = '';
break;
case 'image':
title = gContextMenu.target.title || gContextMenu.target.alt;
url = gContextMenu.imageURL;
selection = '';
break;
case 'qr':
title = gContextMenu.target.title || gContextMenu.target.alt;
url = gContextMenu.imageURL;
// Detect images of QR codes generated by the Google Charts API
// Extract the URI if valid and sendtophone
var chartLink=/^chl=/;
var chartUrl=/^http:\/\/chart.apis.google.com\/chart\?/;
url=url.replace(chartUrl, '');
qrArray = url.split("&");
for(qrI=0;qrI<qrArray.length;qrI++){
if(chartLink.test(qrArray[qrI])){
//Decode and Unescape any URL encoded in the QR Image Link
url=decodeURI(unescape(qrArray[qrI].replace(chartLink, '')));
if(this.validURI(url)){
url=url;
}
}
}
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){
if (!title)
title=this.strings.getString("qrTitle");
}
selection = '';
break;
case 'text':
title = "Selection";
url = 'http://www.google.com/';
url = 'http://google.com/';
var input = gContextMenu.target;
if (gContextMenu.onTextInput && input && input.value)
{
@@ -97,6 +100,40 @@ var sendtophone = {
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();
@@ -118,9 +155,22 @@ var sendtophone = {
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, 'page');
sendtophone.onMenuItemCommand(e, 'pageButton');
},
getInfo: function() {