Created
March 24, 2012 11:57
-
-
Save gurugray/2181532 to your computer and use it in GitHub Desktop.
кнопка «Архив» для почты Яндекса
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name «Yandex Mail Archive button» | |
// @namespace gurugray | |
// @author Sergey sergeev (gurugray) | |
// @version 0.2.4 | |
// @homepage_url https://raw.github.com/gist/2181532/yandex-mail.archive.user.js | |
// @description Создаём кнопку «В архив» на web-почте Яндекса | |
// @include http*://mail.yandex*/neo2/* | |
// ==/UserScript== | |
var __msgRegExp = /message\//, | |
__showedButton = false, | |
__archiveTitle = 'Архив', | |
__buttonTitle = 'В архив'; | |
setTimeout(function(){ | |
var initTime = setInterval(function(){ | |
if (!!_$('.block-toolbar:not([style^="display"]) img.b-ico_compose')[0]) { | |
__init(); | |
clearInterval(initTime); | |
} | |
}, 500); | |
}, 1000); | |
function __eventLoop(){ | |
if ((__isMailChecked() || __isReadingMessage()) && !__isArchiveFolder()) { | |
__showButton(); | |
} else { | |
__hideButton(); | |
} | |
} | |
function __isReadingMessage(){ | |
return __msgRegExp.test(unsafeWindow.location.hash); | |
} | |
function _$(path) { | |
return document.querySelectorAll(path); | |
} | |
function __init(){ | |
var a = document.createElement('a'); | |
a.className = 'b-toolbar__item b-toolbar__item_archive', | |
a.href = '#archive'; | |
a.style.display = 'none'; | |
a.style.width = '5em'; | |
a.title = __buttonTitle; | |
a.innerHTML = '<img class="b-ico b-ico_archive" src="https://mailstatic.yandex.net/neo2/5.6.21/static/lego/blocks/b-ico/b-ico.gif" style="background-position: -233px 0;"><span class="b-toolbar__item__label">'+__buttonTitle+'</span><span class="b-toolbar__item__selected b-toolbar__item__selected_left-border"></span><span class="b-toolbar__item__selected b-toolbar__item__selected_right-border"></span>'; | |
var chevron = _$('.block-toolbar:not([style^="display"]) .b-toolbar__block_chevron')[0]; | |
chevron.insertBefore(a, chevron.firstChild); | |
__locationHash = unsafeWindow.location.hash; | |
__addSingleEvents(); | |
setInterval(__eventLoop, 200); | |
} | |
function __isArchiveFolder(){ | |
var archive = _$('a.daria-action[title="'+__archiveTitle+'"]')[0]; | |
var folder = _$('.b-folders__folder_current a.b-folders__folder__link[title="'+__archiveTitle+'"]')[0]; | |
return (archive && archive.href == unsafeWindow.location) || (!!folder); | |
} | |
function __addSingleEvents(){ | |
_$('.b-toolbar__item_archive')[0].addEventListener('click', function(event){ | |
event.stopPropagation(); | |
event.preventDefault(); | |
try { | |
_$('a.daria-action[title="'+__archiveTitle+'"]')[0].click(); | |
} catch (e) { | |
var evObj = document.createEvent('Events'); | |
evObj.initEvent('click', true, false); | |
_$('a.daria-action[title="'+__archiveTitle+'"]')[0].dispatchEvent(evObj); | |
} | |
return false; | |
}, false); | |
} | |
function __showButton(){ | |
if (!__showedButton) { | |
_$('.b-toolbar__item_archive')[0].style.display = 'inline-block'; | |
__showedButton = true; | |
} | |
} | |
function __hideButton(){ | |
if (__showedButton) { | |
_$('.b-toolbar__item_archive')[0].style.display = 'none'; | |
__showedButton = false; | |
} | |
} | |
function __isMailChecked(){ | |
return ( !!_$('.block-messages:not([style^="display"]) .b-messages__message__checkbox__input:checked').length); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment