levushka / Hide posts

// ==UserScript==
// @name       Hide posts
// @namespace  http://www.kitsis.ca/
// @version    0.26
// @description  Allow to hide old, readed or not intresting posts from website posts list
// @grant      unsafeWindow
// @updateURL  https://openuserjs.org/install/levushka/Hide_posts.user.js
// @installURL https://openuserjs.org/install/levushka/Hide_posts.user.js
// @downloadURL https://openuserjs.org/install/levushka/Hide_posts.user.js
// @include    http://geektimes.ru/*
// @include    https://geektimes.ru/*
// @include    http://habrahabr.ru/*
// @include    https://habrahabr.ru/*
// @include    http://warnet.ws/*
// @include    http://ibigdan.com/*
// @include    http://avaxhome.cc/*
// @include    http://goodzona.co/*
// @include    http://www.3dnews.ru/*
// @copyright  2013+, Lev Kitsis
// ==/UserScript==


var btn_hide = '<div class="btn_hide" style="float:right; cursor: pointer; border: 2px dotted blue; color: blue; font-weight: bold;">HIDE</div>';

function fn_habrahabr_hide(id, $post) {
  return function() {
    hide_post(id, $post);
  };
}

function check_habrahabr_posts() {
  jQuery('.content_left .posts_list .post').each(function(key, post) {
    var $post = jQuery(post);
    var id = ~~($post.attr('id') || '').replace("post_","");
    if(id === 0 || jQuery.inArray( id, hidden_posts) >= 0) {
      $post.remove();
    } else {
      $post.find('.title').append(btn_hide).find('.btn_hide').click(fn_habrahabr_hide(id, $post));
    }
  });
}

function check_posts(posts_selector, post_id_selector, post_id_attr, post_id_replace, hide_holder_selector) {
  if(posts_selector && typeof jQuery === 'function') {
    jQuery(posts_selector).each(function(key, post) {
      var $post = jQuery(post);
      var $post_id = $post;
      var id = 0;

      if($post && post_id_selector) {
        $post_id = $post.find(post_id_selector);
      }

      if($post_id && $post_id.attr(post_id_attr)) {
        id = ~~$post_id.attr(post_id_attr).replace(post_id_replace, "");
      }

      if(id === 0 || jQuery.inArray( id, hidden_posts) >= 0) {
        $post.remove();
      } else if($post.find('.btn_hide').length === 0) {
        $post.find(hide_holder_selector).append(btn_hide).find('.btn_hide').click(hide_posts(id, $post));
      }
    });
  }
}

function inArray(strData, arrFind) {
  for(var i in arrFind) {
    if(strData.indexOf(arrFind[i]) >= 0) {
      return true;
    }
  }
  return false;
}

function uniqueArray(arr) {
  arr.sort();
  return arr.filter(function(value, index, self) {
    return self.indexOf(value) === index;
  });  
}

function hide_post(id, $post) {
  if($post) {
    $post.remove();
  }
  var hidden_posts = JSON.parse( unsafeWindow.localStorage.getItem("hidden_posts") || '[]' ) ;
  hidden_posts.push(id);
  unsafeWindow.localStorage.setItem("hidden_posts", JSON.stringify(uniqueArray(hidden_posts)));
}

function hide_posts(id, $post) {
  return function(eventObject) {
    hide_post(id, $post);
    eventObject.stopImmediatePropagation();
    return false;
  }
}

function unhide_post(id) {
  var hidden_posts = JSON.parse( unsafeWindow.localStorage.getItem("hidden_posts") || '[]' ) ;
  var index = hidden_posts.indexOf(id);
  if(index >= 0) {
    hidden_posts.splice(index, 1);
    unsafeWindow.localStorage.setItem("hidden_posts", JSON.stringify(uniqueArray(hidden_posts)));
  }
}

function unhide_posts(id) {
  return function() {
    unhide(id);
  }
}

function jqLoad() {
  if(jQueryNotLoaded === false) {
    if(typeof unsafeWindow.jQuery === 'function') {
      unsafeWindow.jQuery.noConflict();
      ready();
    } else {
      console.log('Hide posts: jQuery is not loaded');  
      setTimeout(jqLoad, 1000);
    }
    return;     
  }    
  if(typeof unsafeWindow.$ === 'undefined' && typeof unsafeWindow.jQuery === 'undefined') {
    if(jQueryNotLoaded) {
      jQueryNotLoaded = false;
      var jq = unsafeWindow.document.createElement('script');
      jq.src = "https://code.jquery.com/jquery-latest.min.js";
      unsafeWindow.document.getElementsByTagName('head')[0].appendChild(jq); 
      setTimeout(jqLoad, 1000);
    }
  } else {
    if(typeof unsafeWindow.$ === 'function' && typeof unsafeWindow.jQuery === 'undefined') {
      unsafeWindow.jQuery = unsafeWindow.$;
    }
    ready();
  }


}

function ready() {
  if(typeof jQuery === 'function') {
    if(inArray(url, ['habrahabr.ru', 'geektimes.ru'])) {
      check_habrahabr_posts();
    } else if(inArray(url, ['warnet.ws'])) {
      check_posts('.main-block', 'h3 span', 'id', 'fav_span_', '.post-vote-widget #post-rate');
    } else if(inArray(url, ['ibigdan.com'])) {
      check_posts('.home #page #content #primary #main .post.type-post', null, 'id', 'post-', 'h1:first');
    } else if(inArray(url, ['avaxhome.cc'])) {
      check_posts('#main-info-container .news', null, 'id', 'news-', '.title h1:first');
    } else if(inArray(url, ['goodzona.co'])) {
      check_posts('#allEntries div[id*="entryID"', null, 'id', 'entryID', 'div#filmblock div:last');
    } else if(inArray(url, ['www.3dnews.ru'])) {
      check_posts('.article-entry.article-infeed', null, 'id', '', 'div.entry-info');
    }
  } else {
    console && console.log && console.log('Hide posts: jQuery is not defined');
  }

}

var url = location.href;
var jQueryNotLoaded = true;

unsafeWindow.unhide_post = unhide_post;

var hidden_posts = JSON.parse( unsafeWindow.localStorage.getItem("hidden_posts") || '[]' ) ;

(function(){
  var tId = setInterval(function() {
    if (unsafeWindow.document.readyState == "complete") onComplete()
    }, 11);
  function onComplete(){
    clearInterval(tId);    
    jqLoad();    
  };
})()