Zdjęcie w tle
ojek

ojek

Praktykant
  • 2wpisy
  • 4komentarzy
Jako, że na głównej widzę że ktoś narzeka na zbyt rzadkie odświeżanie strony głównej, poniżej wklejam skrypt do tampermonkey, który na 24 godziny ukrywa przeczytane wpisy, dzięki temu po każdym odświeżeniu strony pojawiają nam się tylko nowe wpisy. Rozszerzenie ma trochę bugów (jak otworzysz na nowej stronie wpis który ukryłeś, to strona jest pusta), przydałoby się go trochę usprawnić (obecnie ikona ukryj/pokaż pojawia się nad wpisem, mogłaby się pojawiać obok pioruna) - tak więc jak się komuś chce, może to nieco rozwinąć, mi się już nie chce bo 20% effortu pokrywa 80% wymagań - mi wystarczy.
Używam tego już ze dwa miesiące i polecam - aby zainstalować musisz mieć rozszerzenie tampermonkey, dodać nowy skrypt i wkleić w całości to co jest poniżej. #hejto #programowanie #dyskusja
// ==UserScript==
// @name     HejtoSeenFilter
// @namespace    http://tampermonkey.net/
// @version   0.1
// @description try to take over the world!
// @author    You
// @match        https://www.hejto.pl/*
// @grant    none
// ==/UserScript==
(function() {
  'use strict';
  const localStorageKey = "commFilter";
  const cleanupTimeMS = 86400000;//24h
  function filterItems()
  {
    let articles = document.querySelectorAll('article:not(.parsed)');
    let articlesLen = articles.length;
    while (articlesLen--) {
      let article = articles[articlesLen];
      let articleId = getArticleId(article);
      if (!articleId) return;
      article.setAttribute('article-id', articleId);
      let shouldHide = _checkIfItemExists(articleId);
      if (shouldHide) {
        article.classList.add("hidden");
      }
      addHideButton(article, shouldHide);
      article.classList.add("parsed");
    }
  }
  function hideUnhide(articleId) {
    if (!articleId) return;
    let article = document.querySelector("article[article-id='"+articleId+"']");
    if (!article) return;
    var exists = _checkIfItemExists(articleId);
    if (!exists) {
      _addItem(articleId);
      article.classList.add("hidden");
    } else {
      _removeItem(articleId);
      article.classList.remove("hidden");
    }
    let hideUnhideButton = document.querySelector("a[hide-id='"+articleId+"']");
    if (!hideUnhideButton) return;
    hideUnhideButton.innerText = exists ? '' : '';
  }
  function getArticleId(article){
    if (!article) return;
    var articleLink = article.querySelector('div.items-start a.text-grey-500');
    if (!articleLink) {
      articleLink = article.querySelector('a.text-textPrimary-light');
      if (!articleLink) return;
    };
    return articleLink.getAttribute('href');
  }
  function addHideButton(article, isHidden = false){
    if (!article) return;
    let articleId = getArticleId(article);
    if (!articleId) return;
    //
    let innerText = isHidden ? '' : '';
    return article.insertAdjacentHTML("beforebegin", '<a style="margin-left: auto;" href="javascript:void(0);" title="Ukryj" hide-id="'+articleId+'" onclick="hideUnhide(\\''+articleId+'\\');">'+innerText+'</a>');
  }
  function _addItem(item){
    var items = localStorage[localStorageKey];
    if (!items) {
      items = JSON.stringify(new Array());
      localStorage[localStorageKey] = items;
    }
    var arr = JSON.parse(items);
    var itemObj = {_item: item, _timestamp: new Date()};
    var exists = _checkIfItemExists(item);
    if (!exists) {
      arr.push(itemObj);
      items = JSON.stringify(arr);
      localStorage[localStorageKey] = items;
    }
    _cleanLocalStorageExpiredValues();
  }
  function _removeItem(item){
    var items = localStorage[localStorageKey];
    if (!items) {
      items = JSON.stringify(new Array());
      localStorage[localStorageKey] = items;
    }
    var arr = JSON.parse(items);
    var idx = arr.findIndex(x => x._item == item);
    if (idx > -1) {
      arr.splice(idx, 1);
      localStorage[localStorageKey] = JSON.stringify(arr);
    }
    _cleanLocalStorageExpiredValues();
  }
  function _checkIfItemExists(item){
    var items = localStorage[localStorageKey];
    if (!items) return false;
    var arr = JSON.parse(items);
    var exists = arr.some((elem) => elem._item == item);
    return exists;
  }
  function _cleanLocalStorageExpiredValues(){
    var items = localStorage[localStorageKey];
    if (!items) {
      items = JSON.stringify(new Array());
      localStorage[localStorageKey] = items;
    }
    var arr = JSON.parse(items);
    var arrLen = arr.length;
    var date = new Date();
    while(arrLen--){
      var diff = date - new Date(arr[arrLen]._timestamp);
      if (diff > cleanupTimeMS) {
        arr.splice(arrLen, 1);
      }
    }
    items = JSON.stringify(arr);
    localStorage[localStorageKey] = items;
  }
  function init(){
    window.hideUnhide = hideUnhide;
    window.setInterval(filterItems, 500);
  }
  window.addEventListener('load', init);
  //window.addEventListener('focus', init);
})();
ojek

I mała notka - chyba lepiej najpierw wkleić to do notatnika, a stamtąd do tampermonkey; inaczej formatowanie się rozjeżdża z tego co widzę.

Zaloguj się aby komentować