Skip to content

Instantly share code, notes, and snippets.

@lesha-co
Last active July 27, 2018 12:33
Show Gist options
  • Save lesha-co/6066b23d8dc2d967f9460ca65263673e to your computer and use it in GitHub Desktop.
Save lesha-co/6066b23d8dc2d967f9460ca65263673e to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Отключить старые диалоги
// @namespace http://tampermonkey.net/
// @version 0.1
// @description Скрывает диалоги старше месяца ВКонтакте
// @author lichevsky
// @match https://vk.com/im*
// @grant none
// ==/UserScript==
(() => {
const TIMEOUT = 1000 * 60 * 60 * 24 * 30;
const getLastUpdatedDate = dialogNode => dialogNode.querySelector('._im_dialog_date').innerText.trim();
/**
* Преобразует строку вида "01 авг 2016" в Date
*/
const getDateFromString = str => {
const dateRegex = /^(\d{1,2})\s(янв|фев|мар|апр|мая|июн|июл|авг|сен|окт|ноя|дек)(?:\s(\d{4}))?$/;
const months = ["янв", "фев", "мар", "апр", "мая", "июн", "июл", "авг", "сен", "окт", "ноя", "дек"];
const parts = dateRegex.exec(str);
if (parts === null) return null;
const day = parseInt(parts[1]);
const month = months.indexOf(parts[2]);
const year = parts[3] ? parts[3]: new Date().getFullYear();
return new Date(year, month, day);
};
const run = () => {
const dialogs = document.querySelectorAll('.nim-dialog');
const dates = Array.from(dialogs, getLastUpdatedDate);
const datetimes = dates.map(getDateFromString);
const now = new Date();
for (let i = 0; i< dialogs.length; i++) {
if (datetimes[i] === null) continue;
dialogs[i].style.display = (now - datetimes[i] > TIMEOUT) ? 'none' : 'block';
}
};
setInterval(run, 1000);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment