Skip to content

Instantly share code, notes, and snippets.

@chrisroos
Last active November 11, 2018 15:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chrisroos/8840a1d9c506236b57fb to your computer and use it in GitHub Desktop.
Save chrisroos/8840a1d9c506236b57fb to your computer and use it in GitHub Desktop.
Bookmarklet that allows me to add a link to more information for specific emails I receive

I receive a number of periodic emails that prompt me to do something (e.g. take an electricity meter reading). I wanted to be able to add information to those emails to remind me how to perform the task.

This JavaScript allows me to configure rules so that I can display a link to a web page when I'm viewing an email with a specific subject in Gmail.

I'm currently hosting the JavaScript and installer web page using rawgit.com.

Visit this page to install the bookmarklet.

// Original
(function() {
var script = document.createElement('script');
script.src = 'https://rawgit.com/chrisroos/8840a1d9c506236b57fb/raw/5cd978e1b01aee2c58fd6ffbfeabc61fb55b410d/gmail-rules.js';
document.body.appendChild(script);
})();
// One line (done by hand)
(function(){ var script = document.createElement('script'); script.src = 'https://rawgit.com/chrisroos/8840a1d9c506236b57fb/raw/5cd978e1b01aee2c58fd6ffbfeabc61fb55b410d/gmail-rules.js'; document.body.appendChild(script); })();
var rules = [
{
subject: /Reminder to file your VAT Return/,
url: 'https://github.com/freerange/business/wiki/Reminder-to-file-your-VAT-Return:-Email-from-HMRC'
}
];
var matchEmails = function($) {
// The subject appears in an h2 in Gmail web interface
$('h2').each(function(index, element) {
$(rules).each(function(index, rule) {
if ($(element).text().match(rule.subject)) {
var link = $('<a>');
link.attr('href', rule.url);
link.text('More information');
$(element).append(' - ');
$(element).append(link);
};
});
});
};
var jQueryAvailable = function() {
return (typeof(jQuery) != 'undefined');
};
if (!jQueryAvailable()) {
var script = document.createElement('script');
script.src = 'https://code.jquery.com/jquery-2.1.3.min.js';
document.body.appendChild(script);
};
// Wait for jQuery before calling matchEmails()
var pid = setInterval(function() {
if (jQueryAvailable) {
matchEmails(jQuery);
clearInterval(pid);
}
}, 1000);
<a href="javascript:(function(){ var script = document.createElement('script'); script.src = 'https://rawgit.com/chrisroos/8840a1d9c506236b57fb/raw/5cd978e1b01aee2c58fd6ffbfeabc61fb55b410d/gmail-rules.js'; document.body.appendChild(script); })();">Gmail rules</a>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment