mirror of
https://github.com/fergalmoran/chrometophone.git
synced 2025-12-22 09:41:51 +00:00
164 lines
4.3 KiB
JavaScript
164 lines
4.3 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();
|
|
},
|
|
|
|
onMenuItemCommand: function(e, type)
|
|
{
|
|
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 = '';
|
|
// Detect images of QR codes generated by the Google Charts API
|
|
// Extract the URI if valid and sendtophone
|
|
var qrPref = this.prefs.getBoolPref( "qrlink" ) ;
|
|
var chartType="cht=qr";
|
|
var chartLink="chl=";
|
|
var chartUrl="http://chart.apis.google.com/chart?";
|
|
if(!url.indexOf(chartUrl)&&url.match(chartType)&&qrPref){
|
|
qrUrl=url.replace(chartUrl, '');
|
|
qrArray = qrUrl.split("&");
|
|
for(qrI=0;qrI<qrArray.length;qrI++){
|
|
if(!qrArray[qrI].indexOf(chartLink)){
|
|
//Decode and Unescape any URL encoded in the QR Image Link
|
|
qrUrl=decodeURI(unescape(qrArray[qrI].replace(chartLink, '')));
|
|
if(this.validURI(qrUrl)){
|
|
url=qrUrl;
|
|
}
|
|
}
|
|
}
|
|
|
|
if(!title){
|
|
title=this.strings.getString("qrTitle");
|
|
}
|
|
}
|
|
break;
|
|
case 'text':
|
|
title = "Selection";
|
|
url = 'http://www.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 '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);
|
|
},
|
|
|
|
onToolbarButtonCommand: function(e) {
|
|
// just reuse the function above.
|
|
sendtophone.onMenuItemCommand(e, 'page');
|
|
},
|
|
|
|
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.*");
|
|
},
|
|
|
|
initPopup: function()
|
|
{
|
|
// returning true will make the popup show
|
|
return true;
|
|
},
|
|
|
|
logout: function()
|
|
{
|
|
sendtophoneCore.logout();
|
|
}
|
|
|
|
};
|
|
|
|
window.addEventListener("load", sendtophone.onLoad, false);
|