Skip to content

Commit

Permalink
Keyboard shortcut
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacopo committed Jun 19, 2011
1 parent 4ae8319 commit 4a6d8c1
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 19 deletions.
3 changes: 3 additions & 0 deletions _locales/en/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
"optEmailThis": {
"message": "Add an 'Email this' command to the right-click menu"
},
"optEnableShortcut": {
"message": "Use Ctrl+Shift+M to email the current page"
},
"optNoteReload": {
"message": "Note: you might need to reload tabs for the options to take effect"
},
Expand Down
3 changes: 3 additions & 0 deletions _locales/it/messages.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,9 @@
"optEmailThis": {
"message": "Aggiungi il comando 'Invia via e-mail' al menu contestuale (tasto destro)."
},
"optEnableShortcut": {
"message": "Ctrl-Maiusc-M invia la pagina via e-mail."
},
"optNoteReload": {
"message": "Nota: potrebbe essere necessario ricaricare le pagine perché le modifiche abbiano effetto."
},
Expand Down
51 changes: 33 additions & 18 deletions background.html
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
//
// 2) Registers and handles the right-click context menus
//
// 3) Receives and handles Ctrl-Shift-M notifications from the
// content scripts
//
//////////////////////////////////////////////////////////////////////

var baseGmailUrl = "https://mail.google.com/";
Expand All @@ -40,20 +43,31 @@
// We also use it to add or remove the context menus
// Note: the options page sends this on save
chrome.extension.onConnect.addListener(function(port) {
if (port.name != "GmailUrlConn") {
} else {
port.onMessage.addListener(function(msg) {
if (msg.req == "OptionsPlease") {
port.postMessage({gmailDomainUrl: makeGmailDomainUrl() + mailtoUrlSuffix, windowOptions: window.localStorage["gmail_window_options"]});
addToKnownPorts(port);
} else if (msg.req == "OptionsChanged") {
sendToKnownPorts({gmailDomainUrl: makeGmailDomainUrl() + mailtoUrlSuffix, windowOptions: window.localStorage["gmail_window_options"]});
refreshContextMenus();
} else {
console.log("Unsupported req on valid port");
}
});
}
if (port.name != "GmailUrlConn")
return;

port.onMessage.addListener(function(msg) {
if (msg.req == "OptionsPlease") {
port.postMessage({
gmailDomainUrl: makeGmailDomainUrl() + mailtoUrlSuffix,
windowOptions: window.localStorage["gmail_window_options"],
enableShortcut: !(window.localStorage["enable_shortcut"] == "off")
});
addToKnownPorts(port);
} else if (msg.req == "OptionsChanged") {
sendToKnownPorts({
gmailDomainUrl: makeGmailDomainUrl() + mailtoUrlSuffix,
windowOptions: window.localStorage["gmail_window_options"],
enableShortcut: !(window.localStorage["enable_shortcut"] == "off")
});
refreshContextMenus();
} else if (msg.req == "EmailThisPage") {
console.log(port);
openGmail(port.sender.tab.url, port.sender.tab.title);
} else {
console.log("Unsupported req on valid port");
}
});
});

function addToKnownPorts(port) {
Expand All @@ -74,7 +88,6 @@

// Context menu callbacks
function menuClicked(type, info, tab) {
// 1) Determine the default text for the e-mail
var emailStr = "";
var emailSubj = "";
var addPage = true;
Expand All @@ -98,11 +111,13 @@
}
if (addPage)
emailStr = emailStr + "\nFrom: " + info.pageUrl;

// 2) Build the Gmail URL
openGmail(emailStr, emailSubj);
}
function openGmail(emailStr, emailSubj) {
// Build the Gmail URL
var gmailFullUrl = makeGmailDomainUrl() + "mail/?view=cm&fs=1&tf=1&su=" + encodeURIComponent(emailSubj) + "&body=" + encodeURIComponent(emailStr);

// 3) Open the composition window/tab
// Open the composition window/tab
window.open(gmailFullUrl,"_blank",window.localStorage["gmail_window_options"]);
}
function creationCallback() {
Expand Down
12 changes: 12 additions & 0 deletions mailto.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
var cachedGmailUrl = "";
var windowOptions = "";
var listenersAdded = false;
var enableShortcut = false;

function encodeForMailto(inUrl) {
// GMail unescapes most of the string in the first step,
Expand Down Expand Up @@ -58,6 +59,12 @@ function creaListener(originalUrl)
};
}

function keyupListener(ev)
{
if (ev.ctrlKey && ev.shiftKey && (ev.keyCode == 77))
bgPort.postMessage({req: "EmailThisPage"});
}


if (cachedGmailUrl != "") {
rewriteMailtosOnPage();
Expand All @@ -70,7 +77,12 @@ function(msg) {
//console.log("Got message from bg page - " + msg.windowOptions);
cachedGmailUrl = msg.gmailDomainUrl;
windowOptions = msg.windowOptions;
enableShortcut = msg.enableShortcut;

if (!listenersAdded)
rewriteMailtosOnPage();

if (enableShortcut)
window.addEventListener("keyup", keyupListener, false);
else window.removeEventListener("keyup", keyupListener, false);
});
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@
"name": "__MSG_extName__",
"default_locale": "en",
"options_page": "options.html",
"version": "1.11.10"
"version": "1.11.11"
}
5 changes: 5 additions & 0 deletions options.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
document.getElementById('optInTab').innerHTML = chrome.i18n.getMessage('optInTab');
document.getElementById('optInWin').innerHTML = chrome.i18n.getMessage('optInWin');
document.getElementById('optEmailThis').innerHTML = chrome.i18n.getMessage('optEmailThis');
document.getElementById('optEnableShortcut').innerHTML = chrome.i18n.getMessage('optEnableShortcut');
document.getElementById('optNoteReload').innerHTML = chrome.i18n.getMessage('optNoteReload');
document.getElementById('options_save').value = chrome.i18n.getMessage('optSave');
}
Expand All @@ -65,6 +66,7 @@
document.getElementById('open_new_tab').checked = (windowOptions != optionsForNewWindow);
document.getElementById('open_new_window').checked = (windowOptions == optionsForNewWindow);
document.getElementById('email_this').checked = !(localStorage["enable_email_this"] == "off");
document.getElementById('enable_shortcut').checked = !(localStorage["enable_shortcut"] == "off");
}

function saveOptions() {
Expand All @@ -76,6 +78,7 @@
}
localStorage["gmail_window_options"] = document.getElementById('open_new_window').checked ? optionsForNewWindow : "";
localStorage["enable_email_this"] = document.getElementById('email_this').checked ? "on" : "off";
localStorage["enable_shortcut"] = document.getElementById('enable_shortcut').checked ? "on" : "off";

// Notify the background page
var bgPort = chrome.extension.connect({name: "GmailUrlConn"});
Expand All @@ -96,6 +99,8 @@ <h2><span id="optOpenNewWindow"></span></h2>
</div>
<div id="email_this_option" class="options_group">
<label><input onclick="saveOptions()" id="email_this" type="checkbox" /><span id="optEmailThis"></span></label>
<br />
<label><input onclick="saveOptions()" id="enable_shortcut" type="checkbox" /><span id="optEnableShortcut"></span></label>
</div>
<h2><span id="optGoogleApps"></span></h2>
<div id="apps_for_domain_option" class="options_group" style="border-bottom: none; margin-bottom: 2em;">
Expand Down

0 comments on commit 4a6d8c1

Please sign in to comment.