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
|
// show or hide the menuitem based on what the context menu is on
|
||||||
// see http://kb.mozillazine.org/Adding_items_to_menus
|
// see http://kb.mozillazine.org/Adding_items_to_menus
|
||||||
gContextMenu.showItem("context-sendtophone-link", gContextMenu.onLink);
|
gContextMenu.showItem("context-sendtophone-link", gContextMenu.onLink);
|
||||||
gContextMenu.showItem("context-sendtophone-image", gContextMenu.onImage);
|
|
||||||
var qrPat1=/^http:\/\/chart.apis.google.com\/chart\?/;
|
gContextMenu.showItem("context-sendtophone-image", false);
|
||||||
var qrPat2=/cht=qr/;
|
gContextMenu.showItem("context-sendtophone-qrimage", false);
|
||||||
gContextMenu.showItem("context-sendtophone-qrimage", (gContextMenu.onImage & qrPat1.test(gContextMenu.imageURL) && qrPat2.test(gContextMenu.imageURL)));
|
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.showItem("context-sendtophone-text", gContextMenu.isTextSelected ||
|
||||||
(gContextMenu.onTextInput && gContextMenu.target.selectionEnd > gContextMenu.target.selectionStart) );
|
(gContextMenu.onTextInput && gContextMenu.target.selectionEnd > gContextMenu.target.selectionStart) );
|
||||||
|
|
||||||
|
|||||||
@@ -40,51 +40,54 @@ var sendtophone = {
|
|||||||
me.init();
|
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)
|
onMenuItemCommand: function(e, type)
|
||||||
{
|
{
|
||||||
var title, url, selection;
|
var title, url, selection = '';
|
||||||
switch(type)
|
switch(type)
|
||||||
{
|
{
|
||||||
case 'link':
|
case 'link':
|
||||||
title = gContextMenu.linkText();
|
title = gContextMenu.linkText();
|
||||||
url = gContextMenu.linkURL;
|
url = gContextMenu.linkURL;
|
||||||
selection = '';
|
|
||||||
break;
|
break;
|
||||||
case 'image':
|
case 'image':
|
||||||
title = gContextMenu.target.title || gContextMenu.target.alt;
|
title = gContextMenu.target.title || gContextMenu.target.alt;
|
||||||
url = gContextMenu.imageURL;
|
url = gContextMenu.imageURL;
|
||||||
selection = '';
|
|
||||||
break;
|
break;
|
||||||
case 'qr':
|
case 'qr':
|
||||||
title = gContextMenu.target.title || gContextMenu.target.alt;
|
title = gContextMenu.target.title || gContextMenu.target.alt;
|
||||||
url = gContextMenu.imageURL;
|
url = gContextMenu.imageURL;
|
||||||
|
|
||||||
// Detect images of QR codes generated by the Google Charts API
|
var data = this.detectQR(url);
|
||||||
// Extract the URI if valid and sendtophone
|
if (this.validURI(data))
|
||||||
var chartLink=/^chl=/;
|
url = data;
|
||||||
var chartUrl=/^http:\/\/chart.apis.google.com\/chart\?/;
|
else
|
||||||
|
selection = data;
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
//If the QR Image has no title text, give it one.
|
//If the QR Image has no title text, give it one.
|
||||||
|
if (!title)
|
||||||
if(!title){
|
|
||||||
title=this.strings.getString("qrTitle");
|
title=this.strings.getString("qrTitle");
|
||||||
}
|
|
||||||
selection = '';
|
|
||||||
break;
|
break;
|
||||||
case 'text':
|
case 'text':
|
||||||
title = "Selection";
|
title = "Selection";
|
||||||
url = 'http://www.google.com/';
|
url = 'http://google.com/';
|
||||||
var input = gContextMenu.target;
|
var input = gContextMenu.target;
|
||||||
if (gContextMenu.onTextInput && input && input.value)
|
if (gContextMenu.onTextInput && input && input.value)
|
||||||
{
|
{
|
||||||
@@ -97,6 +100,40 @@ var sendtophone = {
|
|||||||
selection = focusedWindow.getSelection().toString();
|
selection = focusedWindow.getSelection().toString();
|
||||||
}
|
}
|
||||||
break;
|
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':
|
case 'page':
|
||||||
default:
|
default:
|
||||||
var info = this.getInfo();
|
var info = this.getInfo();
|
||||||
@@ -118,9 +155,22 @@ var sendtophone = {
|
|||||||
text);
|
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) {
|
onToolbarButtonCommand: function(e) {
|
||||||
// just reuse the function above.
|
// just reuse the function above.
|
||||||
sendtophone.onMenuItemCommand(e, 'page');
|
sendtophone.onMenuItemCommand(e, 'pageButton');
|
||||||
},
|
},
|
||||||
|
|
||||||
getInfo: function() {
|
getInfo: function() {
|
||||||
|
|||||||
@@ -17,5 +17,7 @@ InvalidFile=Not a valid file
|
|||||||
SendFileToPhone=Send files to your phone.
|
SendFileToPhone=Send files to your phone.
|
||||||
SendFolderToPhone=Send a folder to your phone.
|
SendFolderToPhone=Send a folder to your phone.
|
||||||
qrTitle=QR Image Link
|
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.
|
FileUploadsDisabled=It's not possible to upload files to the phone.
|
||||||
FileTooBig=The file is too big.
|
FileTooBig=The file is too big.
|
||||||
|
|||||||
@@ -17,5 +17,7 @@ InvalidFile=No es un fichero válido.
|
|||||||
SendFileToPhone=Envía ficheros al teléfono.
|
SendFileToPhone=Envía ficheros al teléfono.
|
||||||
SendFolderToPhone=Envía una carpeta al teléfono.
|
SendFolderToPhone=Envía una carpeta al teléfono.
|
||||||
qrTitle=Imagen QR
|
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.
|
FileUploadsDisabled=No se pueden enviar ficheros al teléfono.
|
||||||
FileTooBig=El fichero que quiere enviar es demasiado grande.
|
FileTooBig=El fichero que quiere enviar es demasiado grande.
|
||||||
|
|||||||
@@ -22,6 +22,7 @@
|
|||||||
|
|
||||||
#context-sendtophone-link,
|
#context-sendtophone-link,
|
||||||
#context-sendtophone-image,
|
#context-sendtophone-image,
|
||||||
|
#context-sendtophone-qrimage,
|
||||||
#context-sendtophone-text,
|
#context-sendtophone-text,
|
||||||
#context-sendtophone-page
|
#context-sendtophone-page
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user