Wykop XS dla Wykop.pl

Michał Białek znowu zepsuł mirko (⌐ ͡■ ͜ʖ ͡■) Wykop XS służy do wspomagania działania stylu "Wykop X", który jest wymagany do poprawnego działania niniejszego skryptu. Wykop X zainstaluj stąd: https://userstyles.world/style/8174/wykop-x

As of 07/02/2023. See the latest version.

// ==UserScript==
// @name        Wykop XS dla Wykop.pl
// @version     2.6.0
// @author      SebastianDosiadłgo <[email protected]>
// @author      xXdeepfriedXx: "getObserveCount", "getTagsFingerPrint", "newAutoCompleteWindowAdded" - xXdeepfriedXx napisał funkcje pobierające z API wykopu liczbę obserwujących tagi podczas ich wpisywania! Podziękowania, za uzyczenie kodu.
// @namespace   Violentmonkey Scripts
// @match       https://wykop.pl/*
// @require     https://unpkg.com/xhook@latest/dist/xhook.min.js
// @require     http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js
// @require     https://greasyfork.org/scripts/383527-wait-for-key-elements/code/Wait_for_key_elements.js?version=701631
// @require     https://greasyfork.org/scripts/458629-depaginator-for-wykop-pl/code/Depaginator%20for%20Wykoppl.user.js
// @grant       none
// @license
// @description Michał Białek znowu zepsuł mirko (⌐ ͡■ ͜ʖ ͡■)  Wykop XS służy do wspomagania działania stylu "Wykop X", który jest wymagany do poprawnego działania niniejszego skryptu. Wykop X zainstaluj stąd: https://userstyles.world/style/8174/wykop-x
// ==/UserScript==
const currentVersion = "2.6.0";



const adjacentCode = `
<style>
  /* wykop x promo ON */
  .wykopx_promo { width: 100%; display: block!important; height: 300px!important; background: url('https://raw.githubusercontent.com/wykopx/WykopX/main/promo-images/wykopx-install-sidebar-day.png'); }
  [data-night-mode] .wykopx_promo { background: url('https://raw.githubusercontent.com/wykopx/WykopX/main/promo-images/wykopx-install-sidebar-night.png'); }
  /* hide wykopx features if wykopx is not installed */
  .wykopx { display: none; }
  /* wykopxs-promo OFF */
  section.inline-autocomplete section.inline-autocomplete-stream div.content:after,
  body div.main-content section > section.sidebar:after { display: none!important; }
  /* wykopxs new version available */
  body > section > section.wykopxs_new_version { display: flex;    align-items: center;border-bottom: 1px solid rgba(128, 128, 128, 0.2);color: rgba(128, 128, 128, 1); padding: 8px 20px; font-size: 14px; }
  section.wykopxs_new_version a{display: inline-block;background: rgb(0,85,0);color: white;display: inline-block;background-color: #005200;padding: .3em 0.7em;margin: 0 10px;}
  section.wykopxs_new_version a:hover { background: rgba(0,85,0, 0.7); text-decoration: none!important; }
  section.wykopxs_new_version span { opacity: 0.6; margin-left: auto; }
</style>`;


document.head.insertAdjacentHTML('beforeend', adjacentCode);
$.get(`https://raw.githubusercontent.com/wykopx/WykopX/main/old-versions/wykopxs.${ currentVersion }.gif`)
    .done(function() {
       addWykopXSNewVersionAvailableToast(); // new version available
    }).fail(function() {})

function hideWykopXSPromo()
{
    $(`body div.main-content section > section.sidebar:after`).css("display: none!important;");
}

function addWykopXPromo()
{
   let wykopxpromo = `<section class="wykopx_promo"></section>`;
   $(wykopxpromo).insertBefore(`section.sidebar > footer`);
}


function addWykopXSNewVersionAvailableToast()
{
   let wykopxsnewversionavailabletoast = `<section class="wykopxs_new_version">Dostępna jest nowa wersja skryptu Wykop XS. <a href="https://bit.ly/wykopxs_update_available" target="_blank" style="color: #fff!important;">Zaktualizuj Wykop XS</a> do najnowszej wersji <span>Twoja wersja Wykop XS to v.${ currentVersion }</span></section>`;
   $(wykopxsnewversionavailabletoast).insertAfter(`header.header`);
}


function countNumberOfNotificationsWithDelay()
{
    setTimeout(function(){countNumberOfNotifications()}, 1000);
}

function countNumberOfNotifications()
{
    $("header .right ul li.dropdown").removeClass("unread_5").removeClass("unread_4").removeClass("unread_3").removeClass("unread_2").removeClass("unread_1");
    $("header .right ul li.dropdown:has(a.new)" ).each(function(index, value)
    {
        let numberOfNotifications = 0;
        // liczba powiadomień o tagach / wołaniach
        $(this).find(".notify:not(.read)" ).each(function(index, value)
        {
            ++numberOfNotifications;
            $(this).addClass(`unread_${numberOfNotifications}`);
        }).parents(`.notifications.dropdown`).addClass(`unread_${numberOfNotifications}`);
    })

    $("header .right ul li.pm.dropdown:has(a.new)" ).each(function(index, value)
    {
        let numberOfNotifications = 0;
        // liczba powiadomień o wiadomościach PM
        $(this).find(".item.unread" ).each(function(index, value)
        {
            ++numberOfNotifications;
            $(this).addClass(`unread_${numberOfNotifications}`);
        }).parents(`.pm.dropdown`).addClass(`unread_${numberOfNotifications}`);
        console.log(`[Wykop X]: liczba nowych wiadomości: ${numberOfNotifications}`);
    })
}


navigation.addEventListener('navigate', (event) => { countNumberOfNotificationsWithDelay()});
window.onload = function(event) {
    countNumberOfNotificationsWithDelay();
    addWykopXButtonsToNavBar();
    unrollDropdowns();
    focusOnAddingNewMicroblogEntry();
    addNotificationSummaryButtonToNavBar();
    addWykopXPromo();
    hideWykopXSPromo();
};



  const promoString = "[Przycisk dodany przez Wykop X #wykopwnowymstylu]";

  function addWykopXButtonsToNavBar()
  {
    let $clone = $(`body header nav ul li:not(.active):not(:has(a[href="/wykopalisko"])):first`).clone();
    let $cloneHits = $clone.clone();
    let $cloneMyWykop = $clone.clone();
    let $cloneAddNewEntry = $clone.clone();
    let $cloneAddNewLink = $clone.clone();
    let $cloneWykopWNowymStylu = $clone.clone();
    let $cloneInstallWykopX = $clone.clone();
    let $cloneWykopXWikiDropdown = $(`body header div.right nav ul li.account.dropdown ul.dropdown-body li.settings`).clone();

    $cloneMyWykop.attr({class: "wykopx wykopx_mywykop_li"}).css("display","none").find("a").attr({ href: "/obserwowane", class: "wykopx wykopx_mywykop_button", title: `Mój Wykop ${ promoString }`}).find("span").text("Mój Wykop");
    $cloneHits.attr({class: "wykopx wykopx_hits_li"}).css("display","none").find("a").attr({href: "/hity", class: "wykopx wykopx_hits_button", title: `Hity ${ promoString }`}).find("span").text("Hity");
    $cloneAddNewLink.attr({class: "wykopx wykopx_add_new_link_li wykopx_plus_li"}).css("display","none").find("a").attr({href: "/dodaj-link", class: "wykopx wykopx_add_new_link wykopx_plus_button", title: `Dodaj nowe Znalezisko ${ promoString }`}).find("span").text("+");
    $cloneAddNewEntry.attr({class: "wykopx wykopx_add_new_entry_li wykopx_plus_li"}).css("display","none").find("a").attr({href: "/mikroblog/#dodaj", class: "wykopx wykopx_add_new_entry wykopx_plus_button", title: `Dodaj nowy wpis na Mirko ${ promoString }`}).find("span").text("+");
    $cloneInstallWykopX.attr({class: "wykopx-promo wykopx_install_wykopx_li"}).find("a").attr({href: "https://bit.ly/wykopx_install_wykopx_button", target: "_blank", class: "wykopx-promo wykopx_install_wykopx_button", title: `Zainstaluj style CSS "𝗪𝘆𝗸𝗼𝗽 𝗫" w rozszerzeniu Stylus i odkryj dziesiątki dodatkowych funkcji Wykopu. Masz już zainstalowane rozszerzenie `}).find("span").text("Zainstaluj Wykop X");
    $cloneWykopXWikiDropdown.attr({class: "wykopx wykopx_wykopxwiki_wykopx_li"}).find("a").attr({href: "https://github.com/wykopx/WykopX/wiki", target: "_blank", class: "wykopx wykopx_wykopxwiki_wykopx_button", title: `Wykop X - instrukcja i spis wszystkich funkcji`}).text("Wykop X - Obsługa");

    $cloneHits.insertAfter(`body header nav.main ul li:has(a[href="/wykopalisko"])`);
    $cloneAddNewLink.insertAfter(`body header nav.main ul li:has(a[href="/wykopalisko"])`);
    $cloneAddNewEntry.insertAfter(`body header nav.main ul li:has(a[href="/mikroblog"])`);
    $cloneMyWykop.appendTo(`body header nav.main ul`);
    $cloneInstallWykopX.appendTo(`body header nav.main ul`);

    $cloneWykopXWikiDropdown.appendTo(`body header div.right nav ul li.account.dropdown ul.dropdown-body`);
}


function addNotificationSummaryButtonToNavBar()
{
    let wykopx_mywykop_mobile_li = `<li class="wykopx wykopx_mywykop_mobile_li dropdown"><a href="/obserwowane" class="wykopx wykopx_mywykop_mobile_button" title="Mój Wykop ${ promoString }"><figure> </figure></a></li>`;
    let wykopx_microblog_mobile_li = `<li class="wykopx wykopx_microblog_mobile_li dropdown"><a href="/mikroblog" class="wykopx wykopx_microblog_mobile_button" title="Mikroblog ${ promoString }"><figure> </figure></a></li>`;
    let wykopx_wykopwnowymstylu_li = `<li class="wykopx wykopx_wykopwnowymstylu_li dropdown"><a href="/tag/wykopwnowymstylu" class="wykopx wykopx_wykopwnowymstylu_button" title="Przejdź na #wykopwnowymstylu"><span>#</span></a></li>`;
    let wykopx_notification_summary = `<li class="notifications dropdown wykopx wykopx_notification_summary" title="Masz nowe powiadomienia ${ promoString }"><a href="/powiadomienia/"><figure></figure></a></li>`;
    $(wykopx_wykopwnowymstylu_li).appendTo(`header.header > .right > nav > ul`);
    $(wykopx_mywykop_mobile_li).appendTo(`header.header > .right > nav > ul`);
    $(wykopx_microblog_mobile_li).appendTo(`header.header > .right > nav > ul`);
    $(wykopx_notification_summary).prependTo(`header.header > .right > nav > ul`);
}



function focusOnAddingNewMicroblogEntry()
{
    let hash = new URL(document.URL).hash;
    if(hash == "#dodaj")
    {
       document.querySelector(`section.microblog-page section.microblog section.editor div.content textarea`).focus();
    }
}

// waitForKeyElements("section.stream div.content section.entry div.comments section.entry-subcomments div.content section.reply", sortCommentsByID);
// document.removeEventListener('click', this.documentClick)


function unrollDropdowns(dropdown)
{
    console.log("Wykop X :: unrollDropdowns")
    //console.log(dropdown)
    document.removeEventListener('click', this.documentClick)
}

function sortCommentsByID(comment)
{
    let commentID = comment.attr ("id").substr(8, 9);
    //console.log(commentID);
    comment.css("order", commentID);
}



/* browarek programisto piekielny, przestań mi kurka kod prześladować! */

waitForKeyElements(".inline-autocomplete-stream", newAutoCompleteWindowAdded);

const tagsAutocompleteURL = "https://wykop.pl/api/v3/tags/"
const profilesAutocompleteURL = "https://wykop.pl/api/v3/profile/users/"

function newAutoCompleteWindowAdded(element) {
    let suggestions = element[0].getElementsByTagName('span');
    if (typeof this.lastFingerPrint == "undefined" || this.lastCount != suggestions.length || this.lastFingerPrint != getTagsFingerPrint(suggestions)) {
        for (let i = 0; i < suggestions.length; i++) {
            let suggestion = suggestions[i];
            getObserveCount(suggestion);
        }
    }
    this.lastFingerPrint = getTagsFingerPrint(suggestions);
    this.lastCount = suggestions.length
    return true;
}

function getTagsFingerPrint(suggestions) {
    let fingerPrint = '';
    for (let i = 0; i < suggestions.length; i++) {
        let suggestion = suggestions[i];
        fingerPrint += suggestion.innerText.substring(1,).split(' ')[0];
    }
    return fingerPrint;
}

function getObserveCount(element) {
    var type = element.innerText.substring(0,1);
    var query = element.innerText.substring(1,);
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            var details = JSON.parse(this.responseText);
            addCount(type, element, details.data)
        }
    };
    xmlhttp.open("GET", (type === '@' ? profilesAutocompleteURL : tagsAutocompleteURL) + query);
    xmlhttp.setRequestHeader("Authorization", "Bearer " + window.localStorage.token)
    xmlhttp.send();
}

function addCount(prefix, element, data) {
    const name = (prefix === '@' ? data.username : data.name)
    const followers = (prefix === '@' ? data.summary.followers : data.followers)
    if(prefix != '@')
    {
        $(element)
            .parent()
            .attr({style: `order: ${ followers }`, "data-followers":`${ followers }`})
            .html(`<div class="wykopx_autocomplete_element wykopx_autocomplete_tag">
                                     <div class="wykopx_autocomplete_prefix">${ prefix }</div>
                                     <div class="wykopx_autocomplete_name">${ name }</div>
                                     <div class="wykopx_followers_counter">${ followers }</div>
                                     <meter class="wykopx_followers_meter" min="0" max="1000" low="99" high="500" optimum="999"  value="${ followers }"></meter>`);

    }
}