mirror of
https://github.com/fergalmoran/chrometophone.git
synced 2025-12-22 09:41:51 +00:00
Added detection of QR when clicking the toolbar button, minor tweaks.
This commit is contained in:
@@ -86,10 +86,23 @@ sendtophone.showFirefoxContextMenu = function(event) {
|
||||
// show or hide the menuitem based on what the context menu is on
|
||||
// see http://kb.mozillazine.org/Adding_items_to_menus
|
||||
gContextMenu.showItem("context-sendtophone-link", gContextMenu.onLink);
|
||||
gContextMenu.showItem("context-sendtophone-image", gContextMenu.onImage);
|
||||
var qrPat1=/^http:\/\/chart.apis.google.com\/chart\?/;
|
||||
var qrPat2=/cht=qr/;
|
||||
gContextMenu.showItem("context-sendtophone-qrimage", (gContextMenu.onImage & qrPat1.test(gContextMenu.imageURL) && qrPat2.test(gContextMenu.imageURL)));
|
||||
|
||||
gContextMenu.showItem("context-sendtophone-image", false);
|
||||
gContextMenu.showItem("context-sendtophone-qrimage", false);
|
||||
if (gContextMenu.onImage)
|
||||
{
|
||||
var data = this.detectQR( gContextMenu.imageURL );
|
||||
if (data)
|
||||
{
|
||||
gContextMenu.showItem("context-sendtophone-qrimage", true);
|
||||
var label = this.strings.getString("qrContextMenu");
|
||||
label = label.replace("%s", data.substring(0, 20) + "..." );
|
||||
document.getElementById("context-sendtophone-qrimage").setAttribute("label", label);
|
||||
}
|
||||
else
|
||||
gContextMenu.showItem("context-sendtophone-image", true);
|
||||
}
|
||||
|
||||
gContextMenu.showItem("context-sendtophone-text", gContextMenu.isTextSelected ||
|
||||
(gContextMenu.onTextInput && gContextMenu.target.selectionEnd > gContextMenu.target.selectionStart) );
|
||||
|
||||
|
||||
@@ -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() {
|
||||
|
||||
@@ -17,5 +17,7 @@ InvalidFile=Not a valid file
|
||||
SendFileToPhone=Send files to your phone.
|
||||
SendFolderToPhone=Send a folder to your phone.
|
||||
qrTitle=QR Image Link
|
||||
ConfirmQR=A QR image has been detected\r\n"%s"\r\nDo you want to send that instead of the page?
|
||||
qrContextMenu=Send "%s" to Android
|
||||
FileUploadsDisabled=It's not possible to upload files to the phone.
|
||||
FileTooBig=The file is too big.
|
||||
|
||||
@@ -17,5 +17,7 @@ InvalidFile=No es un fichero válido.
|
||||
SendFileToPhone=Envía ficheros al teléfono.
|
||||
SendFolderToPhone=Envía una carpeta al teléfono.
|
||||
qrTitle=Imagen QR
|
||||
ConfirmQR=A QR image has been detected\r\n"%s"\r\nDo you want to send that instead of the page?
|
||||
qrContextMenu=Send "%s" to Android
|
||||
FileUploadsDisabled=No se pueden enviar ficheros al teléfono.
|
||||
FileTooBig=El fichero que quiere enviar es demasiado grande.
|
||||
|
||||
@@ -22,6 +22,7 @@
|
||||
|
||||
#context-sendtophone-link,
|
||||
#context-sendtophone-image,
|
||||
#context-sendtophone-qrimage,
|
||||
#context-sendtophone-text,
|
||||
#context-sendtophone-page
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user