// ==UserScript== // @name mail.ru: clean links // @namespace lainscripts_mailru_clean_links // @description Dereferences mail.ru links in emails when clicked. Skips advertisement block displayed when some links are clicked. // @author lainverse // @license CC BY-SA // @version 4.1 // @include https://e.mail.ru/* // @grant none // @downloadURL none // ==/UserScript== var confirmForm = null, locate_confirmer = function() { confirmForm = document.querySelector('#MailRuConfirm'); if (!confirmForm) setTimeout(locate_confirmer, 100); }; locate_confirmer(); var clb = /^https?:\/\/r\.mail\.ru\/cl[a-z][0-9]+\/(.*)/i, cgi = /&(amp;)?url=([^&]*)/i, clearLink = function(evt) { evt.preventDefault(); var i = 10, x, confirmer = function() { i--; if (confirmForm.style.display !== 'none') confirmForm.querySelector('.confirm-cancel').click(); else if (i>0) setTimeout(confirmer, 10); }; for (x in this) if (x.indexOf('__originUrl') > -1) { var res = null; do { res = cgi.exec(this[x]); if (res) this[x] = decodeURIComponent(res[2]); res = clb.exec(this[x]); if (res) this[x] = 'http://' + res[1]; } while (res); this.href = this[x]; console.log("Dereferenced link:", this[x]); window.open(this[x], '_blank').focus(); if (confirmForm) confirmer(); } }, task = function () { var links = document.querySelectorAll('#b-letter A:not(.fixed)'); if (links.length > 0) for (var l = 0; l < links.length; l++) { links[l].classList.add("fixed"); links[l].onclick = clearLink; } }; function link_monitor() { task(); setTimeout(link_monitor, 100); } setTimeout(link_monitor, 0);