Skip to content

Instantly share code, notes, and snippets.

@philipbjorge
Created November 7, 2015 00:46
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 philipbjorge/1fbc1dad2df50d782962 to your computer and use it in GitHub Desktop.
Save philipbjorge/1fbc1dad2df50d782962 to your computer and use it in GitHub Desktop.
Fastmail Unsubscribe Button GreaseMonkey
// ==UserScript==
// @name Fastmail Unsubscribe
// @namespace philipbjorge-fastmail-unsubscribe
// @include https://*.fastmail.com/*
// @include https://fastmail.com/*
// @require http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @require https://gist.github.com/raw/2625891/waitForKeyElements.js
// @version 1
// @grant GM_addStyle
// ==/UserScript==
// Makes contains case insensitive
jQuery.expr[':'].Contains = function(a, i, m) {
return jQuery(a).text().toUpperCase()
.indexOf(m[3].toUpperCase()) >= 0;
};
function unsubscribeInAnchor(node) {
return jQuery(node).find("a:Contains('unsubscribe')");
};
function unsubscribeInHref(node) {
return jQuery(node).find('a[href*="unsubscribe"]');
};
function linkNearUnsubscribeText(node) {
return jQuery(node).find("a").parent(":Contains('unsubscribe')").filter(function(){
return jQuery(this).find('a').length == 1;
}).find("a");
};
waitForKeyElements (".v-Message-body", function(node) {
jQuery("#fm-unsubscribe").remove();
var unsubLink = null;
var strategies = [unsubscribeInAnchor, unsubscribeInHref, linkNearUnsubscribeText];
for(var i = 0; !unsubLink && i < strategies.length; i++) {
var strategy = strategies[i];
var result = strategy(node);
unsubLink = (result.length == 0) ? null : result.eq(0).attr('href');
}
if (unsubLink) {
jQuery(".v-Toolbar-section.v-Toolbar-section--right").append(
jQuery("<button id='fm-unsubscribe' class='v-Button' style='filter: hue-rotate(100deg)'>" +
"<span class='label'>Unsubscribe</span>" +
"</button>")
);
jQuery("#fm-unsubscribe").click(function() {
window.open(unsubLink);
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment