Skip to content

Instantly share code, notes, and snippets.

@antosha4e
Created May 6, 2018 15:14
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 antosha4e/60fd76bc16ef65932ec3686892074df9 to your computer and use it in GitHub Desktop.
Save antosha4e/60fd76bc16ef65932ec3686892074df9 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name Habrahabr Userscript
// @namespace habra
// @version 0.1
// @description Add filters to habr site
// @author anton.arsentyev
// @match https://habrahabr.ru/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
var $ = window.$ || window.jQuery;
var postList = $('.post'); // .posts
var hiddenPosts = getArrayFromStorage('hiddenPosts');
var hiddenHubs = ['FPGA', 'Yii', 'Восстановление данных', 'DNS', 'Symphony', 'CAD/CAM', 'Резервное копирование'];
postList.each(function(ind, elem) {
elem = $(elem);
var ID = /post_(\d+)/.exec(elem.attr('id'));
ID = ID ? ID[1] : '';
//console.info(ID);
var buttonText = 'Hide Post';
var hasHiddenHub = elem.find('.hub').filter(function(ind, elem) {return hiddenHubs.indexOf(elem.innerText) >= 0;}).length > 0;
if(isPostHidden(hiddenPosts, ID) || hasHiddenHub) {
hidePost(elem);
buttonText = 'Show Post';
}
var button = $('<div class="buttons" style="display: inline-block;"><a class="button">' + buttonText + '</a></div>');
button.click(function() {
if(isPostHidden(hiddenPosts, ID) || hasHiddenHub) {
button.find('a').html('Hide post');
showPost(elem);
hiddenPosts.splice(hiddenPosts.indexOf(ID), 1);
localStorage.setItem('hiddenPosts', JSON.stringify(hiddenPosts));
} else {
button.find('a').html('Show post');
hidePost(elem);
hiddenPosts.push(ID);
localStorage.setItem('hiddenPosts', JSON.stringify(hiddenPosts));
}
});
elem.find('.post__title').append(button);
});
// TODO add blocks by HUB and FLOWS
function getArrayFromStorage(name) {
var arr = localStorage.getItem(name);
return arr ? JSON.parse(arr) : new Array();
}
function isPostHidden(hiddenPosts, ID) {
return hiddenPosts.indexOf(ID) >= 0;
}
function hidePost(post) {
post.find('.post__body').hide();
//post.find('.post__body').css('opacity', 0.4);
}
function showPost(post) {
post.find('.post__body').show();
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment