fork-openpgpjs/plugins/chrome/background.html
2011-12-09 18:29:47 +01:00

134 lines
4.6 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script type="text/javascript">
//GPG4Browsers - An OpenPGP implementation in javascript
//Copyright (C) 2011 Recurity Labs GmbH
//
//This library is free software; you can redistribute it and/or
//modify it under the terms of the GNU Lesser General Public
//License as published by the Free Software Foundation; either
//version 2.1 of the License, or (at your option) any later version.
//
//This library is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
//Lesser General Public License for more details.
//
//You should have received a copy of the GNU Lesser General Public
//License along with this library; if not, write to the Free Software
//Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
var tab_registry = new Array();
var account_name = null;
/**
* Event listener for chrom.extension message interface.
* This is the main data exchange interface for communication between windows.
*/
function onRequest(request, sender, sendResponse) {
// only the extension itself
if (document.URL.split("/")[2] != sender.id)
return;
/* the following code is currently not working, but a check for incognito mode would be nice to have here.
if (chrome.extension.inIncognitoContext != true)
alert("enable incognito mode for GPG4Browsers Extension!"+JSON.stringify(chrome.extension.inIncognitoContext));
*/
// Show the page action for the tab that the sender (content script)
// was on.
if (tab_registry[sender.tab.id] != null) {
sendResponse(tab_registry[sender.tab.id]);
tab_registry[sender.tab.id] = null;
return;
}
// page action... open the pgp tab with compose
if (request.action == 0) {
openComposeWindow(request);
sendResponse({});
return;
}
// request from contentscript on gmail interface to open a openpgp window with a message to decrypt/verify
if (request.action == 1) {
openComposeWindow(request);
sendResponse({});
return;
}
// openpgp is requesting to open a gmail compose window
if (request.action == 2 && sender.tab.id) {
openGmailComposeWindow(request);
sendResponse({});
return;
}
// contentscript on gmail compose window requesting the message
// not used due to missing implemenation
if (request.action == 3 && sender.tab.id) {
sendResponse(tab_registry[sender.tab.id]);
return;
}
account_name = request.account;
chrome.pageAction.show(sender.tab.id);
// Return nothing to let the connection be cleaned up.
sendResponse({});
}
chrome.pageAction.onClicked.addListener(pageActionListener);
/**
* opens the Gmail ComposeWindow and transmits the email data
* @param to string of recipient email addresses separated by ", "
* @param cc string of recipient email addresses separated by ", " receiving a copy
* @param cc string of recipient email addresses separated by ", " receiving a blind copy
* @param subject email subject as string
*/
function openGmailComposeWindow(request) {
var tab = chrome.tabs.create(
{url: "https://mail.google.com/mail/?view=cm&fs=1&tf=1&to="+
encodeURIComponent(request.to)+"&cc="+
encodeURIComponent(request.cc)+"&bcc="+
encodeURIComponent(request.bcc)+"&su="+
encodeURIComponent(request.subject)+"&body="+
// TODO: long bodys result in an error on the google mail server because the URL gets too long
encodeURIComponent(request.body)+"&shva=1", selected: true});
// its a good idea to store the request...
// once the gmail compose window has opened, the content
// script could fetch the request to insert message data
// into the forms this would be a proper solution to the
// "url too long" issue. Problem: the page is rendered
// within an iframe so contentscript has no access to
// fill in the data. We have'nt tried the basic html
// approach yet.. see action == "3"
tab_registry[tab.id] = request;
}
/**
* the page action listener event handler:
* opens a compose window (openpgp.html)
*/
function pageActionListener(tab) {
openComposeWindow({action: 0, account: account_name});
}
/**
*
*/
function openComposeWindow(request) {
var tab = chrome.tabs.create({url: "openpgp.html", selected: true }, function(tab) {
tab_registry[tab.id] = request;
});
}
// Listen for the content script to send a message to the background page.
chrome.extension.onRequest.addListener(onRequest);
</script>
</head>
</html>